Skip to content

Commit 9796608

Browse files
committed
Update types and clean things up
1 parent d99eea4 commit 9796608

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

lib/ts/recipe/session/framework/nextjs.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,12 @@ import jose from "jose";
33
import SuperTokens from "../../../superTokens";
44

55
import type { AccessTokenPayload, LoadedSessionContext } from "../types";
6+
import { CookiesStore, CookiesObject, GetServerSidePropsReturnValue, isCookiesStore } from "./types";
67

78
const COOKIE_ACCESS_TOKEN_NAME = "sAccessToken";
89
const HEADER_ACCESS_TOKEN_NAME = "st-access-token";
910
const 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-
3012
type 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

132114
function parseFrontToken(frontToken: string): AccessTokenPayload {
133115
return JSON.parse(decodeURIComponent(escape(atob(frontToken))));
134116
}
135117

136118
async 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
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { LoadedSessionContext } from "../types";
2+
3+
export type CookiesStore = {
4+
get: (name: string) => { value: string };
5+
};
6+
7+
export function isCookiesStore(obj: unknown): obj is CookiesStore {
8+
return typeof obj === "object" && obj !== null && "get" in obj && typeof (obj as CookiesStore).get === "function";
9+
}
10+
11+
export type CookiesObject = Record<string, string>;
12+
13+
export type GetServerSidePropsRedirect = {
14+
redirect: { destination: string; permanent: boolean };
15+
};
16+
17+
export type GetServerSidePropsReturnValue =
18+
| {
19+
props: { session: LoadedSessionContext };
20+
}
21+
| GetServerSidePropsRedirect;

0 commit comments

Comments
 (0)