Skip to content

Commit fbc59d8

Browse files
Merge pull request #297 from community-scripts/fix/auth
Fix auth cookie secure flag for HTTP in production
2 parents 8af011a + 8c27eac commit fbc59d8

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/app/_components/AuthProvider.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ export function AuthProvider({ children }: AuthProviderProps) {
106106
setUsername(data.username);
107107

108108
// Check auth again to get expiration time
109-
await checkAuth();
109+
// Add a small delay to ensure the httpOnly cookie is available
110+
await new Promise<void>((resolve) => {
111+
setTimeout(() => {
112+
void checkAuth().then(() => resolve());
113+
}, 150);
114+
});
110115
return true;
111116
} else {
112117
const errorData = await response.json();

src/app/api/auth/login/route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ export async function POST(request: NextRequest) {
4747
username
4848
});
4949

50+
// Determine if request is over HTTPS
51+
const isSecure = request.url.startsWith('https://');
52+
5053
// Set httpOnly cookie with configured duration
5154
response.cookies.set('auth-token', token, {
5255
httpOnly: true,
53-
secure: process.env.NODE_ENV === 'production',
56+
secure: isSecure, // Only secure if actually over HTTPS
5457
sameSite: 'strict',
5558
maxAge: sessionDurationDays * 24 * 60 * 60, // Use configured duration
5659
path: '/',

0 commit comments

Comments
 (0)