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