Spaces:
Sleeping
Sleeping
tumberger commited on
Commit ·
00957cd
1
Parent(s): 22da39f
Restore retry logic and SameSite=none cookies to fix redirect loops
Browse files- app/(auth)/auth.config.ts +28 -0
- middleware.ts +12 -1
app/(auth)/auth.config.ts
CHANGED
|
@@ -10,4 +10,32 @@ export const authConfig = {
|
|
| 10 |
// while this file is also used in non-Node.js environments
|
| 11 |
],
|
| 12 |
callbacks: {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
} satisfies NextAuthConfig;
|
|
|
|
| 10 |
// while this file is also used in non-Node.js environments
|
| 11 |
],
|
| 12 |
callbacks: {},
|
| 13 |
+
cookies: {
|
| 14 |
+
sessionToken: {
|
| 15 |
+
name: `next-auth.session-token`,
|
| 16 |
+
options: {
|
| 17 |
+
httpOnly: true,
|
| 18 |
+
sameSite: 'none',
|
| 19 |
+
path: '/',
|
| 20 |
+
secure: true,
|
| 21 |
+
},
|
| 22 |
+
},
|
| 23 |
+
callbackUrl: {
|
| 24 |
+
name: `next-auth.callback-url`,
|
| 25 |
+
options: {
|
| 26 |
+
sameSite: 'none',
|
| 27 |
+
path: '/',
|
| 28 |
+
secure: true,
|
| 29 |
+
},
|
| 30 |
+
},
|
| 31 |
+
csrfToken: {
|
| 32 |
+
name: `next-auth.csrf-token`,
|
| 33 |
+
options: {
|
| 34 |
+
httpOnly: true,
|
| 35 |
+
sameSite: 'none',
|
| 36 |
+
path: '/',
|
| 37 |
+
secure: true,
|
| 38 |
+
},
|
| 39 |
+
},
|
| 40 |
+
},
|
| 41 |
} satisfies NextAuthConfig;
|
middleware.ts
CHANGED
|
@@ -25,10 +25,21 @@ export async function middleware(request: NextRequest) {
|
|
| 25 |
|
| 26 |
if (!token) {
|
| 27 |
const redirectUrl = encodeURIComponent(request.url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
new URL(`/api/auth/guest?redirectUrl=${redirectUrl}`, request.url),
|
| 31 |
);
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
const isGuest = guestRegex.test(token?.email ?? '');
|
|
|
|
| 25 |
|
| 26 |
if (!token) {
|
| 27 |
const redirectUrl = encodeURIComponent(request.url);
|
| 28 |
+
|
| 29 |
+
// Prevent redirect loops - after 3 attempts, just let the user through
|
| 30 |
+
const retryCount = request.cookies.get('auth-retry')?.value || '0';
|
| 31 |
+
if (parseInt(retryCount) > 2) {
|
| 32 |
+
// Clear retry count and continue without auth
|
| 33 |
+
const response = NextResponse.next();
|
| 34 |
+
response.cookies.delete('auth-retry');
|
| 35 |
+
return response;
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
const response = NextResponse.redirect(
|
| 39 |
new URL(`/api/auth/guest?redirectUrl=${redirectUrl}`, request.url),
|
| 40 |
);
|
| 41 |
+
response.cookies.set('auth-retry', String(parseInt(retryCount) + 1));
|
| 42 |
+
return response;
|
| 43 |
}
|
| 44 |
|
| 45 |
const isGuest = guestRegex.test(token?.email ?? '');
|