@civic/auth 0.10.0-beta.0 → 0.10.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +1 -0
- package/dist/browser/storage.d.ts +1 -0
- package/dist/browser/storage.d.ts.map +1 -1
- package/dist/browser/storage.js +3 -0
- package/dist/browser/storage.js.map +1 -1
- package/dist/lib/logger.d.ts +2 -0
- package/dist/lib/logger.d.ts.map +1 -1
- package/dist/lib/logger.js +2 -0
- package/dist/lib/logger.js.map +1 -1
- package/dist/nextjs/config.d.ts +35 -3
- package/dist/nextjs/config.d.ts.map +1 -1
- package/dist/nextjs/config.js +76 -25
- package/dist/nextjs/config.js.map +1 -1
- package/dist/nextjs/cookies.d.ts +2 -1
- package/dist/nextjs/cookies.d.ts.map +1 -1
- package/dist/nextjs/cookies.js +35 -5
- package/dist/nextjs/cookies.js.map +1 -1
- package/dist/nextjs/hooks/useInitialAuthConfig.d.ts.map +1 -1
- package/dist/nextjs/hooks/useInitialAuthConfig.js +36 -13
- package/dist/nextjs/hooks/useInitialAuthConfig.js.map +1 -1
- package/dist/nextjs/middleware.d.ts +2 -1
- package/dist/nextjs/middleware.d.ts.map +1 -1
- package/dist/nextjs/middleware.js +59 -59
- package/dist/nextjs/middleware.js.map +1 -1
- package/dist/nextjs/providers/NextAuthProvider.d.ts.map +1 -1
- package/dist/nextjs/providers/NextAuthProvider.js +8 -5
- package/dist/nextjs/providers/NextAuthProvider.js.map +1 -1
- package/dist/nextjs/providers/NextAuthProviderClient.d.ts +3 -2
- package/dist/nextjs/providers/NextAuthProviderClient.d.ts.map +1 -1
- package/dist/nextjs/providers/NextAuthProviderClient.js +3 -3
- package/dist/nextjs/providers/NextAuthProviderClient.js.map +1 -1
- package/dist/nextjs/providers/ServerUserContext.d.ts +6 -1
- package/dist/nextjs/providers/ServerUserContext.d.ts.map +1 -1
- package/dist/nextjs/providers/ServerUserContext.js.map +1 -1
- package/dist/nextjs/routeHandler.d.ts +3 -0
- package/dist/nextjs/routeHandler.d.ts.map +1 -1
- package/dist/nextjs/routeHandler.js +16 -20
- package/dist/nextjs/routeHandler.js.map +1 -1
- package/dist/nextjs/utils.d.ts +30 -6
- package/dist/nextjs/utils.d.ts.map +1 -1
- package/dist/nextjs/utils.js +159 -35
- package/dist/nextjs/utils.js.map +1 -1
- package/dist/reactjs/core/GlobalAuthManager.d.ts +6 -2
- package/dist/reactjs/core/GlobalAuthManager.d.ts.map +1 -1
- package/dist/reactjs/core/GlobalAuthManager.js +26 -7
- package/dist/reactjs/core/GlobalAuthManager.js.map +1 -1
- package/dist/reactjs/hooks/useUser.d.ts.map +1 -1
- package/dist/reactjs/hooks/useUser.js +83 -130
- package/dist/reactjs/hooks/useUser.js.map +1 -1
- package/dist/server/ServerAuthenticationResolver.d.ts +3 -2
- package/dist/server/ServerAuthenticationResolver.d.ts.map +1 -1
- package/dist/server/ServerAuthenticationResolver.js +23 -6
- package/dist/server/ServerAuthenticationResolver.js.map +1 -1
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/login.d.ts +2 -1
- package/dist/server/login.d.ts.map +1 -1
- package/dist/server/login.js.map +1 -1
- package/dist/server/session.d.ts +4 -3
- package/dist/server/session.d.ts.map +1 -1
- package/dist/server/session.js.map +1 -1
- package/dist/server/users.d.ts +4 -3
- package/dist/server/users.d.ts.map +1 -1
- package/dist/server/users.js.map +1 -1
- package/dist/services/types.d.ts +1 -1
- package/dist/services/types.d.ts.map +1 -1
- package/dist/services/types.js.map +1 -1
- package/dist/shared/hooks/index.d.ts +0 -1
- package/dist/shared/hooks/index.d.ts.map +1 -1
- package/dist/shared/hooks/index.js +0 -1
- package/dist/shared/hooks/index.js.map +1 -1
- package/dist/shared/lib/BrowserAuthenticationRefresher.d.ts.map +1 -1
- package/dist/shared/lib/BrowserAuthenticationRefresher.js +14 -6
- package/dist/shared/lib/BrowserAuthenticationRefresher.js.map +1 -1
- package/dist/shared/lib/BrowserCookieStorage.d.ts.map +1 -1
- package/dist/shared/lib/BrowserCookieStorage.js +5 -1
- package/dist/shared/lib/BrowserCookieStorage.js.map +1 -1
- package/dist/shared/lib/GenericAuthenticationRefresher.d.ts +1 -0
- package/dist/shared/lib/GenericAuthenticationRefresher.d.ts.map +1 -1
- package/dist/shared/lib/GenericAuthenticationRefresher.js +2 -0
- package/dist/shared/lib/GenericAuthenticationRefresher.js.map +1 -1
- package/dist/shared/lib/UserSession.d.ts +4 -3
- package/dist/shared/lib/UserSession.d.ts.map +1 -1
- package/dist/shared/lib/UserSession.js +4 -0
- package/dist/shared/lib/UserSession.js.map +1 -1
- package/dist/shared/lib/cookieConfig.d.ts +1 -1
- package/dist/shared/lib/cookieConfig.d.ts.map +1 -1
- package/dist/shared/lib/cookieConfig.js +2 -1
- package/dist/shared/lib/cookieConfig.js.map +1 -1
- package/dist/shared/lib/cookieUtils.d.ts +6 -0
- package/dist/shared/lib/cookieUtils.d.ts.map +1 -0
- package/dist/shared/lib/cookieUtils.js +21 -0
- package/dist/shared/lib/cookieUtils.js.map +1 -0
- package/dist/shared/lib/session.d.ts +2 -1
- package/dist/shared/lib/session.d.ts.map +1 -1
- package/dist/shared/lib/session.js +11 -2
- package/dist/shared/lib/session.js.map +1 -1
- package/dist/shared/lib/util.d.ts +2 -2
- package/dist/shared/lib/util.d.ts.map +1 -1
- package/dist/shared/lib/util.js +4 -4
- package/dist/shared/lib/util.js.map +1 -1
- package/dist/shared/version.d.ts +1 -1
- package/dist/shared/version.d.ts.map +1 -1
- package/dist/shared/version.js +1 -1
- package/dist/shared/version.js.map +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/vanillajs/auth/BackendAuthenticationRefresher.d.ts +4 -3
- package/dist/vanillajs/auth/BackendAuthenticationRefresher.d.ts.map +1 -1
- package/dist/vanillajs/auth/BackendAuthenticationRefresher.js +42 -21
- package/dist/vanillajs/auth/BackendAuthenticationRefresher.js.map +1 -1
- package/dist/vanillajs/auth/CivicAuth.d.ts.map +1 -1
- package/dist/vanillajs/auth/CivicAuth.js +2 -2
- package/dist/vanillajs/auth/CivicAuth.js.map +1 -1
- package/dist/vanillajs/auth/SessionManager.d.ts.map +1 -1
- package/dist/vanillajs/auth/SessionManager.js +23 -16
- package/dist/vanillajs/auth/SessionManager.js.map +1 -1
- package/dist/vanillajs/auth/TokenRefresher.d.ts +3 -0
- package/dist/vanillajs/auth/TokenRefresher.d.ts.map +1 -1
- package/dist/vanillajs/auth/TokenRefresher.js +27 -4
- package/dist/vanillajs/auth/TokenRefresher.js.map +1 -1
- package/dist/vanillajs/auth/config/ConfigProcessor.d.ts.map +1 -1
- package/dist/vanillajs/auth/config/ConfigProcessor.js +3 -1
- package/dist/vanillajs/auth/config/ConfigProcessor.js.map +1 -1
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.d.ts.map +1 -1
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.js +18 -0
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.js.map +1 -1
- package/dist/vanillajs/auth/types/AuthTypes.d.ts +3 -0
- package/dist/vanillajs/auth/types/AuthTypes.d.ts.map +1 -1
- package/dist/vanillajs/auth/types/AuthTypes.js.map +1 -1
- package/package.json +1 -1
- package/dist/nextjs/hooks/useRefresh.d.ts +0 -5
- package/dist/nextjs/hooks/useRefresh.d.ts.map +0 -1
- package/dist/nextjs/hooks/useRefresh.js +0 -57
- package/dist/nextjs/hooks/useRefresh.js.map +0 -1
- package/dist/shared/hooks/useRefresh.d.ts +0 -6
- package/dist/shared/hooks/useRefresh.d.ts.map +0 -1
- package/dist/shared/hooks/useRefresh.js +0 -47
- package/dist/shared/hooks/useRefresh.js.map +0 -1
|
@@ -7,6 +7,7 @@ import { clearAuthCookies, NextjsCookieStorage } from "../nextjs/cookies.js";
|
|
|
7
7
|
import { CodeVerifier, UserStorage } from "../shared/lib/types.js";
|
|
8
8
|
import { revalidatePath } from "next/cache.js";
|
|
9
9
|
import { NextResponse } from "next/server.js";
|
|
10
|
+
import { prependBasePath, redirectWithBasePath } from "./utils.js";
|
|
10
11
|
const logger = loggers.nextjs.handlers.auth;
|
|
11
12
|
class AuthError extends Error {
|
|
12
13
|
status;
|
|
@@ -105,13 +106,6 @@ async function handleRefresh(request, config) {
|
|
|
105
106
|
const { civicAuth } = createCivicAuth(request, resolvedConfigs);
|
|
106
107
|
await civicAuth.refreshTokens();
|
|
107
108
|
logger.info("[REFRESH_HANDLER] Tokens refreshed successfully");
|
|
108
|
-
const targetUrl = request.nextUrl.searchParams.get("targetUrl");
|
|
109
|
-
if (targetUrl) {
|
|
110
|
-
// Success: clear the refresh attempt tracking cookie and redirect to target
|
|
111
|
-
const response = NextResponse.redirect(targetUrl);
|
|
112
|
-
response.cookies.delete("_civic_last_refresh");
|
|
113
|
-
return response;
|
|
114
|
-
}
|
|
115
109
|
return NextResponse.json({
|
|
116
110
|
status: "success",
|
|
117
111
|
message: "Tokens refreshed",
|
|
@@ -119,10 +113,6 @@ async function handleRefresh(request, config) {
|
|
|
119
113
|
}
|
|
120
114
|
catch (error) {
|
|
121
115
|
logger.error("[REFRESH_HANDLER] Token refresh error:", error);
|
|
122
|
-
const targetUrl = request.nextUrl.searchParams.get("targetUrl");
|
|
123
|
-
if (targetUrl) {
|
|
124
|
-
return NextResponse.redirect(targetUrl);
|
|
125
|
-
}
|
|
126
116
|
return NextResponse.json({ error: "Token refresh failed" }, { status: 500 });
|
|
127
117
|
}
|
|
128
118
|
}
|
|
@@ -156,10 +146,10 @@ async function handleCallback(request, config) {
|
|
|
156
146
|
req: handleCallbackRequest,
|
|
157
147
|
}, {
|
|
158
148
|
// Pass the properly resolved frontendUrl
|
|
159
|
-
frontendUrl: frontendUrl,
|
|
149
|
+
frontendUrl: prependBasePath(frontendUrl, config.basePath || ""),
|
|
160
150
|
});
|
|
161
151
|
if (result.redirectTo) {
|
|
162
|
-
return
|
|
152
|
+
return redirectWithBasePath(config, CivicAuth.toAbsoluteUrl(urlDetectionRequest, result.redirectTo, appUrl));
|
|
163
153
|
}
|
|
164
154
|
if (result.content) {
|
|
165
155
|
// Handle both string content and object content
|
|
@@ -229,7 +219,7 @@ export async function handleLogout(request, config) {
|
|
|
229
219
|
state: state || undefined,
|
|
230
220
|
});
|
|
231
221
|
try {
|
|
232
|
-
await
|
|
222
|
+
await clearAuthCookies(resolvedConfigs);
|
|
233
223
|
// Revalidate current path to update authentication state in server components
|
|
234
224
|
await revalidateUrlPath(request.url);
|
|
235
225
|
}
|
|
@@ -247,7 +237,7 @@ export async function handleLogout(request, config) {
|
|
|
247
237
|
catch (error) {
|
|
248
238
|
logger.error("[LOGOUT_HANDLER] Logout error:", error);
|
|
249
239
|
// If logout URL generation fails, clear tokens and redirect to home
|
|
250
|
-
await clearAuthCookies();
|
|
240
|
+
await clearAuthCookies(resolvedConfigs);
|
|
251
241
|
const fallbackUrl = clientLogoutRedirectUrl || resolvedConfigs.logoutCallbackUrl;
|
|
252
242
|
const finalFallbackUrl = CivicAuth.toAbsoluteUrl(urlDetectionRequest, fallbackUrl, appUrl);
|
|
253
243
|
return NextResponse.redirect(finalFallbackUrl);
|
|
@@ -258,7 +248,7 @@ export async function handleLogoutCallback(request, config) {
|
|
|
258
248
|
try {
|
|
259
249
|
logger.info("[LOGOUT_CALLBACK_HANDLER] Backend logout callback endpoint called");
|
|
260
250
|
// Clear authentication cookies
|
|
261
|
-
await clearAuthCookies();
|
|
251
|
+
await clearAuthCookies(resolvedConfigs);
|
|
262
252
|
// Get framework-agnostic request and create CivicAuth instance
|
|
263
253
|
const urlDetectionRequest = toUrlDetectionRequest(request);
|
|
264
254
|
const { civicAuth } = createCivicAuth(request, resolvedConfigs);
|
|
@@ -281,13 +271,13 @@ export async function handleLogoutCallback(request, config) {
|
|
|
281
271
|
});
|
|
282
272
|
// Revalidate the redirect path to update authentication state in server components
|
|
283
273
|
await revalidateUrlPath(redirectUrl);
|
|
284
|
-
return
|
|
274
|
+
return redirectWithBasePath(config, redirectUrl);
|
|
285
275
|
}
|
|
286
276
|
catch (error) {
|
|
287
277
|
logger.error("[LOGOUT_CALLBACK_HANDLER] Logout callback error:", error);
|
|
288
278
|
const urlDetectionRequest = toUrlDetectionRequest(request);
|
|
289
279
|
const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);
|
|
290
|
-
return
|
|
280
|
+
return redirectWithBasePath(config, CivicAuth.toAbsoluteUrl(urlDetectionRequest, resolvedConfigs.logoutCallbackUrl, appUrl));
|
|
291
281
|
}
|
|
292
282
|
}
|
|
293
283
|
/**
|
|
@@ -300,6 +290,9 @@ export async function handleLogoutCallback(request, config) {
|
|
|
300
290
|
* export const GET = handler({
|
|
301
291
|
* // optional config overrides
|
|
302
292
|
* })
|
|
293
|
+
* export const POST = handler({
|
|
294
|
+
* // optional config overrides
|
|
295
|
+
* })
|
|
303
296
|
* ```
|
|
304
297
|
*/
|
|
305
298
|
export const handler = (authConfig = {}) => async (request) => {
|
|
@@ -331,7 +324,7 @@ export const handler = (authConfig = {}) => async (request) => {
|
|
|
331
324
|
const status = error instanceof AuthError ? error.status : 500;
|
|
332
325
|
const message = error instanceof Error ? error.message : "Authentication failed";
|
|
333
326
|
const response = NextResponse.json({ error: message }, { status });
|
|
334
|
-
await clearAuthCookies();
|
|
327
|
+
await clearAuthCookies(config);
|
|
335
328
|
return response;
|
|
336
329
|
}
|
|
337
330
|
};
|
|
@@ -345,7 +338,10 @@ async function handleUser(request, config) {
|
|
|
345
338
|
const { civicAuth } = createCivicAuth(request, resolvedConfigs);
|
|
346
339
|
const isLoggedIn = await civicAuth.isLoggedIn();
|
|
347
340
|
if (!isLoggedIn) {
|
|
348
|
-
|
|
341
|
+
const statusCode = request.nextUrl.searchParams.get("optimisticRehydration")
|
|
342
|
+
? 202
|
|
343
|
+
: 401;
|
|
344
|
+
return NextResponse.json({ error: "Not authenticated" }, { status: statusCode });
|
|
349
345
|
}
|
|
350
346
|
const user = await civicAuth.getUser();
|
|
351
347
|
return NextResponse.json({ user });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routeHandler.js","sourceRoot":"","sources":["../../src/nextjs/routeHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA4B,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;AAE5C,MAAM,SAAU,SAAQ,KAAK;IAGT;IAFlB,YACE,OAAe,EACC,SAAiB,GAAG;QAEpC,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,WAAM,GAAN,MAAM,CAAc;QAGpC,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IAC1B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,qBAAqB,GAAG,CAAC,OAAoB,EAAuB,EAAE,CAAC,CAAC;IAC5E,GAAG,EAAE,OAAO,CAAC,GAAG;IAChB,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;IACtD,YAAY,EAAE;QACZ,GAAG,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,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AACH,KAAK,UAAU,UAAU,CACvB,OAAoB,EACpB,MAAkB;IAElB,MAAM,eAAe,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAElD,IAAI,CAAC;QACH,MAAM,EAAE,SAAS,EAAE,GAAG,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QAEhE,MAAM,UAAU,GAAG,MAAM,SAAS,CAAC,UAAU,EAAE,CAAC;QAEhD,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO,YAAY,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QAC5E,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,SAAS,CAAC,OAAO,EAAE,CAAC;QAEvC,OAAO,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IACrC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAC,qCAAqC,EAAE,KAAK,CAAC,CAAC;QAC3D,OAAO,YAAY,CAAC,IAAI,CACtB,EAAE,KAAK,EAAE,uBAAuB,EAAE,EAClC,EAAE,MAAM,EAAE,GAAG,EAAE,CAChB,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import { CivicAuth, type UrlDetectionRequest } from \"@civic/auth/server\";\nimport { LOGOUT_SUCCESS_TEXT } from \"@/constants.js\";\nimport { loggers } from \"@/lib/logger.js\";\nimport { displayModeFromState } from \"@/lib/oauth.js\";\nimport type { AuthConfig } from \"@/nextjs/config.js\";\nimport { resolveAuthConfig } from \"@/nextjs/config.js\";\nimport { clearAuthCookies, NextjsCookieStorage } from \"@/nextjs/cookies.js\";\nimport { CodeVerifier, UserStorage } from \"@/shared/lib/types.js\";\nimport { revalidatePath } from \"next/cache.js\";\nimport type { NextRequest } from \"next/server.js\";\nimport { NextResponse } from \"next/server.js\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nclass AuthError extends Error {\n constructor(\n message: string,\n public readonly status: number = 401,\n ) {\n super(message);\n this.name = \"AuthError\";\n }\n}\n\n/**\n * Helper to convert NextRequest to UrlDetectionRequest for framework-agnostic URL handling\n */\nconst toUrlDetectionRequest = (request: NextRequest): UrlDetectionRequest => ({\n url: request.url,\n headers: Object.fromEntries(request.headers.entries()),\n searchParams: {\n get: (name: string) => request.nextUrl.searchParams.get(name),\n },\n cookies: {\n get: (name: string) => request.cookies.get(name),\n },\n});\n\n/**\n * Helper to create CivicAuth instance for a request\n * Now handles appUrl detection for proxy environments\n */\nconst createCivicAuth = (request: NextRequest, config: AuthConfig) => {\n const resolvedConfig = resolveAuthConfig(config);\n const cookieStorage = new NextjsCookieStorage({\n ...resolvedConfig.cookies?.tokens,\n [UserStorage.USER]: resolvedConfig.cookies?.user,\n });\n\n // Convert to framework-agnostic request format\n const urlDetectionRequest = toUrlDetectionRequest(request);\n\n // Get appUrl from client (for proxy environments)\n const clientAppUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n\n // Use baseUrl from config, then client appUrl, then request origin\n // This matches the main branch priority: config > client > request\n const appUrl =\n resolvedConfig.baseUrl ||\n clientAppUrl ||\n new URL(urlDetectionRequest.url).origin;\n\n // Build absolute URLs using detected appUrl or request origin\n const absoluteCallbackUrl = resolvedConfig.callbackUrl.startsWith(\"http\")\n ? resolvedConfig.callbackUrl\n : CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n resolvedConfig.callbackUrl,\n appUrl,\n );\n const absoluteLogoutCallbackUrl = resolvedConfig.logoutCallbackUrl.startsWith(\n \"http\",\n )\n ? resolvedConfig.logoutCallbackUrl\n : CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n resolvedConfig.logoutCallbackUrl,\n appUrl,\n );\n\n const civicAuth = new CivicAuth(cookieStorage, {\n clientId: resolvedConfig.clientId,\n redirectUrl: absoluteCallbackUrl,\n oauthServer: resolvedConfig.oauthServer,\n postLogoutRedirectUrl: absoluteLogoutCallbackUrl,\n loginSuccessUrl: request.url,\n });\n\n return {\n civicAuth,\n cookieStorage,\n appUrl, // Return appUrl for use in other functions\n urlDetectionRequest, // Return for use in handlers\n };\n};\n\n/**\n * Login handler - backend OAuth login initiation endpoint\n * Uses CivicAuth.buildLoginUrl()\n */\nasync function handleLogin(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n const frontendState = request.nextUrl.searchParams.get(\"state\");\n\n // Store appUrl in cookie if provided as query parameter\n const appUrlFromQuery = request.nextUrl.searchParams.get(\"appUrl\");\n if (appUrlFromQuery) {\n const cookieStorage = new NextjsCookieStorage(\n resolvedConfigs.cookies?.tokens ?? {},\n );\n await cookieStorage.set(CodeVerifier.APP_URL, appUrlFromQuery);\n }\n\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n const url = await civicAuth.buildLoginUrl({\n state: frontendState || undefined,\n });\n\n logger.info(\"[LOGIN_HANDLER] Redirecting to OAuth login URL\", {\n loginUrl: url.toString(),\n });\n\n return NextResponse.redirect(url.toString());\n } catch (error) {\n logger.error(\"[LOGIN_HANDLER] Backend login error:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n \"/?error=login_failed\",\n appUrl,\n ),\n );\n }\n}\n\nasync function handleRefresh(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n await civicAuth.refreshTokens();\n\n logger.info(\"[REFRESH_HANDLER] Tokens refreshed successfully\");\n\n const targetUrl = request.nextUrl.searchParams.get(\"targetUrl\");\n if (targetUrl) {\n // Success: clear the refresh attempt tracking cookie and redirect to target\n const response = NextResponse.redirect(targetUrl);\n response.cookies.delete(\"_civic_last_refresh\");\n return response;\n }\n\n return NextResponse.json({\n status: \"success\",\n message: \"Tokens refreshed\",\n });\n } catch (error) {\n logger.error(\"[REFRESH_HANDLER] Token refresh error:\", error);\n const targetUrl = request.nextUrl.searchParams.get(\"targetUrl\");\n if (targetUrl) {\n return NextResponse.redirect(targetUrl);\n }\n return NextResponse.json(\n { error: \"Token refresh failed\" },\n { status: 500 },\n );\n }\n}\n\nasync function handleCallback(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n const code = request.nextUrl.searchParams.get(\"code\");\n const state = request.nextUrl.searchParams.get(\"state\");\n const error = request.nextUrl.searchParams.get(\"error\");\n\n if (error) {\n logger.error(\"OAuth error in callback:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n \"/?error=oauth_error\",\n appUrl,\n ),\n );\n }\n\n if (!code || !state) throw new AuthError(\"Bad parameters\", 400);\n\n try {\n const { civicAuth, appUrl, urlDetectionRequest } = createCivicAuth(\n request,\n resolvedConfigs,\n );\n\n // Convert NextRequest to the format expected by handleCallback\n const handleCallbackRequest = {\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url.toString(),\n };\n\n // Get loginSuccessUrl with proper baseUrl handling\n const loginSuccessUrl = CivicAuth.getLoginSuccessUrl(\n urlDetectionRequest,\n appUrl,\n );\n const frontendUrl =\n loginSuccessUrl || resolvedConfigs.loginSuccessUrl || \"/\";\n\n // Use CivicAuth's smart callback handler\n const result = await civicAuth.handleCallback(\n {\n code,\n state,\n req: handleCallbackRequest,\n },\n {\n // Pass the properly resolved frontendUrl\n frontendUrl: frontendUrl,\n },\n );\n\n if (result.redirectTo) {\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(urlDetectionRequest, result.redirectTo, appUrl),\n );\n }\n\n if (result.content) {\n // Handle both string content and object content\n if (typeof result.content === \"string\") {\n return new NextResponse(result.content, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/html\",\n },\n });\n } else {\n // Object content (JSON response)\n return NextResponse.json(result.content);\n }\n }\n\n // Fallback redirect\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(urlDetectionRequest, \"/\", appUrl),\n );\n } catch (error) {\n logger.error(\"[CALLBACK_HANDLER] OAuth callback error:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n \"/?error=callback_failed\",\n appUrl,\n ),\n );\n }\n}\n\nconst revalidateUrlPath = async (url: string) => {\n try {\n const path = new URL(url).pathname;\n revalidatePath(path);\n } catch (error) {\n logger.warn(\"Failed to revalidate path after logout:\", error);\n }\n};\n\nexport async function handleLogout(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n // Get framework-agnostic request for URL utilities\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n\n // Read the state from the query parameters\n const state = request.nextUrl.searchParams.get(\"state\");\n const clientLogoutRedirectUrl =\n request.nextUrl.searchParams.get(\"logoutRedirectUrl\");\n\n try {\n logger.info(\"[LOGOUT_HANDLER] Backend logout endpoint called\");\n\n // If client provided a logoutRedirectUrl, override the config\n let configToUse = resolvedConfigs;\n if (clientLogoutRedirectUrl) {\n configToUse = {\n ...resolvedConfigs,\n logoutCallbackUrl: clientLogoutRedirectUrl,\n };\n logger.info(\"[LOGOUT_HANDLER] Overriding logout callback URL\", {\n original: resolvedConfigs.logoutCallbackUrl,\n override: clientLogoutRedirectUrl,\n });\n }\n\n const { civicAuth } = createCivicAuth(request, configToUse);\n\n // Always redirect to OAuth logout (like main branch)\n // Don't validate session - even invalid local sessions should hit OAuth logout\n logger.info(\"[LOGOUT_HANDLER] Processing logout request\", {\n state: !!state,\n });\n\n // Always redirect to OAuth logout endpoint (like main branch)\n // Client-side iframe logic will handle completion and redirect appropriately\n const logoutUrl = await civicAuth.buildLogoutRedirectUrl({\n state: state || undefined,\n });\n\n try {\n await civicAuth.clearTokens();\n\n // Revalidate current path to update authentication state in server components\n await revalidateUrlPath(request.url);\n } catch (error) {\n logger.error(\"[LOGOUT_HANDLER] Error clearing tokens:\", error);\n }\n\n // Remove state parameter from logout URL to prevent it from appearing in frontend URL\n const cleanLogoutUrl = new URL(logoutUrl);\n cleanLogoutUrl.searchParams.delete(\"state\");\n\n logger.info(\"[LOGOUT_HANDLER] Redirecting to OAuth logout endpoint\", {\n logoutUrl: cleanLogoutUrl.toString(),\n });\n\n return NextResponse.redirect(cleanLogoutUrl.toString());\n } catch (error) {\n logger.error(\"[LOGOUT_HANDLER] Logout error:\", error);\n // If logout URL generation fails, clear tokens and redirect to home\n await clearAuthCookies();\n\n const fallbackUrl =\n clientLogoutRedirectUrl || resolvedConfigs.logoutCallbackUrl;\n const finalFallbackUrl = CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n fallbackUrl,\n appUrl,\n );\n\n return NextResponse.redirect(finalFallbackUrl);\n }\n}\n\nexport async function handleLogoutCallback(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n logger.info(\n \"[LOGOUT_CALLBACK_HANDLER] Backend logout callback endpoint called\",\n );\n\n // Clear authentication cookies\n await clearAuthCookies();\n\n // Get framework-agnostic request and create CivicAuth instance\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n // Get the state parameter for iframe detection\n const state = request.nextUrl.searchParams.get(\"state\");\n\n // If this is an iframe request, return HTML with logout success signal\n if (state && displayModeFromState(state, \"iframe\") === \"iframe\") {\n // For iframe mode, include the post-logout redirect URL in the HTML\n const postLogoutRedirectUrl =\n civicAuth.getPostLogoutRedirectUrl(urlDetectionRequest);\n const response = new NextResponse(\n `<html lang=\"en\"><span style=\"display:none\">${LOGOUT_SUCCESS_TEXT}<a href=\"${postLogoutRedirectUrl}\" rel=\"civic-auth-post-logout-redirect-url\"></a></span></html>`,\n );\n response.headers.set(\"Content-Type\", \"text/html; charset=utf-8\");\n logger.info(\n \"[LOGOUT_CALLBACK_HANDLER] Returning iframe logout success HTML\",\n { postLogoutRedirectUrl },\n );\n return response;\n }\n\n // For non-iframe requests, redirect to the logout callback URL or post-logout URL\n const redirectUrl = civicAuth.getPostLogoutRedirectUrl(urlDetectionRequest);\n logger.info(\n \"[LOGOUT_CALLBACK_HANDLER] Redirecting to logout callback URL\",\n {\n logoutCallbackUrl: resolvedConfigs.logoutCallbackUrl,\n redirectUrl,\n },\n );\n\n // Revalidate the redirect path to update authentication state in server components\n await revalidateUrlPath(redirectUrl);\n return NextResponse.redirect(redirectUrl);\n } catch (error) {\n logger.error(\"[LOGOUT_CALLBACK_HANDLER] Logout callback error:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n resolvedConfigs.logoutCallbackUrl,\n appUrl,\n ),\n );\n }\n}\n\n/**\n * Creates an authentication handler for Next.js API routes\n *\n * Usage:\n * ```ts\n * // app/api/auth/[...civicauth]/route.ts\n * import { handler } from '@civic/auth/nextjs'\n * export const GET = handler({\n * // optional config overrides\n * })\n * ```\n */\nexport const handler =\n (authConfig = {}) =>\n async (request: NextRequest): Promise<NextResponse> => {\n const config = resolveAuthConfig(authConfig);\n\n try {\n const pathname = request.nextUrl.pathname;\n const pathSegments = pathname.split(\"/\");\n const lastSegment = pathSegments[pathSegments.length - 1];\n\n switch (lastSegment) {\n case \"challenge\":\n case \"login\":\n return await handleLogin(request, config);\n case \"callback\":\n return await handleCallback(request, config);\n case \"refresh\":\n return await handleRefresh(request, config);\n case \"logout\":\n return await handleLogout(request, config);\n case \"logoutcallback\":\n return await handleLogoutCallback(request, config);\n case \"user\":\n return await handleUser(request, config);\n default:\n throw new AuthError(`Invalid auth route: ${pathname}`, 404);\n }\n } catch (error) {\n logger.error(\"Auth handler error:\", error);\n\n const status = error instanceof AuthError ? error.status : 500;\n const message =\n error instanceof Error ? error.message : \"Authentication failed\";\n\n const response = NextResponse.json({ error: message }, { status });\n\n await clearAuthCookies();\n return response;\n }\n };\n\n/**\n * User endpoint - returns current user data as JSON\n * Uses CivicAuth.isLoggedIn() and getUser()\n */\nasync function handleUser(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n const isLoggedIn = await civicAuth.isLoggedIn();\n\n if (!isLoggedIn) {\n return NextResponse.json({ error: \"Not authenticated\" }, { status: 401 });\n }\n\n const user = await civicAuth.getUser();\n\n return NextResponse.json({ user });\n } catch (error) {\n logger.error(\"[USER_HANDLER] User endpoint error:\", error);\n return NextResponse.json(\n { error: \"Internal server error\" },\n { status: 500 },\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"routeHandler.js","sourceRoot":"","sources":["../../src/nextjs/routeHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA4B,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,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H,CAAC","sourcesContent":["import { CivicAuth, type UrlDetectionRequest } from \"@civic/auth/server\";\nimport { LOGOUT_SUCCESS_TEXT } from \"@/constants.js\";\nimport { loggers } from \"@/lib/logger.js\";\nimport { displayModeFromState } from \"@/lib/oauth.js\";\nimport type { AuthConfig } from \"@/nextjs/config.js\";\nimport { resolveAuthConfig } from \"@/nextjs/config.js\";\nimport { clearAuthCookies, NextjsCookieStorage } from \"@/nextjs/cookies.js\";\nimport { CodeVerifier, UserStorage } from \"@/shared/lib/types.js\";\nimport { revalidatePath } from \"next/cache.js\";\nimport type { NextRequest } from \"next/server.js\";\nimport { NextResponse } from \"next/server.js\";\nimport { prependBasePath, redirectWithBasePath } from \"./utils.js\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nclass AuthError extends Error {\n constructor(\n message: string,\n public readonly status: number = 401,\n ) {\n super(message);\n this.name = \"AuthError\";\n }\n}\n\n/**\n * Helper to convert NextRequest to UrlDetectionRequest for framework-agnostic URL handling\n */\nconst toUrlDetectionRequest = (request: NextRequest): UrlDetectionRequest => ({\n url: request.url,\n headers: Object.fromEntries(request.headers.entries()),\n searchParams: {\n get: (name: string) => request.nextUrl.searchParams.get(name),\n },\n cookies: {\n get: (name: string) => request.cookies.get(name),\n },\n});\n\n/**\n * Helper to create CivicAuth instance for a request\n * Now handles appUrl detection for proxy environments\n */\nconst createCivicAuth = (request: NextRequest, config: AuthConfig) => {\n const resolvedConfig = resolveAuthConfig(config);\n const cookieStorage = new NextjsCookieStorage({\n ...resolvedConfig.cookies?.tokens,\n [UserStorage.USER]: resolvedConfig.cookies?.user,\n });\n\n // Convert to framework-agnostic request format\n const urlDetectionRequest = toUrlDetectionRequest(request);\n\n // Get appUrl from client (for proxy environments)\n const clientAppUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n\n // Use baseUrl from config, then client appUrl, then request origin\n // This matches the main branch priority: config > client > request\n const appUrl =\n resolvedConfig.baseUrl ||\n clientAppUrl ||\n new URL(urlDetectionRequest.url).origin;\n\n // Build absolute URLs using detected appUrl or request origin\n const absoluteCallbackUrl = resolvedConfig.callbackUrl.startsWith(\"http\")\n ? resolvedConfig.callbackUrl\n : CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n resolvedConfig.callbackUrl,\n appUrl,\n );\n const absoluteLogoutCallbackUrl = resolvedConfig.logoutCallbackUrl.startsWith(\n \"http\",\n )\n ? resolvedConfig.logoutCallbackUrl\n : CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n resolvedConfig.logoutCallbackUrl,\n appUrl,\n );\n\n const civicAuth = new CivicAuth(cookieStorage, {\n clientId: resolvedConfig.clientId,\n redirectUrl: absoluteCallbackUrl,\n oauthServer: resolvedConfig.oauthServer,\n postLogoutRedirectUrl: absoluteLogoutCallbackUrl,\n loginSuccessUrl: request.url,\n });\n\n return {\n civicAuth,\n cookieStorage,\n appUrl, // Return appUrl for use in other functions\n urlDetectionRequest, // Return for use in handlers\n };\n};\n\n/**\n * Login handler - backend OAuth login initiation endpoint\n * Uses CivicAuth.buildLoginUrl()\n */\nasync function handleLogin(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n const frontendState = request.nextUrl.searchParams.get(\"state\");\n\n // Store appUrl in cookie if provided as query parameter\n const appUrlFromQuery = request.nextUrl.searchParams.get(\"appUrl\");\n if (appUrlFromQuery) {\n const cookieStorage = new NextjsCookieStorage(\n resolvedConfigs.cookies?.tokens ?? {},\n );\n await cookieStorage.set(CodeVerifier.APP_URL, appUrlFromQuery);\n }\n\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n const url = await civicAuth.buildLoginUrl({\n state: frontendState || undefined,\n });\n\n logger.info(\"[LOGIN_HANDLER] Redirecting to OAuth login URL\", {\n loginUrl: url.toString(),\n });\n\n return NextResponse.redirect(url.toString());\n } catch (error) {\n logger.error(\"[LOGIN_HANDLER] Backend login error:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n \"/?error=login_failed\",\n appUrl,\n ),\n );\n }\n}\n\nasync function handleRefresh(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n await civicAuth.refreshTokens();\n\n logger.info(\"[REFRESH_HANDLER] Tokens refreshed successfully\");\n\n return NextResponse.json({\n status: \"success\",\n message: \"Tokens refreshed\",\n });\n } catch (error) {\n logger.error(\"[REFRESH_HANDLER] Token refresh error:\", error);\n return NextResponse.json(\n { error: \"Token refresh failed\" },\n { status: 500 },\n );\n }\n}\n\nasync function handleCallback(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n const code = request.nextUrl.searchParams.get(\"code\");\n const state = request.nextUrl.searchParams.get(\"state\");\n const error = request.nextUrl.searchParams.get(\"error\");\n\n if (error) {\n logger.error(\"OAuth error in callback:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n \"/?error=oauth_error\",\n appUrl,\n ),\n );\n }\n\n if (!code || !state) throw new AuthError(\"Bad parameters\", 400);\n\n try {\n const { civicAuth, appUrl, urlDetectionRequest } = createCivicAuth(\n request,\n resolvedConfigs,\n );\n\n // Convert NextRequest to the format expected by handleCallback\n const handleCallbackRequest = {\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url.toString(),\n };\n\n // Get loginSuccessUrl with proper baseUrl handling\n const loginSuccessUrl = CivicAuth.getLoginSuccessUrl(\n urlDetectionRequest,\n appUrl,\n );\n const frontendUrl =\n loginSuccessUrl || resolvedConfigs.loginSuccessUrl || \"/\";\n\n // Use CivicAuth's smart callback handler\n const result = await civicAuth.handleCallback(\n {\n code,\n state,\n req: handleCallbackRequest,\n },\n {\n // Pass the properly resolved frontendUrl\n frontendUrl: prependBasePath(frontendUrl, config.basePath || \"\"),\n },\n );\n\n if (result.redirectTo) {\n return redirectWithBasePath(\n config,\n CivicAuth.toAbsoluteUrl(urlDetectionRequest, result.redirectTo, appUrl),\n );\n }\n\n if (result.content) {\n // Handle both string content and object content\n if (typeof result.content === \"string\") {\n return new NextResponse(result.content, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/html\",\n },\n });\n } else {\n // Object content (JSON response)\n return NextResponse.json(result.content);\n }\n }\n\n // Fallback redirect\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(urlDetectionRequest, \"/\", appUrl),\n );\n } catch (error) {\n logger.error(\"[CALLBACK_HANDLER] OAuth callback error:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return NextResponse.redirect(\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n \"/?error=callback_failed\",\n appUrl,\n ),\n );\n }\n}\n\nconst revalidateUrlPath = async (url: string) => {\n try {\n const path = new URL(url).pathname;\n revalidatePath(path);\n } catch (error) {\n logger.warn(\"Failed to revalidate path after logout:\", error);\n }\n};\n\nexport async function handleLogout(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n // Get framework-agnostic request for URL utilities\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n\n // Read the state from the query parameters\n const state = request.nextUrl.searchParams.get(\"state\");\n const clientLogoutRedirectUrl =\n request.nextUrl.searchParams.get(\"logoutRedirectUrl\");\n\n try {\n logger.info(\"[LOGOUT_HANDLER] Backend logout endpoint called\");\n\n // If client provided a logoutRedirectUrl, override the config\n let configToUse = resolvedConfigs;\n if (clientLogoutRedirectUrl) {\n configToUse = {\n ...resolvedConfigs,\n logoutCallbackUrl: clientLogoutRedirectUrl,\n };\n logger.info(\"[LOGOUT_HANDLER] Overriding logout callback URL\", {\n original: resolvedConfigs.logoutCallbackUrl,\n override: clientLogoutRedirectUrl,\n });\n }\n\n const { civicAuth } = createCivicAuth(request, configToUse);\n\n // Always redirect to OAuth logout (like main branch)\n // Don't validate session - even invalid local sessions should hit OAuth logout\n logger.info(\"[LOGOUT_HANDLER] Processing logout request\", {\n state: !!state,\n });\n\n // Always redirect to OAuth logout endpoint (like main branch)\n // Client-side iframe logic will handle completion and redirect appropriately\n const logoutUrl = await civicAuth.buildLogoutRedirectUrl({\n state: state || undefined,\n });\n\n try {\n await clearAuthCookies(resolvedConfigs);\n\n // Revalidate current path to update authentication state in server components\n await revalidateUrlPath(request.url);\n } catch (error) {\n logger.error(\"[LOGOUT_HANDLER] Error clearing tokens:\", error);\n }\n\n // Remove state parameter from logout URL to prevent it from appearing in frontend URL\n const cleanLogoutUrl = new URL(logoutUrl);\n cleanLogoutUrl.searchParams.delete(\"state\");\n\n logger.info(\"[LOGOUT_HANDLER] Redirecting to OAuth logout endpoint\", {\n logoutUrl: cleanLogoutUrl.toString(),\n });\n\n return NextResponse.redirect(cleanLogoutUrl.toString());\n } catch (error) {\n logger.error(\"[LOGOUT_HANDLER] Logout error:\", error);\n // If logout URL generation fails, clear tokens and redirect to home\n await clearAuthCookies(resolvedConfigs);\n\n const fallbackUrl =\n clientLogoutRedirectUrl || resolvedConfigs.logoutCallbackUrl;\n const finalFallbackUrl = CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n fallbackUrl,\n appUrl,\n );\n\n return NextResponse.redirect(finalFallbackUrl);\n }\n}\n\nexport async function handleLogoutCallback(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n logger.info(\n \"[LOGOUT_CALLBACK_HANDLER] Backend logout callback endpoint called\",\n );\n\n // Clear authentication cookies\n await clearAuthCookies(resolvedConfigs);\n\n // Get framework-agnostic request and create CivicAuth instance\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n // Get the state parameter for iframe detection\n const state = request.nextUrl.searchParams.get(\"state\");\n\n // If this is an iframe request, return HTML with logout success signal\n if (state && displayModeFromState(state, \"iframe\") === \"iframe\") {\n // For iframe mode, include the post-logout redirect URL in the HTML\n const postLogoutRedirectUrl =\n civicAuth.getPostLogoutRedirectUrl(urlDetectionRequest);\n const response = new NextResponse(\n `<html lang=\"en\"><span style=\"display:none\">${LOGOUT_SUCCESS_TEXT}<a href=\"${postLogoutRedirectUrl}\" rel=\"civic-auth-post-logout-redirect-url\"></a></span></html>`,\n );\n response.headers.set(\"Content-Type\", \"text/html; charset=utf-8\");\n logger.info(\n \"[LOGOUT_CALLBACK_HANDLER] Returning iframe logout success HTML\",\n { postLogoutRedirectUrl },\n );\n return response;\n }\n\n // For non-iframe requests, redirect to the logout callback URL or post-logout URL\n const redirectUrl = civicAuth.getPostLogoutRedirectUrl(urlDetectionRequest);\n logger.info(\n \"[LOGOUT_CALLBACK_HANDLER] Redirecting to logout callback URL\",\n {\n logoutCallbackUrl: resolvedConfigs.logoutCallbackUrl,\n redirectUrl,\n },\n );\n\n // Revalidate the redirect path to update authentication state in server components\n await revalidateUrlPath(redirectUrl);\n return redirectWithBasePath(config, redirectUrl);\n } catch (error) {\n logger.error(\"[LOGOUT_CALLBACK_HANDLER] Logout callback error:\", error);\n const urlDetectionRequest = toUrlDetectionRequest(request);\n const appUrl = CivicAuth.getAppUrl(urlDetectionRequest);\n return redirectWithBasePath(\n config,\n CivicAuth.toAbsoluteUrl(\n urlDetectionRequest,\n resolvedConfigs.logoutCallbackUrl,\n appUrl,\n ),\n );\n }\n}\n\n/**\n * Creates an authentication handler for Next.js API routes\n *\n * Usage:\n * ```ts\n * // app/api/auth/[...civicauth]/route.ts\n * import { handler } from '@civic/auth/nextjs'\n * export const GET = handler({\n * // optional config overrides\n * })\n * export const POST = handler({\n * // optional config overrides\n * })\n * ```\n */\nexport const handler =\n (authConfig = {}) =>\n async (request: NextRequest): Promise<NextResponse> => {\n const config = resolveAuthConfig(authConfig);\n\n try {\n const pathname = request.nextUrl.pathname;\n const pathSegments = pathname.split(\"/\");\n const lastSegment = pathSegments[pathSegments.length - 1];\n\n switch (lastSegment) {\n case \"challenge\":\n case \"login\":\n return await handleLogin(request, config);\n case \"callback\":\n return await handleCallback(request, config);\n case \"refresh\":\n return await handleRefresh(request, config);\n case \"logout\":\n return await handleLogout(request, config);\n case \"logoutcallback\":\n return await handleLogoutCallback(request, config);\n case \"user\":\n return await handleUser(request, config);\n default:\n throw new AuthError(`Invalid auth route: ${pathname}`, 404);\n }\n } catch (error) {\n logger.error(\"Auth handler error:\", error);\n\n const status = error instanceof AuthError ? error.status : 500;\n const message =\n error instanceof Error ? error.message : \"Authentication failed\";\n\n const response = NextResponse.json({ error: message }, { status });\n\n await clearAuthCookies(config);\n return response;\n }\n };\n\n/**\n * User endpoint - returns current user data as JSON\n * Uses CivicAuth.isLoggedIn() and getUser()\n */\nasync function handleUser(\n request: NextRequest,\n config: AuthConfig,\n): Promise<NextResponse> {\n const resolvedConfigs = resolveAuthConfig(config);\n\n try {\n const { civicAuth } = createCivicAuth(request, resolvedConfigs);\n\n const isLoggedIn = await civicAuth.isLoggedIn();\n\n if (!isLoggedIn) {\n const statusCode = request.nextUrl.searchParams.get(\n \"optimisticRehydration\",\n )\n ? 202\n : 401;\n return NextResponse.json(\n { error: \"Not authenticated\" },\n { status: statusCode },\n );\n }\n\n const user = await civicAuth.getUser();\n\n return NextResponse.json({ user });\n } catch (error) {\n logger.error(\"[USER_HANDLER] User endpoint error:\", error);\n return NextResponse.json(\n { error: \"Internal server error\" },\n { status: 500 },\n );\n }\n}\n"]}
|
package/dist/nextjs/utils.d.ts
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AuthConfig, type AuthConfigWithDefaults } from "../nextjs/config.js";
|
|
2
2
|
import type { NextRequest } from "next/server.js";
|
|
3
3
|
import { NextResponse } from "next/server.js";
|
|
4
4
|
import type { SessionData } from "../types.js";
|
|
5
|
+
import { type CookieConfig, type KeySetter } from "../shared/lib/types.js";
|
|
6
|
+
import { CookieStorage } from "../server/index.js";
|
|
5
7
|
export declare const resolveCallbackUrl: (config: AuthConfigWithDefaults, baseUrl?: string) => string;
|
|
6
8
|
export declare function sanitizeBasePath(path: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Removes the basePath prefix from a pathname, properly handling edge cases
|
|
11
|
+
* This is the inverse operation of adding basePath to a URL
|
|
12
|
+
*/
|
|
13
|
+
export declare function removeBasePathFromPath(pathname: string, basePath?: string): string;
|
|
7
14
|
/**
|
|
8
15
|
* Determines if we should attempt token refresh based on session state
|
|
9
16
|
*/
|
|
10
17
|
export declare const shouldAttemptRefresh: (session: SessionData) => boolean;
|
|
11
18
|
/**
|
|
12
|
-
*
|
|
19
|
+
* Checks if the current path is a system URL that should skip auth
|
|
13
20
|
*/
|
|
14
|
-
export declare const
|
|
21
|
+
export declare const shouldSkipAuthForSystemUrls: (pathname: string, authConfig: AuthConfigWithDefaults) => boolean;
|
|
15
22
|
/**
|
|
16
|
-
*
|
|
23
|
+
* CookieStorage implementation for NextJS middleware context that works with NextRequest
|
|
17
24
|
*/
|
|
18
|
-
export declare
|
|
25
|
+
export declare class NextjsMiddlewareCookieStorage extends CookieStorage {
|
|
26
|
+
config: Partial<Record<KeySetter, CookieConfig>>;
|
|
27
|
+
private request;
|
|
28
|
+
private response;
|
|
29
|
+
constructor(config: Partial<Record<KeySetter, CookieConfig>> | undefined, request: NextRequest, response: NextResponse);
|
|
30
|
+
get(key: string): Promise<string | null>;
|
|
31
|
+
set(key: string, value: string, cookieConfigOverride: CookieConfig): Promise<void>;
|
|
32
|
+
delete(key: string): Promise<void>;
|
|
33
|
+
}
|
|
19
34
|
/**
|
|
20
35
|
* Handles authentication logic specifically for the login URL
|
|
21
36
|
* Provides logging for login URL access patterns
|
|
@@ -25,8 +40,17 @@ export declare const handleLoginUrl: (pathname: string, session: SessionData, au
|
|
|
25
40
|
* Checks if the current path should skip auth based on include/exclude patterns
|
|
26
41
|
*/
|
|
27
42
|
export declare const shouldSkipAuthForRoutePatterns: (pathname: string, authConfig: AuthConfigWithDefaults) => boolean;
|
|
43
|
+
export declare const copyCivicCookies: (sourceResponse: NextResponse, targetCall: NextResponse | NextRequest) => void;
|
|
28
44
|
/**
|
|
29
45
|
* Handles final authentication logic for unauthenticated users on protected routes
|
|
30
46
|
*/
|
|
31
|
-
export declare const handleUnauthenticatedUser: (session: SessionData, request: NextRequest, authConfig: AuthConfigWithDefaults) => NextResponse
|
|
47
|
+
export declare const handleUnauthenticatedUser: (session: SessionData, request: NextRequest, response: NextResponse, storage: NextjsMiddlewareCookieStorage, authConfig: AuthConfigWithDefaults) => Promise<NextResponse | undefined>;
|
|
48
|
+
/**
|
|
49
|
+
* Prepends the basePath onto a given URL if it's not already there. Works for both relative and absolute URLs.
|
|
50
|
+
* @param url
|
|
51
|
+
* @param basePath
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
export declare const prependBasePath: (url: string, basePath: string) => string;
|
|
55
|
+
export declare const redirectWithBasePath: (config: AuthConfig, targetUrl: string) => NextResponse;
|
|
32
56
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/nextjs/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/nextjs/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,sBAAsB,EAC5B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,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ACP,WAAW,cACR,sBAAsB,KACjC,IAWF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,8BAA8B,aAC/B,MAAM,cACJ,sBAAsB,KACjC,OAiBF,CAAC;AAEF,eAAO,MAAM,gBAAgB,mBACX,YAAY,cAChB,YAAY,GAAG,WAAW,SAoBvC,CAAC;AACF;;GAEG;AACH,eAAO,MAAM,yBAAyB,YAC3B,WAAW,WACX,WAAW,YACV,YAAY,WACb,6BAA6B,cAC1B,sBAAsB,KACjC,OAAO,CAAC,YAAY,GAAG,SAAS,CA8BlC,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAS,MAAM,YAAY,MAAM,WAiB5D,CAAC;AAEF,eAAO,MAAM,oBAAoB,WACvB,UAAU,aACP,MAAM,KAChB,YACuE,CAAC"}
|
package/dist/nextjs/utils.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import { systemUrlsConfig, } from "../nextjs/config.js";
|
|
1
2
|
import { NextResponse } from "next/server.js";
|
|
2
3
|
import { loggers } from "../lib/logger.js";
|
|
3
4
|
import picomatch from "picomatch";
|
|
5
|
+
import { CodeVerifier, OAuthTokenTypes, UserStorage, } from "../shared/lib/types.js";
|
|
6
|
+
import { clearTokens, getCookieConfiguration } from "../shared/lib/util.js";
|
|
7
|
+
import { CookieStorage } from "../server/index.js";
|
|
8
|
+
import { extractCookieFromRawHeader } from "../shared/lib/cookieUtils.js";
|
|
4
9
|
const logger = loggers.nextjs.middleware;
|
|
5
10
|
export const resolveCallbackUrl = (config, baseUrl) => {
|
|
6
11
|
const callbackUrl = new URL(config?.callbackUrl, baseUrl).toString();
|
|
@@ -14,6 +19,29 @@ export function sanitizeBasePath(path) {
|
|
|
14
19
|
// Remove all trailing slashes (not just one)
|
|
15
20
|
return withLeadingSlash.replace(/\/+$/, "");
|
|
16
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Removes the basePath prefix from a pathname, properly handling edge cases
|
|
24
|
+
* This is the inverse operation of adding basePath to a URL
|
|
25
|
+
*/
|
|
26
|
+
export function removeBasePathFromPath(pathname, basePath) {
|
|
27
|
+
if (!basePath || basePath === "" || basePath === "/") {
|
|
28
|
+
return pathname;
|
|
29
|
+
}
|
|
30
|
+
// Sanitize the basePath to ensure consistent format
|
|
31
|
+
const sanitizedBasePath = sanitizeBasePath(basePath);
|
|
32
|
+
// Check if pathname starts with the basePath followed by a slash or end of string
|
|
33
|
+
// This prevents partial matches like "/app" matching "/application"
|
|
34
|
+
if (pathname === sanitizedBasePath) {
|
|
35
|
+
// Exact match - return root
|
|
36
|
+
return "/";
|
|
37
|
+
}
|
|
38
|
+
else if (pathname.startsWith(sanitizedBasePath + "/")) {
|
|
39
|
+
// basePath followed by slash - remove basePath but keep the slash
|
|
40
|
+
return pathname.slice(sanitizedBasePath.length);
|
|
41
|
+
}
|
|
42
|
+
// If basePath doesn't match as a complete path segment, return pathname as-is
|
|
43
|
+
return pathname;
|
|
44
|
+
}
|
|
17
45
|
const getOriginUrl = (request, authConfig) => {
|
|
18
46
|
// Use configured baseUrl if provided (for reverse proxy scenarios)
|
|
19
47
|
if (authConfig.baseUrl) {
|
|
@@ -42,37 +70,88 @@ const matchesGlobs = (pathname, patterns) => patterns.some((pattern) => {
|
|
|
42
70
|
export const shouldAttemptRefresh = (session) => {
|
|
43
71
|
return !session.authenticated && !!session.refreshToken;
|
|
44
72
|
};
|
|
45
|
-
/**
|
|
46
|
-
* Creates a redirect response to the refresh endpoint
|
|
47
|
-
*/
|
|
48
|
-
export const createRefreshResponse = (request, authConfig) => {
|
|
49
|
-
const targetUrl = getOriginUrl(request, authConfig) + request.nextUrl.pathname;
|
|
50
|
-
// Check if we're already in a redirect flow
|
|
51
|
-
if (request.nextUrl.searchParams.has("targetUrl")) {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
// Create refresh redirect
|
|
55
|
-
const refreshUrl = new URL(authConfig.refreshUrl, getOriginUrl(request, authConfig));
|
|
56
|
-
refreshUrl.searchParams.set("targetUrl", targetUrl);
|
|
57
|
-
logger.debug("→ Refresh token found - redirecting to refresh endpoint");
|
|
58
|
-
return NextResponse.redirect(refreshUrl.toString());
|
|
59
|
-
};
|
|
60
73
|
/**
|
|
61
74
|
* Checks if the current path is a system URL that should skip auth
|
|
62
75
|
*/
|
|
63
|
-
export const shouldSkipAuthForSystemUrls = (pathname, authConfig
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
76
|
+
export const shouldSkipAuthForSystemUrls = (pathname, authConfig) => {
|
|
77
|
+
// make an array of all system URLs from authConfig using the systemUrlsConfig keys
|
|
78
|
+
const systemUrls = Object.keys(systemUrlsConfig).map((key) => authConfig[key]);
|
|
79
|
+
// check if any of the urls in systemUrls has a substring match with pathname
|
|
80
|
+
// the systemUrl could have a basePath, i.e. /dashboard/api/auth/callback, whereas the pathname will not
|
|
81
|
+
// therefore we check if the systemUrl includes the pathname
|
|
82
|
+
const isSystemUrl = systemUrls.some((url) => url && url.includes(pathname));
|
|
83
|
+
logger.debug("→ isSystemUrl check", { pathname, isSystemUrl, systemUrls });
|
|
71
84
|
if (isSystemUrl) {
|
|
72
85
|
logger.debug("→ Skipping auth check - this a URL defined in authConfig", pathname);
|
|
73
86
|
}
|
|
74
87
|
return isSystemUrl;
|
|
75
88
|
};
|
|
89
|
+
/**
|
|
90
|
+
* CookieStorage implementation for NextJS middleware context that works with NextRequest
|
|
91
|
+
*/
|
|
92
|
+
export class NextjsMiddlewareCookieStorage extends CookieStorage {
|
|
93
|
+
config;
|
|
94
|
+
request;
|
|
95
|
+
response;
|
|
96
|
+
constructor(config = {}, request, response) {
|
|
97
|
+
super({
|
|
98
|
+
secure: true,
|
|
99
|
+
httpOnly: true,
|
|
100
|
+
});
|
|
101
|
+
this.config = config;
|
|
102
|
+
this.request = request;
|
|
103
|
+
this.response = response;
|
|
104
|
+
}
|
|
105
|
+
async get(key) {
|
|
106
|
+
// First try to get cookies from the response if it has already been set
|
|
107
|
+
const cookieValue = this.response.cookies.get(key)?.value;
|
|
108
|
+
if (cookieValue) {
|
|
109
|
+
return cookieValue;
|
|
110
|
+
}
|
|
111
|
+
const cookieSettings = this.config?.[key] || {};
|
|
112
|
+
const configuredPath = cookieSettings.path;
|
|
113
|
+
// If we have a non-root basePath, use raw header parsing to get the first cookie
|
|
114
|
+
// which should be from the most specific path, avoiding duplicate cookie conflicts
|
|
115
|
+
if (configuredPath && configuredPath !== "/") {
|
|
116
|
+
const cookieHeader = this.request.headers.get("cookie");
|
|
117
|
+
const rawValue = extractCookieFromRawHeader(cookieHeader, key);
|
|
118
|
+
if (rawValue) {
|
|
119
|
+
return rawValue;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Fallback to standard Next.js request cookies
|
|
123
|
+
return this.request.cookies.get(key)?.value || null;
|
|
124
|
+
}
|
|
125
|
+
async set(key, value, cookieConfigOverride) {
|
|
126
|
+
const cookieSettings = this.config?.[key] || {
|
|
127
|
+
...this.settings,
|
|
128
|
+
};
|
|
129
|
+
const dynamicConfig = getCookieConfiguration(this.request);
|
|
130
|
+
const useCookieSettings = {
|
|
131
|
+
...cookieSettings,
|
|
132
|
+
...cookieConfigOverride,
|
|
133
|
+
// Apply dynamic configuration for secure and sameSite
|
|
134
|
+
secure: dynamicConfig.secure,
|
|
135
|
+
sameSite: dynamicConfig.sameSite,
|
|
136
|
+
};
|
|
137
|
+
// Respect the httpOnly setting from configuration instead of hardcoding it
|
|
138
|
+
this.response.cookies.set(key, value, useCookieSettings);
|
|
139
|
+
}
|
|
140
|
+
async delete(key) {
|
|
141
|
+
// Get cookie configuration for this key to respect the path setting
|
|
142
|
+
const cookieSettings = this.config?.[key] || {};
|
|
143
|
+
// If we have a path configured, use it when deleting the cookie
|
|
144
|
+
if (cookieSettings.path) {
|
|
145
|
+
this.response.cookies.set(key, "", {
|
|
146
|
+
expires: new Date(0), // Expire in the past
|
|
147
|
+
path: cookieSettings.path,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
this.response.cookies.delete(key);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
76
155
|
/**
|
|
77
156
|
* Handles authentication logic specifically for the login URL
|
|
78
157
|
* Provides logging for login URL access patterns
|
|
@@ -105,22 +184,67 @@ export const shouldSkipAuthForRoutePatterns = (pathname, authConfig) => {
|
|
|
105
184
|
}
|
|
106
185
|
return false;
|
|
107
186
|
};
|
|
187
|
+
export const copyCivicCookies = (sourceResponse, targetCall) => {
|
|
188
|
+
const civicCookieNames = [
|
|
189
|
+
...Object.values(OAuthTokenTypes),
|
|
190
|
+
...Object.values(UserStorage),
|
|
191
|
+
...Object.values(CodeVerifier),
|
|
192
|
+
];
|
|
193
|
+
logger.debug("Copying Civic cookies:", {
|
|
194
|
+
src: sourceResponse.url,
|
|
195
|
+
target: targetCall.url,
|
|
196
|
+
});
|
|
197
|
+
sourceResponse?.cookies
|
|
198
|
+
.getAll()
|
|
199
|
+
.filter((cookie) => civicCookieNames.includes(cookie.name))
|
|
200
|
+
.forEach((cookie) => {
|
|
201
|
+
logger.debug("Setting middlewareResponse cookie:", cookie);
|
|
202
|
+
targetCall.cookies.set(cookie);
|
|
203
|
+
});
|
|
204
|
+
};
|
|
108
205
|
/**
|
|
109
206
|
* Handles final authentication logic for unauthenticated users on protected routes
|
|
110
207
|
*/
|
|
111
|
-
export const handleUnauthenticatedUser = (session, request, authConfig) => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return NextResponse.redirect(clearBadTokensUrl.toString());
|
|
119
|
-
}
|
|
120
|
-
// Final fallback: redirect to login
|
|
208
|
+
export const handleUnauthenticatedUser = async (session, request, response, storage, authConfig) => {
|
|
209
|
+
// Clear expired/invalid tokens if they exist
|
|
210
|
+
if (session.accessToken || session.idToken || session.refreshToken) {
|
|
211
|
+
logger.debug(`→ Clearing expired/invalid tokens`);
|
|
212
|
+
await clearTokens(storage);
|
|
213
|
+
}
|
|
214
|
+
// Final fallback: redirect to login unless we're already there.
|
|
121
215
|
const loginUrl = new URL(authConfig.loginUrl, getOriginUrl(request, authConfig));
|
|
122
216
|
const redirectUrl = `${loginUrl.toString()}`;
|
|
123
|
-
|
|
124
|
-
return
|
|
217
|
+
const loginPathWithoutBasePath = removeBasePathFromPath(loginUrl.pathname, authConfig.basePath);
|
|
218
|
+
// If we're already at the login URL, the middleware will just return undefined.
|
|
219
|
+
// This is to prevent an infinite redirect loop if middleware is applied to the login route itself.
|
|
220
|
+
// The loginUrl from getOriginUrl already includes the basePath, but request.nextUrl.pathname does not. So we strip it off to enable comparison.
|
|
221
|
+
if (request.nextUrl.pathname !== loginPathWithoutBasePath) {
|
|
222
|
+
logger.debug(`→ No valid tokens found - redirecting to login "${redirectUrl}"`);
|
|
223
|
+
const redirectedResponse = redirectWithBasePath(authConfig, redirectUrl);
|
|
224
|
+
return redirectedResponse;
|
|
225
|
+
}
|
|
226
|
+
return response;
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Prepends the basePath onto a given URL if it's not already there. Works for both relative and absolute URLs.
|
|
230
|
+
* @param url
|
|
231
|
+
* @param basePath
|
|
232
|
+
* @returns
|
|
233
|
+
*/
|
|
234
|
+
export const prependBasePath = (url, basePath) => {
|
|
235
|
+
basePath = "/" + basePath.replace(/^\/|\/$/g, ""); // normalize basePath
|
|
236
|
+
const isAbsolute = /^https?:\/\//.test(url);
|
|
237
|
+
if (isAbsolute) {
|
|
238
|
+
const u = new URL(url);
|
|
239
|
+
if (!u.pathname.startsWith(basePath)) {
|
|
240
|
+
u.pathname =
|
|
241
|
+
basePath + (u.pathname.startsWith("/") ? "" : "/") + u.pathname;
|
|
242
|
+
}
|
|
243
|
+
return u.toString();
|
|
244
|
+
}
|
|
245
|
+
return url.startsWith(basePath)
|
|
246
|
+
? url
|
|
247
|
+
: basePath + (url.startsWith("/") ? "" : "/") + url;
|
|
125
248
|
};
|
|
249
|
+
export const redirectWithBasePath = (config, targetUrl) => NextResponse.redirect(prependBasePath(targetUrl, config.basePath || ""));
|
|
126
250
|
//# sourceMappingURL=utils.js.map
|