@@ -51,8 +51,8 @@ export default class SuperTokensNextjsSSRAPIWrapper {
5151 **/
5252 static async getSSRSession ( cookies : CookiesStore , redirect : ( url : string ) => never ) : Promise < LoadedSessionContext > {
5353 const redirectPath = cookies . get ( CURRENT_PATH_COOKIE_NAME ) ?. value || "/" ;
54- const authPagePath = ` ${ getAuthPagePath ( ) } ? ${ REDIRECT_PATH_PARAM_NAME } = ${ redirectPath } ` ;
55- const refreshLocation = ` ${ getRefreshLocation ( ) } ? ${ REDIRECT_PATH_PARAM_NAME } = ${ redirectPath } ` ;
54+ const authPagePath = getAuthPagePath ( redirectPath ) ;
55+ const refreshLocation = getRefreshLocation ( redirectPath ) ;
5656
5757 const { state, session } = await getSSRSessionState ( cookies ) ;
5858 logDebugMessage ( `SSR Session State: ${ state } ` ) ;
@@ -77,21 +77,20 @@ export default class SuperTokensNextjsSSRAPIWrapper {
7777 }
7878 }
7979
80- // TODO: This method is isolated atm to make it easier to test and debug
81- // In the end it should be merged in the getSSRSession function
82- static async getServerSidePropsSession ( cookies : CookiesObject ) : Promise < GetServerSidePropsReturnValue > {
83- const redirectTo = "/" ;
80+ static async getServerSidePropsSession (
81+ request : Request & { cookies : CookiesObject }
82+ ) : Promise < GetServerSidePropsReturnValue > {
83+ const requestUrl = new URL ( request . url ) ;
84+ const redirectPath = requestUrl . pathname ;
85+ const authPagePath = getAuthPagePath ( redirectPath ) ;
86+ const refreshLocation = getRefreshLocation ( redirectPath ) ;
8487
85- const authPagePath = `${ getAuthPagePath ( ) } ` ;
86- const refreshLocation = `/api/auth/session/refresh?redirectTo=${ redirectTo } ` ;
87-
88- const { state, session } = await getSSRSessionState ( cookies ) ;
88+ const { state, session } = await getSSRSessionState ( request . cookies ) ;
8989 logDebugMessage ( `SSR Session State: ${ state } ` ) ;
9090 switch ( state ) {
9191 case "front-token-not-found" :
9292 case "front-token-invalid" :
9393 case "access-token-invalid" :
94- // TODO: Should we also reset the auth state(save cookies/tokens) from the frontend using a query param?
9594 logDebugMessage ( `Redirecting to Auth Page: ${ authPagePath } ` ) ;
9695 return { redirect : { destination : authPagePath , permanent : false } } ;
9796 case "front-token-expired" :
@@ -114,16 +113,16 @@ export const init = SuperTokensNextjsSSRAPIWrapper.init;
114113export const getSSRSession = SuperTokensNextjsSSRAPIWrapper . getSSRSession ;
115114export const getServerSidePropsSession = SuperTokensNextjsSSRAPIWrapper . getServerSidePropsSession ;
116115
117- function getAuthPagePath ( ) : string {
116+ function getAuthPagePath ( redirectPath : string ) : string {
118117 const authPagePath = SuperTokensNextjsSSRAPIWrapper . getConfigOrThrow ( ) . appInfo . websiteBasePath || "/auth" ;
119- return `${ authPagePath } ?${ FORCE_LOGOUT_PATH_PARAM_NAME } =true` ;
118+ return `${ authPagePath } ?${ FORCE_LOGOUT_PATH_PARAM_NAME } =true& ${ REDIRECT_PATH_PARAM_NAME } = ${ redirectPath } ` ;
120119}
121120
122- function getRefreshLocation ( ) : string {
121+ function getRefreshLocation ( redirectPath : string ) : string {
123122 // The backend api routes defined in appInfo might be different from what we use here.
124123 // This refresh route will be made available only by including the node handler in the next.js middleware
125124 // If someone uses nextjs with SSR, and a separate backend api server, the app info data will be invalid here.
126- return " /api/auth/session/refresh" ;
125+ return ` /api/auth/session/refresh? ${ REDIRECT_PATH_PARAM_NAME } = ${ redirectPath } ` ;
127126}
128127
129128async function getSSRSessionState (
0 commit comments