mirror of
https://gitlab.w-hs.de/an14051/backerup-website.git
synced 2026-07-27 17:35:29 +00:00
feat: enhance session management by adding secure cookie handling based on HTTPS detection
This commit is contained in:
@@ -86,6 +86,13 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearAttempts(ip)
|
clearAttempts(ip)
|
||||||
await createSession({ userId: user.id, username: user.username, role: user.role })
|
|
||||||
|
// Detect whether the client is on HTTPS (via reverse-proxy header or direct TLS).
|
||||||
|
// CasaOS and most self-hosted setups run over plain HTTP, so the cookie must
|
||||||
|
// NOT be marked Secure or the browser will silently drop it.
|
||||||
|
const proto = request.headers.get('x-forwarded-proto') ?? ''
|
||||||
|
const isHttps = proto.split(',')[0].trim() === 'https'
|
||||||
|
|
||||||
|
await createSession({ userId: user.id, username: user.username, role: user.role }, isHttps)
|
||||||
return NextResponse.json({ ok: true, username: user.username, role: user.role })
|
return NextResponse.json({ ok: true, username: user.username, role: user.role })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export function getSecretKey(): Uint8Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Creates and stores a session JWT in an httpOnly cookie. */
|
/** Creates and stores a session JWT in an httpOnly cookie. */
|
||||||
export async function createSession(payload: SessionPayload): Promise<void> {
|
export async function createSession(payload: SessionPayload, secure?: boolean): Promise<void> {
|
||||||
const expiresAt = new Date(Date.now() + SESSION_DURATION_MS)
|
const expiresAt = new Date(Date.now() + SESSION_DURATION_MS)
|
||||||
const token = await new SignJWT({ ...payload })
|
const token = await new SignJWT({ ...payload })
|
||||||
.setProtectedHeader({ alg: 'HS256' })
|
.setProtectedHeader({ alg: 'HS256' })
|
||||||
@@ -38,10 +38,14 @@ export async function createSession(payload: SessionPayload): Promise<void> {
|
|||||||
.setExpirationTime(expiresAt)
|
.setExpirationTime(expiresAt)
|
||||||
.sign(getSecretKey())
|
.sign(getSecretKey())
|
||||||
|
|
||||||
|
// If the caller explicitly passes a value, honour it.
|
||||||
|
// Otherwise fall back to NODE_ENV (safe default for fully-HTTPS deploys).
|
||||||
|
const isSecure = secure !== undefined ? secure : process.env.NODE_ENV === 'production'
|
||||||
|
|
||||||
const cookieStore = await cookies()
|
const cookieStore = await cookies()
|
||||||
cookieStore.set(SESSION_COOKIE, token, {
|
cookieStore.set(SESSION_COOKIE, token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === 'production',
|
secure: isSecure,
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
expires: expiresAt,
|
expires: expiresAt,
|
||||||
path: '/',
|
path: '/',
|
||||||
|
|||||||
Reference in New Issue
Block a user