Files
carmanagement/apps/api/src/security/sessionCookies.ts
T
root ad5f5ebab7
Build & Deploy / Build & Push Docker Image (push) Successful in 2m49s
Test / Type Check (all packages) (push) Successful in 55s
Build & Deploy / Deploy to VPS (push) Successful in 3s
Test / API Unit Tests (push) Successful in 48s
Test / Homepage Unit Tests (push) Successful in 46s
Test / Storefront Unit Tests (push) Successful in 47s
Test / Admin Unit Tests (push) Successful in 40s
Test / Dashboard Unit Tests (push) Successful in 40s
Test / API Integration Tests (push) Successful in 59s
fix(api): support shared session cookie domain
2026-07-02 15:10:40 -04:00

34 lines
1.1 KiB
TypeScript

import type { CookieOptions, Response } from 'express'
import type { ActorType } from './tokens'
const COOKIE_NAMES: Record<ActorType, string> = {
admin: 'admin_session',
employee: 'employee_session',
renter: 'renter_session',
}
export function getSessionCookieName(actorType: ActorType) {
return COOKIE_NAMES[actorType]
}
function getSessionCookieOptions(actorType: ActorType, maxAgeMs?: number): CookieOptions {
const domain = process.env.SESSION_COOKIE_DOMAIN?.trim()
return {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: actorType === 'admin' ? 'strict' : 'lax',
path: '/',
...(domain ? { domain } : {}),
...(maxAgeMs !== undefined ? { maxAge: maxAgeMs } : {}),
}
}
export function setSessionCookie(res: Response, actorType: ActorType, token: string, maxAgeMs?: number) {
res.cookie(getSessionCookieName(actorType), token, getSessionCookieOptions(actorType, maxAgeMs))
}
export function clearSessionCookie(res: Response, actorType: ActorType) {
res.clearCookie(getSessionCookieName(actorType), getSessionCookieOptions(actorType))
}