kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat(web): replace redirect next/navigation with useRouter
rodzic
2f18f53b4b
commit
63a7daed33
|
@ -1,7 +1,8 @@
|
|||
'use client'
|
||||
|
||||
import { sanitizeSearchParams } from '@agentic/platform-core'
|
||||
import { redirect, RedirectType, useSearchParams } from 'next/navigation'
|
||||
import { useSearchParams } from 'next/navigation'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useEffect } from 'react'
|
||||
|
||||
import {
|
||||
|
@ -22,6 +23,7 @@ export function OAuthSuccessCallback({
|
|||
const code = searchParams.get('code')
|
||||
const ctx = useUnauthenticatedAgentic()
|
||||
const nextUrl = useNextUrl()
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
;(async function () {
|
||||
|
@ -40,15 +42,14 @@ export function OAuthSuccessCallback({
|
|||
} catch (err) {
|
||||
await toastError(err, { label: 'Auth error' })
|
||||
|
||||
return redirect(
|
||||
`/login?${sanitizeSearchParams({ next: nextUrl }).toString()}`,
|
||||
RedirectType.replace
|
||||
return router.replace(
|
||||
`/login?${sanitizeSearchParams({ next: nextUrl }).toString()}`
|
||||
)
|
||||
}
|
||||
|
||||
return redirect(nextUrl || '/app', RedirectType.replace)
|
||||
return router.replace(nextUrl || '/app')
|
||||
})()
|
||||
}, [code, ctx, nextUrl])
|
||||
}, [code, ctx, nextUrl, router])
|
||||
|
||||
return <LoadingIndicator />
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import { sanitizeSearchParams } from '@agentic/platform-core'
|
|||
import { isValidEmail, isValidPassword } from '@agentic/platform-validators'
|
||||
import { useForm } from '@tanstack/react-form'
|
||||
import { Loader2Icon } from 'lucide-react'
|
||||
import { redirect, RedirectType } from 'next/navigation'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useCallback } from 'react'
|
||||
import { z } from 'zod'
|
||||
|
||||
|
@ -22,14 +22,15 @@ import { cn } from '@/lib/utils'
|
|||
export function LoginForm() {
|
||||
const ctx = useUnauthenticatedAgentic()
|
||||
const nextUrl = useNextUrl()
|
||||
const router = useRouter()
|
||||
|
||||
const onAuthWithGitHub = useCallback(async () => {
|
||||
const redirectUri = `${globalThis.location.origin}/auth/github/success?${sanitizeSearchParams({ next: nextUrl }).toString()}`
|
||||
|
||||
const url = await ctx!.api.initAuthFlowWithGitHub({ redirectUri })
|
||||
|
||||
redirect(url, RedirectType.push)
|
||||
}, [ctx, nextUrl])
|
||||
void router.push(url)
|
||||
}, [ctx, nextUrl, router])
|
||||
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
|
@ -58,7 +59,7 @@ export function LoginForm() {
|
|||
return
|
||||
}
|
||||
|
||||
return redirect(nextUrl || '/app', RedirectType.push)
|
||||
return router.push(nextUrl || '/app')
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
'use client'
|
||||
|
||||
// import { redirect, RedirectType } from 'next/navigation'
|
||||
import { useEffect } from 'react'
|
||||
|
||||
import { useAuthenticatedAgentic } from '@/components/agentic-provider'
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
import { assert, omit, sanitizeSearchParams } from '@agentic/platform-core'
|
||||
import { Loader2Icon } from 'lucide-react'
|
||||
import { redirect, useSearchParams } from 'next/navigation'
|
||||
import { useSearchParams } from 'next/navigation'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
import { useAgentic } from '@/components/agentic-provider'
|
||||
|
@ -22,6 +23,7 @@ export function MarketplaceProjectIndex({
|
|||
const plan = searchParams.get('plan')
|
||||
const [isLoadingStripeCheckoutForPlan, setIsLoadingStripeCheckoutForPlan] =
|
||||
useState<string | null>(null)
|
||||
const router = useRouter()
|
||||
|
||||
// Load the public project
|
||||
const {
|
||||
|
@ -70,7 +72,7 @@ export function MarketplaceProjectIndex({
|
|||
)
|
||||
|
||||
if (!ctx.isAuthenticated) {
|
||||
return redirect(
|
||||
return router.push(
|
||||
`/signup?${sanitizeSearchParams({
|
||||
next: `/marketplace/projects/${projectIdentifier}?checkout=true&plan=${pricingPlanSlug}`
|
||||
}).toString()}`
|
||||
|
@ -94,9 +96,9 @@ export function MarketplaceProjectIndex({
|
|||
setIsLoadingStripeCheckoutForPlan(null)
|
||||
}
|
||||
|
||||
redirect(checkoutSession.url)
|
||||
return router.push(checkoutSession.url)
|
||||
},
|
||||
[ctx, projectIdentifier, project]
|
||||
[ctx, projectIdentifier, project, router]
|
||||
)
|
||||
|
||||
const hasInitializedCheckoutFromSearchParams = useRef(false)
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
} from '@agentic/platform-validators'
|
||||
import { useForm } from '@tanstack/react-form'
|
||||
import { Loader2Icon } from 'lucide-react'
|
||||
import { redirect, RedirectType } from 'next/navigation'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useCallback } from 'react'
|
||||
import { z } from 'zod'
|
||||
|
||||
|
@ -26,13 +26,14 @@ import { cn } from '@/lib/utils'
|
|||
export function SignupForm() {
|
||||
const ctx = useUnauthenticatedAgentic()
|
||||
const nextUrl = useNextUrl()
|
||||
const router = useRouter()
|
||||
|
||||
const onAuthWithGitHub = useCallback(async () => {
|
||||
const redirectUri = `${globalThis.location.origin}/auth/github/success?${sanitizeSearchParams({ next: nextUrl }).toString()}`
|
||||
const url = await ctx!.api.initAuthFlowWithGitHub({ redirectUri })
|
||||
|
||||
redirect(url, RedirectType.push)
|
||||
}, [ctx, nextUrl])
|
||||
return router.push(url)
|
||||
}, [ctx, nextUrl, router])
|
||||
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
|
@ -71,7 +72,7 @@ export function SignupForm() {
|
|||
return
|
||||
}
|
||||
|
||||
return redirect(nextUrl || '/app', RedirectType.push)
|
||||
return router.push(nextUrl || '/app')
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -5,12 +5,8 @@ import {
|
|||
type AuthSession
|
||||
} from '@agentic/platform-api-client'
|
||||
import { sanitizeSearchParams } from '@agentic/platform-core'
|
||||
import {
|
||||
redirect,
|
||||
RedirectType,
|
||||
usePathname,
|
||||
useSearchParams
|
||||
} from 'next/navigation'
|
||||
import { usePathname, useSearchParams } from 'next/navigation'
|
||||
import { useRouter } from 'next/router'
|
||||
import {
|
||||
createContext,
|
||||
type ReactNode,
|
||||
|
@ -131,10 +127,12 @@ export function useAgentic(): AgenticContextType | undefined {
|
|||
export function useUnauthenticatedAgentic(): AgenticContextType | undefined {
|
||||
const ctx = useAgentic()
|
||||
const nextUrl = useNextUrl() || '/app'
|
||||
const router = useRouter()
|
||||
|
||||
if (ctx && ctx.isAuthenticated) {
|
||||
console.log('REQUIRES NO AUTHENTICATION: redirecting to', nextUrl)
|
||||
return redirect(nextUrl, RedirectType.replace)
|
||||
void router.replace(nextUrl)
|
||||
return
|
||||
}
|
||||
|
||||
return ctx
|
||||
|
@ -143,21 +141,23 @@ export function useUnauthenticatedAgentic(): AgenticContextType | undefined {
|
|||
export function useAuthenticatedAgentic(): AgenticContextType | undefined {
|
||||
const ctx = useAgentic()
|
||||
const pathname = usePathname()
|
||||
const router = useRouter()
|
||||
|
||||
if (ctx && !ctx.isAuthenticated) {
|
||||
if (pathname === '/logout') {
|
||||
console.log('LOGOUT SUCCESS: redirecting to /')
|
||||
return redirect('/', RedirectType.replace)
|
||||
void router.replace('/')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('REQUIRES AUTHENTICATION: redirecting to /login', {
|
||||
next: pathname
|
||||
})
|
||||
|
||||
return redirect(
|
||||
`/login?${sanitizeSearchParams({ next: pathname }).toString()}`,
|
||||
RedirectType.replace
|
||||
void router.replace(
|
||||
`/login?${sanitizeSearchParams({ next: pathname }).toString()}`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
return ctx
|
||||
|
|
Ładowanie…
Reference in New Issue