11import { redirect } from '@tanstack/react-router' ;
22import { createServerFn } from '@tanstack/react-start' ;
33import type { Impersonator , User } from '../types.js' ;
4- import { getRawAuthFromContext , refreshSession } from './auth-helpers.js' ;
4+ import { getRawAuthFromContext , refreshSession , getRedirectUriFromContext } from './auth-helpers.js' ;
55import { getAuthkit } from './authkit-loader.js' ;
66
77// Type-only import - safe for bundling
@@ -159,6 +159,16 @@ export const getAuthorizationUrl = createServerFn({ method: 'GET' })
159159 . inputValidator ( ( options ?: GetAuthURLOptions ) => options )
160160 . handler ( async ( { data : options = { } } ) => {
161161 const authkit = await getAuthkit ( ) ;
162+ const contextRedirectUri = getRedirectUriFromContext ( ) ;
163+
164+ // Only inject context redirectUri if it exists and user didn't provide one
165+ if ( contextRedirectUri && ! options . redirectUri ) {
166+ return authkit . getAuthorizationUrl ( {
167+ ...options ,
168+ redirectUri : contextRedirectUri ,
169+ } ) ;
170+ }
171+
162172 return authkit . getAuthorizationUrl ( options ) ;
163173 } ) ;
164174
@@ -185,7 +195,17 @@ export const getSignInUrl = createServerFn({ method: 'GET' })
185195 . inputValidator ( ( data ?: string | SignInUrlOptions ) => data )
186196 . handler ( async ( { data } ) => {
187197 const options = typeof data === 'string' ? { returnPathname : data } : data ;
198+ const contextRedirectUri = getRedirectUriFromContext ( ) ;
188199 const authkit = await getAuthkit ( ) ;
200+
201+ // Only inject context redirectUri if it exists and user didn't provide one
202+ if ( contextRedirectUri && ! options ?. redirectUri ) {
203+ return authkit . getSignInUrl ( {
204+ ...options ,
205+ redirectUri : contextRedirectUri ,
206+ } ) ;
207+ }
208+
189209 return authkit . getSignInUrl ( options ) ;
190210 } ) ;
191211
@@ -209,7 +229,17 @@ export const getSignUpUrl = createServerFn({ method: 'GET' })
209229 . inputValidator ( ( data ?: string | SignInUrlOptions ) => data )
210230 . handler ( async ( { data } ) => {
211231 const options = typeof data === 'string' ? { returnPathname : data } : data ;
232+ const contextRedirectUri = getRedirectUriFromContext ( ) ;
212233 const authkit = await getAuthkit ( ) ;
234+
235+ // Only inject context redirectUri if it exists and user didn't provide one
236+ if ( contextRedirectUri && ! options ?. redirectUri ) {
237+ return authkit . getSignUpUrl ( {
238+ ...options ,
239+ redirectUri : contextRedirectUri ,
240+ } ) ;
241+ }
242+
213243 return authkit . getSignUpUrl ( options ) ;
214244 } ) ;
215245
0 commit comments