Skip to content

Commit 465f8d4

Browse files
committed
Rename files and update function singatures
1 parent 1d48378 commit 465f8d4

File tree

9 files changed

+146
-182
lines changed

9 files changed

+146
-182
lines changed

examples/with-next-ssr-app-directory/app/components/home.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import { CallAPIButton } from "./callApiButton";
77
import { LinksComponent } from "./linksComponent";
88
import { SessionAuthForNextJS } from "./sessionAuthForNextJS";
99

10-
import { getSSRSession, init } from "supertokens-auth-react/nextjs/ssr";
10+
import { getSSRSession } from "supertokens-auth-react/nextjs/ssr";
1111
import { ssrConfig } from "../config/ssr";
1212

13-
init(ssrConfig());
14-
1513
export async function HomePage() {
1614
const cookiesStore = await cookies();
1715
const headersStore = await headers();
18-
const session = await getSSRSession(headersStore, cookiesStore, redirect);
16+
const session = await getSSRSession(ssrConfig(), headersStore, cookiesStore, redirect);
1917
console.log(session);
2018

2119
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { appInfo } from "./appInfo";
22
import { useRouter } from "next/navigation";
33
import { SuperTokensConfig } from "supertokens-auth-react/lib/build/types";
4-
import { init } from "supertokens-auth-react/nextjs";
4+
import { init } from "supertokens-auth-react/nextjs/middleware";
55

66
const routerInfo: { router?: ReturnType<typeof useRouter>; pathName?: string } = {};
77

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { SessionContainer } from "supertokens-node/recipe/session";
44
import { withSession } from "supertokens-node/nextjs";
55
import { ensureSuperTokensInit } from "./app/config/backend";
66
import { ssrConfig } from "./app/config/ssr";
7-
import { superTokensSessionMiddleware } from "supertokens-auth-react/nextjs";
7+
import { superTokensMiddleware } from "supertokens-auth-react/nextjs/middleware";
88

99
ensureSuperTokensInit();
1010

11-
init(ssrConfig());
12-
1311
export async function middleware(request: NextRequest & { session?: SessionContainer }) {
1412
if (request.nextUrl.pathname.startsWith("/api")) {
1513
if (request.headers.has("x-user-id")) {
@@ -40,7 +38,7 @@ export async function middleware(request: NextRequest & { session?: SessionConta
4038
});
4139
}
4240

43-
return superTokensSessionMiddleware(request, NextResponse);
41+
return superTokensMiddleware(ssrConfig(), request, NextResponse);
4442
}
4543

4644
const refreshTokenCookieName = "sRefreshToken";

lib/ts/nextjs/middleware.ts

Lines changed: 45 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { enableLogging, logDebugMessage } from "../logger";
22

3-
import type { NextRequest, NextResponse } from "./types";
4-
import type { SuperTokensConfig } from "../types";
3+
import type { NextRequest, NextResponse, SuperTokensNextjsConfig } from "./types";
54

65
const ACCESS_TOKEN_COOKIE_NAME = "sAccessToken";
76
const ACCESS_TOKEN_HEADER_NAME = "st-access-token";
@@ -10,83 +9,62 @@ const FRONT_TOKEN_HEADER_NAME = "front-token";
109
const REFRESH_TOKEN_COOKIE_NAME = "sRefreshToken";
1110
const REFRESH_TOKEN_HEADER_NAME = "st-refresh-token";
1211

13-
export default class SuperTokensNextjsEdgeAPIWrapper {
14-
static config: SuperTokensConfig;
12+
let AppInfo: SuperTokensNextjsConfig["appInfo"];
1513

16-
static getConfigOrThrow(): SuperTokensConfig {
17-
if (!SuperTokensNextjsEdgeAPIWrapper.config) {
18-
throw new Error("SuperTokens must be initialized before calling this method.");
19-
}
20-
21-
return SuperTokensNextjsEdgeAPIWrapper.config;
14+
export function superTokensMiddleware(config: SuperTokensNextjsConfig, request: NextRequest, response: NextResponse) {
15+
AppInfo = config.appInfo;
16+
if (config.enableDebugLogs) {
17+
enableLogging();
2218
}
2319

24-
static init(config: SuperTokensConfig) {
25-
if (config.enableDebugLogs) {
26-
enableLogging();
27-
}
28-
SuperTokensNextjsEdgeAPIWrapper.config = config;
20+
const url = new URL(request.url);
21+
const shouldRefresh = url.searchParams.get("forceRefresh") === "true";
22+
if (shouldRefresh) {
23+
return refreshSession(request, response);
2924
}
3025

31-
static middleware(request: NextRequest, response: NextResponse) {
32-
const url = new URL(request.url);
33-
const shouldRefresh = url.searchParams.get("forceRefresh") === "true";
34-
if (shouldRefresh) {
35-
return refreshSession(request, response);
36-
}
26+
// Save the current path so that we can use it during SSR
27+
// Used to redirect the user to the correct path after login/refresh
28+
return response.next({
29+
headers: {
30+
"x-current-path": url.pathname,
31+
},
32+
});
33+
}
3734

38-
// Save the current path so that we can use it during SSR
39-
// Used to redirect the user to the correct path after login/refresh
40-
return response.next({
41-
headers: {
42-
"x-current-path": url.pathname,
43-
},
44-
});
35+
async function refreshSession(request: NextRequest, response: NextResponse): Promise<NextResponse> {
36+
const refreshToken =
37+
request.cookies.get(REFRESH_TOKEN_COOKIE_NAME)?.value || request.headers.get(REFRESH_TOKEN_HEADER_NAME);
38+
if (!refreshToken) {
39+
logDebugMessage("Refresh token not found");
40+
return redirectToAuthPage(request, response);
4541
}
4642

47-
static async refreshSession(request: NextRequest, response: NextResponse): Promise<NextResponse> {
48-
const refreshToken =
49-
request.cookies.get(REFRESH_TOKEN_COOKIE_NAME)?.value || request.headers.get(REFRESH_TOKEN_HEADER_NAME);
50-
if (!refreshToken) {
51-
logDebugMessage("Refresh token not found");
43+
try {
44+
const tokens = await fetchNewTokens(refreshToken);
45+
if (!tokens.accessToken || !tokens.refreshToken || !tokens.frontToken) {
46+
logDebugMessage("Missing tokens from refresh response");
5247
return redirectToAuthPage(request, response);
5348
}
5449

55-
try {
56-
const tokens = await fetchNewTokens(refreshToken);
57-
if (!tokens.accessToken || !tokens.refreshToken || !tokens.frontToken) {
58-
logDebugMessage("Missing tokens from refresh response");
59-
return redirectToAuthPage(request, response);
60-
}
61-
62-
const redirectUrl = new URL(request.url);
63-
redirectUrl.searchParams.delete("forceRefresh");
64-
const finalResponse = response.redirect(redirectUrl);
65-
finalResponse.headers.set("x-current-path", redirectUrl.pathname);
66-
// @ts-expect-error TS(2345) It complains about tokens being null although we check for that in the previous if condition
67-
attachTokensToResponse(finalResponse, tokens);
68-
logDebugMessage("Attached new tokens to response");
69-
return finalResponse;
70-
} catch (err) {
71-
logDebugMessage("Error refreshing session");
72-
logDebugMessage(err as unknown as string);
73-
return redirectToAuthPage(request, response);
74-
}
50+
const redirectUrl = new URL(request.url);
51+
redirectUrl.searchParams.delete("forceRefresh");
52+
const finalResponse = response.redirect(redirectUrl);
53+
finalResponse.headers.set("x-current-path", redirectUrl.pathname);
54+
// @ts-expect-error TS(2345) It complains about tokens being null although we check for that in the previous if condition
55+
attachTokensToResponse(finalResponse, tokens);
56+
logDebugMessage("Attached new tokens to response");
57+
return finalResponse;
58+
} catch (err) {
59+
logDebugMessage("Error refreshing session");
60+
console.error(err);
61+
logDebugMessage(err as unknown as string);
62+
return redirectToAuthPage(request, response);
7563
}
7664
}
7765

78-
export const init = SuperTokensNextjsEdgeAPIWrapper.init;
79-
export const refreshSession = SuperTokensNextjsEdgeAPIWrapper.refreshSession;
80-
export const superTokensSSRMiddleware = SuperTokensNextjsEdgeAPIWrapper.middleware;
81-
82-
function getRefreshApiURL(): string {
83-
const appInfo = SuperTokensNextjsEdgeAPIWrapper.getConfigOrThrow().appInfo;
84-
return sanitizeUrl(`${appInfo.apiDomain}/${appInfo.apiBasePath}/session/refresh`);
85-
}
86-
8766
function redirectToAuthPage(request: NextRequest, response: NextResponse): NextResponse {
88-
const appInfo = SuperTokensNextjsEdgeAPIWrapper.getConfigOrThrow().appInfo;
89-
const authPagePath = appInfo.websiteBasePath || "/auth";
67+
const authPagePath = AppInfo.websiteBasePath || "/auth";
9068
const redirectUrl = new URL(authPagePath, request.url);
9169
logDebugMessage(`Redirecting to: ${redirectUrl}`);
9270
return response.redirect(redirectUrl);
@@ -95,7 +73,9 @@ function redirectToAuthPage(request: NextRequest, response: NextResponse): NextR
9573
async function fetchNewTokens(
9674
currentRefreshToken: string
9775
): Promise<{ accessToken: string | null; refreshToken: string | null; frontToken: string | null }> {
98-
const refreshResponse = await fetch(getRefreshApiURL(), {
76+
const refreshApiURL = new URL(`${AppInfo.apiBasePath}/session/refresh`, AppInfo.apiDomain);
77+
console.log(refreshApiURL.pathname);
78+
const refreshResponse = await fetch(refreshApiURL, {
9979
method: "POST",
10080
headers: {
10181
"Content-Type": "application/json",
@@ -145,9 +125,6 @@ function attachTokensToResponse(
145125
response.cookies.set(FRONT_TOKEN_COOKIE_NAME, tokens.frontToken);
146126
}
147127

148-
function sanitizeUrl(url: string) {
149-
return url.replace(/([^:]\/)\/+/g, "$1");
150-
}
151128
export function getCookieValue(header: string, name: string): string | null {
152129
const match = header.match(new RegExp(`${name}=([^;]+)`));
153130
return match ? match[1] : null;

0 commit comments

Comments
 (0)