Skip to content

Commit 531e5e9

Browse files
committed
Code review fixes
1 parent 7c23d35 commit 531e5e9

File tree

8 files changed

+51
-47
lines changed

8 files changed

+51
-47
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const appInfo = {
22
appName: "SuperTokens Next.js demo app",
3-
apiDomain: "http://localhost:3000",
4-
websiteDomain: "http://localhost:3000",
3+
apiDomain: "http://localhost:3333",
4+
websiteDomain: "http://localhost:3333",
55
apiBasePath: "/api/auth",
66
websiteBasePath: "/auth",
77
};

examples/with-next-ssr-app-directory/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function superTokensMiddleware(
6464
request.nextUrl.pathname.startsWith("/auth") &&
6565
request.nextUrl.searchParams.get(FORCE_LOGOUT_PATH_PARAM_NAME) === "true"
6666
) {
67-
return revokeSession(config);
67+
return revokeSession(config, request);
6868
}
6969

7070
// Save the current path so that we can use it during SSR

examples/with-next-ssr-app-directory/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"dev": "next dev",
6+
"dev": "next dev -p 3333",
77
"build": "next build",
88
"start": "next start",
99
"lint": "next lint"

lib/ts/nextjs/middleware.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,39 +85,42 @@ export async function refreshSession(config: SuperTokensNextjsConfig, request: R
8585
}
8686
}
8787

88-
export async function revokeSession(config: SuperTokensNextjsConfig): Promise<Response | void> {
88+
export async function revokeSession(config: SuperTokensNextjsConfig, request: Request): Promise<Response | void> {
8989
AppInfo = config.appInfo;
9090
if (config.enableDebugLogs) {
9191
enableLogging();
9292
}
93-
const signOutURL = new URL(`${AppInfo.apiBasePath}/signout`, AppInfo.apiDomain);
93+
94+
const response = new Response(null, {});
95+
9496
try {
95-
const signOutResponse = await fetch(signOutURL, {
97+
const accessToken =
98+
getCookie(request, ACCESS_TOKEN_COOKIE_NAME) || request.headers.get(ACCESS_TOKEN_HEADER_NAME);
99+
if (!accessToken) {
100+
throw new Error("No access token found in the request");
101+
}
102+
const signOutURL = new URL(`${AppInfo.apiBasePath}/signout`, AppInfo.apiDomain);
103+
await fetch(signOutURL, {
96104
method: "POST",
97105
headers: {
98106
"Content-Type": "application/json",
107+
[ACCESS_TOKEN_HEADER_NAME]: accessToken,
108+
Cookie: `${ACCESS_TOKEN_COOKIE_NAME}=${accessToken}`,
99109
},
100110
credentials: "include",
101111
});
102-
103-
if (signOutResponse.status !== 200) {
104-
return;
105-
}
106-
107-
const response = new Response(null, {});
108-
response.headers.set("x-middleware-next", "1");
109-
// TODO: Delete only the cookies that are auth related
110-
response.headers.delete("set-cookie");
111-
response.headers.delete(ACCESS_TOKEN_HEADER_NAME);
112-
response.headers.delete(REFRESH_TOKEN_HEADER_NAME);
113-
response.headers.delete(FRONT_TOKEN_HEADER_NAME);
114-
response.headers.delete(ANTI_CSRF_TOKEN_HEADER_NAME);
115-
return response;
116112
} catch (err) {
117-
logDebugMessage("Error revoking session");
113+
logDebugMessage("Error during the sign out attempt");
118114
logDebugMessage(err as unknown as string);
119-
return;
120115
}
116+
117+
response.headers.set("x-middleware-next", "1");
118+
response.headers.delete("set-cookie");
119+
response.headers.delete(ACCESS_TOKEN_HEADER_NAME);
120+
response.headers.delete(REFRESH_TOKEN_HEADER_NAME);
121+
response.headers.delete(FRONT_TOKEN_HEADER_NAME);
122+
response.headers.delete(ANTI_CSRF_TOKEN_HEADER_NAME);
123+
return response;
121124
}
122125

123126
function redirectToAuthPage(request: Request): Response {
@@ -144,7 +147,8 @@ async function fetchNewTokens(currentRefreshToken: string): Promise<{
144147
method: "POST",
145148
headers: {
146149
"Content-Type": "application/json",
147-
Cookie: `sRefreshToken=${currentRefreshToken}`,
150+
[REFRESH_TOKEN_HEADER_NAME]: currentRefreshToken,
151+
Cookie: `${REFRESH_TOKEN_COOKIE_NAME}=${currentRefreshToken}`,
148152
},
149153
credentials: "include",
150154
});

lib/ts/nextjs/ssr.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
114113
export const getSSRSession = SuperTokensNextjsSSRAPIWrapper.getSSRSession;
115114
export 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

129128
async function getSSRSessionState(

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
},
9292
"dependencies": {
9393
"intl-tel-input": "^17.0.19",
94-
"jose": "^5.9.6",
94+
"jose": "^6.0.8",
9595
"prop-types": "*",
9696
"react-qr-code": "^2.0.12",
9797
"supertokens-js-override": "^0.0.4"

rollup.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default [
2020
session: "lib/ts/recipe/session/index.ts",
2121
nextjsmiddleware: "lib/ts/nextjs/middleware.ts",
2222
nextjsssr: "lib/ts/nextjs/ssr.ts",
23+
nextjspages: "lib/ts/nextjs/pages.ts",
2324
sessionprebuiltui: "lib/ts/recipe/session/prebuiltui.tsx",
2425
emailverification: "lib/ts/recipe/emailverification/index.ts",
2526
emailverificationprebuiltui: "lib/ts/recipe/emailverification/prebuiltui.tsx",

0 commit comments

Comments
 (0)