1+ import * as jose from "jose" ;
2+
3+ import { enableLogging , logDebugMessage } from "../logger" ;
4+
15import { getAppInfoFromEnv } from "./utils" ;
26
7+ import type { AccessTokenPayload , LoadedSessionContext } from "../recipe/session/types" ;
8+
39// export class SSRConfig {
410// static config: SuperTokensConfig | null = null;
511// static setConfig(config: SuperTokensConfig) {
@@ -15,12 +21,6 @@ import { getAppInfoFromEnv } from "./utils";
1521// }
1622// }
1723//
18- //
19- //
20- import * as jose from "jose" ;
21-
22- import type { AccessTokenPayload , LoadedSessionContext } from "../recipe/session/types" ;
23- import { enableLogging , logDebugMessage } from "../logger" ;
2424
2525const COOKIE_ACCESS_TOKEN_NAME = "sAccessToken" ;
2626const HEADER_ACCESS_TOKEN_NAME = "st-access-token" ;
@@ -44,6 +44,7 @@ const AppInfo = getAppInfoFromEnv();
4444 * @returns The session context value or directly redirect the user to either the login page or the refresh API
4545 **/
4646export async function getSSRSession (
47+ headers : HeadersStore ,
4748 cookies : CookiesStore ,
4849 redirect : ( url : string ) => never
4950) : Promise < LoadedSessionContext > ;
@@ -52,8 +53,12 @@ export async function getSSRSession(
5253 * @param cookies - The cookie object that can be extracted from context.req.headers.cookie
5354 * @returns A props object with the session context value or a redirect object
5455 **/
55- export async function getSSRSession ( cookies : CookiesObject ) : Promise < GetServerSidePropsReturnValue > ;
5656export async function getSSRSession (
57+ headers : HeadersStore ,
58+ cookies : CookiesObject
59+ ) : Promise < GetServerSidePropsReturnValue > ;
60+ export async function getSSRSession (
61+ headers : HeadersStore ,
5762 cookies : CookiesObject | CookiesStore ,
5863 redirect ?: ( url : string ) => never
5964) : Promise < LoadedSessionContext | GetServerSidePropsReturnValue > {
@@ -64,38 +69,32 @@ export async function getSSRSession(
6469 }
6570 }
6671
67- const refreshToken = getCookieValue ( cookies , "sRefreshToken" ) ;
72+ const redirectTo = headers . get ( "x-current-path" ) ;
73+ const authPagePath = getAuthPagePath ( redirectTo ) ;
74+ const refreshApiPath = getRefreshApiPath ( redirectTo ) ;
75+
6876 const { state, session } = await getSSRSessionState ( cookies ) ;
6977 logDebugMessage ( `SSR Session State: ${ state } ` ) ;
70- const refreshResponse = await fetch ( sanitizeUrl ( `${ AppInfo . apiDomain } /${ AppInfo . apiBasePath } /session/refresh` ) , {
71- method : "POST" ,
72- headers : {
73- "Content-Type" : "application/json" ,
74- Cookie : `sRefreshToken=${ refreshToken } ` ,
75- } ,
76- credentials : "include" ,
77- } ) ;
78- console . log ( refreshResponse ) ;
7978
8079 switch ( state ) {
8180 case "front-token-not-found" :
8281 case "front-token-invalid" :
8382 case "access-token-invalid" :
8483 // TODO: Should we also reset the auth state(save cookies/tokens) from the frontend using a query param?
85- logDebugMessage ( `Redirecting to Auth Page: ${ getAuthPagePath ( ) } ` ) ;
84+ logDebugMessage ( `Redirecting to Auth Page: ${ authPagePath } ` ) ;
8685 if ( ! redirect ) {
87- return { redirect : { destination : getAuthPagePath ( ) , permanent : false } } ;
86+ return { redirect : { destination : authPagePath , permanent : false } } ;
8887 } else {
89- return redirect ( getAuthPagePath ( ) ) ;
88+ return redirect ( authPagePath ) ;
9089 }
9190 case "front-token-expired" :
9291 case "access-token-not-found" :
9392 case "tokens-do-not-match" :
94- logDebugMessage ( `Redirecting to refresh API: ${ getRefreshApiPath ( ) } ` ) ;
93+ logDebugMessage ( `Redirecting to refresh API: ${ refreshApiPath } ` ) ;
9594 if ( ! redirect ) {
96- return { redirect : { destination : getRefreshApiPath ( ) , permanent : false } } ;
95+ return { redirect : { destination : refreshApiPath , permanent : false } } ;
9796 } else {
98- return redirect ( getRefreshApiPath ( ) ) ;
97+ return redirect ( refreshApiPath ) ;
9998 }
10099 case "tokens-match" :
101100 logDebugMessage ( "Returning session object" ) ;
@@ -129,6 +128,8 @@ async function getSSRSessionState(
129128 if ( ! parsedFrontToken . isValid ) {
130129 return { state : "front-token-invalid" } ;
131130 }
131+
132+ logDebugMessage ( `Front token expires at: ${ new Date ( parsedFrontToken . ate ) } ` ) ;
132133 if ( parsedFrontToken . ate < Date . now ( ) ) {
133134 return { state : "front-token-expired" } ;
134135 }
@@ -160,11 +161,17 @@ async function getSSRSessionState(
160161 } ;
161162}
162163
163- const getRefreshApiPath = ( ) => {
164+ const getRefreshApiPath = ( redirectTo : string | null ) => {
165+ if ( redirectTo ) {
166+ return `${ AppInfo . apiBasePath } /session/refresh?redirectTo=${ redirectTo } ` ;
167+ }
164168 return `${ AppInfo . apiBasePath } /session/refresh` ;
165169} ;
166170
167- const getAuthPagePath = ( ) => {
171+ const getAuthPagePath = ( redirectTo : string | null ) => {
172+ if ( redirectTo ) {
173+ return `${ AppInfo . websiteBasePath } ?redirectTo=${ redirectTo } ` ;
174+ }
168175 return AppInfo . websiteBasePath ;
169176} ;
170177
@@ -213,6 +220,10 @@ type CookiesStore = {
213220 get : ( name : string ) => { value : string } ;
214221} ;
215222
223+ type HeadersStore = {
224+ get : ( name : string ) => string | null ;
225+ } ;
226+
216227function isCookiesStore ( obj : unknown ) : obj is CookiesStore {
217228 return typeof obj === "object" && obj !== null && "get" in obj && typeof ( obj as CookiesStore ) . get === "function" ;
218229}
0 commit comments