@@ -3,30 +3,12 @@ import jose from "jose";
33import SuperTokens from "../../../superTokens" ;
44
55import type { AccessTokenPayload , LoadedSessionContext } from "../types" ;
6+ import { CookiesStore , CookiesObject , GetServerSidePropsReturnValue , isCookiesStore } from "./types" ;
67
78const COOKIE_ACCESS_TOKEN_NAME = "sAccessToken" ;
89const HEADER_ACCESS_TOKEN_NAME = "st-access-token" ;
910const FRONT_TOKEN_NAME = "sFrontToken" ;
1011
11- type CookiesStore = {
12- get : ( name : string ) => { value : string } ;
13- } ;
14-
15- function isCookiesStore ( obj : unknown ) : obj is CookiesStore {
16- return typeof obj === "object" && obj !== null && "get" in obj && typeof ( obj as CookiesStore ) . get === "function" ;
17- }
18-
19- type CookiesObject = Record < string , string > ;
20-
21- type GetServerSidePropsRedirect = {
22- redirect : { destination : string ; permanent : boolean } ;
23- } ;
24- type GetServerSidePropsReturnValue =
25- | {
26- props : { session : LoadedSessionContext } ;
27- }
28- | GetServerSidePropsRedirect ;
29-
3012type SSRSessionState =
3113 | "front-token-not-found"
3214 | "front-token-expired"
@@ -53,17 +35,17 @@ export async function getSSRSession(
5335 switch ( state ) {
5436 case "front-token-not-found" :
5537 if ( ! redirect ) {
56- return { redirect : { destination : getWebsiteBasePath ( ) , permanent : false } } ;
38+ return { redirect : { destination : getAuthPagePath ( ) , permanent : false } } ;
5739 } else {
58- return redirect ( getWebsiteBasePath ( ) ) ;
40+ return redirect ( getAuthPagePath ( ) ) ;
5941 }
6042 case "front-token-expired" :
6143 case "access-token-not-found" :
6244 case "tokens-do-not-match" :
6345 if ( ! redirect ) {
64- return { redirect : { destination : ` ${ getApiBasePath ( ) } /refresh` , permanent : false } } ;
46+ return { redirect : { destination : getRefreshApiPath ( ) , permanent : false } } ;
6547 } else {
66- return redirect ( ` ${ getApiBasePath ( ) } /refresh` ) ;
48+ return redirect ( getRefreshApiPath ( ) ) ;
6749 }
6850 case "tokens-match" :
6951 if ( ! redirect ) {
@@ -121,20 +103,20 @@ async function getSSRSessionState(
121103 } ;
122104}
123105
124- const getApiBasePath = ( ) => {
125- return SuperTokens . getInstanceOrThrow ( ) . appInfo . apiBasePath . getAsStringDangerous ( ) ;
106+ const getRefreshApiPath = ( ) => {
107+ return ` ${ SuperTokens . getInstanceOrThrow ( ) . appInfo . apiBasePath . getAsStringDangerous ( ) } /refresh` ;
126108} ;
127109
128- const getWebsiteBasePath = ( ) => {
129- return SuperTokens . getInstanceOrThrow ( ) . appInfo . websiteBasePath . getAsStringDangerous ( ) ;
110+ const getAuthPagePath = ( ) => {
111+ return ` ${ SuperTokens . getInstanceOrThrow ( ) . appInfo . websiteBasePath . getAsStringDangerous ( ) } /` ;
130112} ;
131113
132114function parseFrontToken ( frontToken : string ) : AccessTokenPayload {
133115 return JSON . parse ( decodeURIComponent ( escape ( atob ( frontToken ) ) ) ) ;
134116}
135117
136118async function parseAccessToken ( token : string ) : Promise < AccessTokenPayload > {
137- const JWKS = jose . createRemoteJWKSet ( new URL ( `${ getApiBasePath ( ) } /authjwt/jwks.json` ) ) ;
119+ const JWKS = jose . createRemoteJWKSet ( new URL ( `${ getRefreshApiPath ( ) } /authjwt/jwks.json` ) ) ;
138120 const { payload } = await jose . jwtVerify ( token , JWKS ) ;
139121 return payload ;
140122}
0 commit comments