kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: fix github auth flow
rodzic
ebd3e0928a
commit
eaa0e3aa33
|
@ -26,6 +26,7 @@
|
||||||
"@agentic/platform-types": "workspace:*",
|
"@agentic/platform-types": "workspace:*",
|
||||||
"@agentic/platform-validators": "workspace:*",
|
"@agentic/platform-validators": "workspace:*",
|
||||||
"@radix-ui/react-dropdown-menu": "^2.1.15",
|
"@radix-ui/react-dropdown-menu": "^2.1.15",
|
||||||
|
"@radix-ui/react-label": "^2.1.7",
|
||||||
"@radix-ui/react-slot": "^1.2.3",
|
"@radix-ui/react-slot": "^1.2.3",
|
||||||
"@radix-ui/react-tooltip": "^1.2.7",
|
"@radix-ui/react-tooltip": "^1.2.7",
|
||||||
"@tanstack/react-form": "^1.12.3",
|
"@tanstack/react-form": "^1.12.3",
|
||||||
|
|
|
@ -1,24 +1,10 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import type { AuthorizeResult } from '@agentic/platform-api-client'
|
|
||||||
import { assert } from '@agentic/platform-core'
|
import { assert } from '@agentic/platform-core'
|
||||||
import { useSearchParams } from 'next/navigation'
|
import { redirect, RedirectType, useSearchParams } from 'next/navigation'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
import { useLocalStorage } from 'react-use'
|
|
||||||
|
|
||||||
import { useAgentic } from '@/components/agentic-provider'
|
import { useUnauthenticatedAgentic } from '@/components/agentic-provider'
|
||||||
|
|
||||||
// function FieldInfo({ field }: { field: AnyFieldApi }) {
|
|
||||||
// return (
|
|
||||||
// <>
|
|
||||||
// {field.state.meta.isTouched && !field.state.meta.isValid ? (
|
|
||||||
// <em>{field.state.meta.errors.join(',')}</em>
|
|
||||||
// ) : null}
|
|
||||||
|
|
||||||
// {field.state.meta.isValidating ? 'Validating...' : null}
|
|
||||||
// </>
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
|
|
||||||
export default async function Page({
|
export default async function Page({
|
||||||
params
|
params
|
||||||
|
@ -31,13 +17,16 @@ export default async function Page({
|
||||||
return <SuccessPage provider={provider} />
|
return <SuccessPage provider={provider} />
|
||||||
}
|
}
|
||||||
|
|
||||||
function SuccessPage({ provider }: { provider: string }) {
|
function SuccessPage({
|
||||||
|
provider:
|
||||||
|
// TODO
|
||||||
|
_provider
|
||||||
|
}: {
|
||||||
|
provider: string
|
||||||
|
}) {
|
||||||
const searchParams = useSearchParams()
|
const searchParams = useSearchParams()
|
||||||
const code = searchParams.get('code')
|
const code = searchParams.get('code')
|
||||||
const { api } = useAgentic()
|
const ctx = useUnauthenticatedAgentic()
|
||||||
const [authResult] = useLocalStorage<AuthorizeResult | undefined>(
|
|
||||||
'auth-result'
|
|
||||||
)
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
;(async function () {
|
;(async function () {
|
||||||
|
@ -46,31 +35,16 @@ function SuccessPage({ provider }: { provider: string }) {
|
||||||
throw new Error('Missing code or challenge')
|
throw new Error('Missing code or challenge')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!authResult) {
|
// TODO: make generic using `provider`
|
||||||
// TODO
|
const authSession = await ctx!.api.exchangeOAuthCodeWithGitHub({
|
||||||
throw new Error('Missing auth-result')
|
code
|
||||||
}
|
|
||||||
|
|
||||||
if (!authResult.challenge) {
|
|
||||||
// TODO
|
|
||||||
throw new Error('Missing challenge')
|
|
||||||
}
|
|
||||||
|
|
||||||
const authUser = await api.exchangeAuthCode({
|
|
||||||
code,
|
|
||||||
redirectUri: new URL(
|
|
||||||
`/auth/${provider}/success`,
|
|
||||||
globalThis.window.location.origin
|
|
||||||
).toString(),
|
|
||||||
verifier: authResult.challenge?.verifier
|
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('AUTH SUCCESS', {
|
console.log('AUTH SUCCESS', { authSession })
|
||||||
authUser,
|
|
||||||
authTokens: api.authTokens
|
redirect('/app', RedirectType.replace)
|
||||||
})
|
|
||||||
})()
|
})()
|
||||||
}, [code, api, authResult, provider])
|
}, [code, ctx])
|
||||||
|
|
||||||
// TODO: show a loading state
|
// TODO: show a loading state
|
||||||
return null
|
return null
|
||||||
|
|
|
@ -4,11 +4,14 @@ import type { PasswordLoginError } from '@agentic/openauth/provider/password'
|
||||||
import { isValidEmail, isValidPassword } from '@agentic/platform-validators'
|
import { isValidEmail, isValidPassword } from '@agentic/platform-validators'
|
||||||
import { useForm } from '@tanstack/react-form'
|
import { useForm } from '@tanstack/react-form'
|
||||||
import { redirect, RedirectType } from 'next/navigation'
|
import { redirect, RedirectType } from 'next/navigation'
|
||||||
import { useState } from 'react'
|
import { useCallback, useState } from 'react'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
import { useUnauthenticatedAgentic } from '@/components/agentic-provider'
|
import { useUnauthenticatedAgentic } from '@/components/agentic-provider'
|
||||||
import { authCopy } from '@/lib/auth-copy'
|
import { Button } from '@/components/ui/button'
|
||||||
|
import { Input } from '@/components/ui/input'
|
||||||
|
import { Label } from '@/components/ui/label'
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
// function FieldInfo({ field }: { field: AnyFieldApi }) {
|
// function FieldInfo({ field }: { field: AnyFieldApi }) {
|
||||||
// return (
|
// return (
|
||||||
|
@ -57,6 +60,14 @@ export default function LoginPage() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const onAuthWithGitHub = useCallback(async () => {
|
||||||
|
const url = await ctx!.api.initAuthFlowWithGitHub({
|
||||||
|
redirectUri: `${globalThis.location.origin}/auth/github/success`
|
||||||
|
})
|
||||||
|
|
||||||
|
redirect(url, RedirectType.push)
|
||||||
|
}, [ctx])
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
if (!ctx) return null
|
if (!ctx) return null
|
||||||
|
|
||||||
|
@ -67,85 +78,120 @@ export default function LoginPage() {
|
||||||
Log In
|
Log In
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<form
|
<div className='flex flex-1 items-center justify-center'>
|
||||||
onSubmit={(e) => {
|
<div className='w-full max-w-xs'>
|
||||||
e.preventDefault()
|
<form
|
||||||
void form.handleSubmit()
|
className={cn('flex flex-col gap-6')}
|
||||||
}}
|
onSubmit={(e) => {
|
||||||
>
|
e.preventDefault()
|
||||||
{/* <FormAlert
|
void form.handleSubmit()
|
||||||
message={error?.type && authCopy?.[`error_${error.type}`]}
|
}}
|
||||||
/> */}
|
>
|
||||||
|
<div className='flex flex-col items-center gap-2 text-center'>
|
||||||
|
<h1 className='text-2xl font-bold'>Login to your account</h1>
|
||||||
|
<p className='text-muted-foreground text-sm text-balance'>
|
||||||
|
Enter your email below to login to your account
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<form.Field
|
<div className='grid gap-6'>
|
||||||
name='email'
|
<form.Field
|
||||||
children={(field) => (
|
name='email'
|
||||||
<>
|
children={(field) => (
|
||||||
{/* <label htmlFor={field.name}>Email:</label> */}
|
<div className='grid gap-3'>
|
||||||
|
<Label htmlFor={field.name}>Email</Label>
|
||||||
|
|
||||||
<input
|
<Input
|
||||||
id={field.name}
|
id={field.name}
|
||||||
name={field.name}
|
name={field.name}
|
||||||
type='email'
|
type='email'
|
||||||
required
|
required
|
||||||
placeholder={authCopy.input_email}
|
placeholder='Email'
|
||||||
autoFocus={!error}
|
autoFocus={!error}
|
||||||
value={field.state.value}
|
value={field.state.value}
|
||||||
onBlur={field.handleBlur}
|
onBlur={field.handleBlur}
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
onChange={(e: any) =>
|
||||||
|
field.handleChange(e.target.value)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* <FieldInfo field={field} /> */}
|
<form.Field
|
||||||
</>
|
name='password'
|
||||||
)}
|
children={(field) => (
|
||||||
/>
|
<div className='grid gap-3'>
|
||||||
|
<div className='flex items-center'>
|
||||||
|
<Label htmlFor={field.name}>Password</Label>
|
||||||
|
|
||||||
<form.Field
|
<a
|
||||||
name='password'
|
href='/forgot-password'
|
||||||
children={(field) => (
|
className='ml-auto text-sm underline-offset-4 hover:underline'
|
||||||
<>
|
>
|
||||||
{/* <label htmlFor={field.name}>Password:</label> */}
|
Forgot your password?
|
||||||
|
</a>
|
||||||
|
|
||||||
<input
|
<Input
|
||||||
id={field.name}
|
id={field.name}
|
||||||
name={field.name}
|
name={field.name}
|
||||||
type='password'
|
type='password'
|
||||||
required
|
required
|
||||||
placeholder={authCopy.input_password}
|
placeholder='Password'
|
||||||
autoFocus={error?.type === 'invalid_password'}
|
autoFocus={error?.type === 'invalid_password'}
|
||||||
autoComplete='current-password'
|
autoComplete='current-password'
|
||||||
value={field.state.value}
|
value={field.state.value}
|
||||||
onBlur={field.handleBlur}
|
onBlur={field.handleBlur}
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* <FieldInfo field={field} /> */}
|
<form.Subscribe
|
||||||
</>
|
selector={(state) => [state.canSubmit, state.isSubmitting]}
|
||||||
)}
|
children={([canSubmit, isSubmitting]) => (
|
||||||
/>
|
<Button
|
||||||
|
type='submit'
|
||||||
|
disabled={!canSubmit || !ctx}
|
||||||
|
className='w-full'
|
||||||
|
>
|
||||||
|
{isSubmitting ? '...' : 'Login'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
<form.Subscribe
|
<div className='after:border-border relative text-center text-sm after:absolute after:inset-0 after:top-1/2 after:z-0 after:flex after:items-center after:border-t'>
|
||||||
selector={(state) => [state.canSubmit, state.isSubmitting]}
|
<span className='bg-background text-muted-foreground relative z-10 px-2'>
|
||||||
children={([canSubmit, isSubmitting]) => (
|
Or continue with
|
||||||
<button type='submit' disabled={!canSubmit || !ctx}>
|
</span>
|
||||||
{isSubmitting ? '...' : authCopy.button_continue}
|
</div>
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div data-component='form-footer'>
|
<Button
|
||||||
<span>
|
variant='outline'
|
||||||
{authCopy.register_prompt}{' '}
|
className='w-full'
|
||||||
<a data-component='link' href='/signup'>
|
onClick={onAuthWithGitHub}
|
||||||
{authCopy.register}
|
>
|
||||||
</a>
|
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'>
|
||||||
</span>
|
<path
|
||||||
|
d='M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12'
|
||||||
|
fill='currentColor'
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
Login with GitHub
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<a data-component='link' href='/forgot-password'>
|
<div className='text-center text-sm'>
|
||||||
{authCopy.change_prompt}
|
Don't have an account?{' '}
|
||||||
</a>
|
<a href='/signup' className='underline underline-offset-4'>
|
||||||
|
Sign up
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
|
@ -524,6 +524,9 @@ importers:
|
||||||
'@radix-ui/react-dropdown-menu':
|
'@radix-ui/react-dropdown-menu':
|
||||||
specifier: ^2.1.15
|
specifier: ^2.1.15
|
||||||
version: 2.1.15(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
|
version: 2.1.15(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
|
||||||
|
'@radix-ui/react-label':
|
||||||
|
specifier: ^2.1.7
|
||||||
|
version: 2.1.7(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
|
||||||
'@radix-ui/react-slot':
|
'@radix-ui/react-slot':
|
||||||
specifier: ^1.2.3
|
specifier: ^1.2.3
|
||||||
version: 1.2.3(@types/react@19.1.8)(react@19.1.0)
|
version: 1.2.3(@types/react@19.1.8)(react@19.1.0)
|
||||||
|
@ -2611,6 +2614,19 @@ packages:
|
||||||
'@types/react':
|
'@types/react':
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
'@radix-ui/react-label@2.1.7':
|
||||||
|
resolution: {integrity: sha512-YT1GqPSL8kJn20djelMX7/cTRp/Y9w5IZHvfxQTVHrOqa2yMl7i/UfMqKRU5V7mEyKTrUVgJXhNQPVCG8PBLoQ==}
|
||||||
|
peerDependencies:
|
||||||
|
'@types/react': '*'
|
||||||
|
'@types/react-dom': '*'
|
||||||
|
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||||
|
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
|
||||||
|
peerDependenciesMeta:
|
||||||
|
'@types/react':
|
||||||
|
optional: true
|
||||||
|
'@types/react-dom':
|
||||||
|
optional: true
|
||||||
|
|
||||||
'@radix-ui/react-menu@2.1.15':
|
'@radix-ui/react-menu@2.1.15':
|
||||||
resolution: {integrity: sha512-tVlmA3Vb9n8SZSd+YSbuFR66l87Wiy4du+YE+0hzKQEANA+7cWKH1WgqcEX4pXqxUFQKrWQGHdvEfw00TjFiew==}
|
resolution: {integrity: sha512-tVlmA3Vb9n8SZSd+YSbuFR66l87Wiy4du+YE+0hzKQEANA+7cWKH1WgqcEX4pXqxUFQKrWQGHdvEfw00TjFiew==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
|
@ -8395,6 +8411,15 @@ snapshots:
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
'@types/react': 19.1.8
|
'@types/react': 19.1.8
|
||||||
|
|
||||||
|
'@radix-ui/react-label@2.1.7(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)':
|
||||||
|
dependencies:
|
||||||
|
'@radix-ui/react-primitive': 2.1.3(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
|
||||||
|
react: 19.1.0
|
||||||
|
react-dom: 19.1.0(react@19.1.0)
|
||||||
|
optionalDependencies:
|
||||||
|
'@types/react': 19.1.8
|
||||||
|
'@types/react-dom': 19.1.6(@types/react@19.1.8)
|
||||||
|
|
||||||
'@radix-ui/react-menu@2.1.15(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)':
|
'@radix-ui/react-menu@2.1.15(@types/react-dom@19.1.6(@types/react@19.1.8))(@types/react@19.1.8)(react-dom@19.1.0(react@19.1.0))(react@19.1.0)':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@radix-ui/primitive': 1.1.2
|
'@radix-ui/primitive': 1.1.2
|
||||||
|
|
Ładowanie…
Reference in New Issue