@@ -4,10 +4,12 @@ import { parseAsString, useQueryStates } from 'nuqs'
44import { useRef , useState } from 'react'
55import { toast } from 'sonner'
66import * as yup from 'yup'
7+ import { useRouter } from 'next/router'
78
89import { useSignUpMutation } from 'data/misc/signup-mutation'
910import { BASE_PATH } from 'lib/constants'
1011import { passwordSchema } from 'lib/schemas'
12+ import { buildPathWithParams } from 'lib/gotrue'
1113import {
1214 AlertDescription_Shadcn_ ,
1315 AlertTitle_Shadcn_ ,
@@ -28,6 +30,7 @@ const SignUpForm = () => {
2830 const [ isSubmitted , setIsSubmitted ] = useState ( false )
2931 const [ passwordHidden , setPasswordHidden ] = useState ( true )
3032 const [ captchaToken , setCaptchaToken ] = useState < string | null > ( null )
33+ const router = useRouter ( )
3134
3235 const [ searchParams ] = useQueryStates ( {
3336 auth_id : parseAsString . withDefault ( '' ) ,
@@ -59,9 +62,19 @@ const SignUpForm = () => {
5962 ? location . origin
6063 : process . env . NEXT_PUBLIC_SITE_URL
6164 } ${ BASE_PATH } `
62- const redirectTo = isInsideOAuthFlow
63- ? `${ redirectUrlBase } /authorize?auth_id=${ searchParams . auth_id } ${ searchParams . token && `&token=${ searchParams . token } ` } `
64- : `${ redirectUrlBase } /sign-in`
65+
66+ let redirectTo : string
67+
68+ if ( isInsideOAuthFlow ) {
69+ redirectTo = `${ redirectUrlBase } /authorize?auth_id=${ searchParams . auth_id } ${ searchParams . token && `&token=${ searchParams . token } ` } `
70+ } else {
71+ // Use getRedirectToPath to handle redirect_to parameter and other query params
72+ const { returnTo } = router . query
73+ const basePath = returnTo || '/sign-in'
74+ const fullPath = buildPathWithParams ( basePath as string )
75+ const fullRedirectUrl = `${ redirectUrlBase } ${ fullPath } `
76+ redirectTo = fullRedirectUrl
77+ }
6578
6679 signup ( {
6780 email,
0 commit comments