32 lines
928 B
TypeScript
32 lines
928 B
TypeScript
import type { 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]
|
|
}
|
|
|
|
export function setSessionCookie(res: Response, actorType: ActorType, token: string, maxAgeMs?: number) {
|
|
res.cookie(getSessionCookieName(actorType), token, {
|
|
httpOnly: true,
|
|
secure: process.env.NODE_ENV === 'production',
|
|
sameSite: actorType === 'admin' ? 'strict' : 'lax',
|
|
path: '/',
|
|
maxAge: maxAgeMs,
|
|
})
|
|
}
|
|
|
|
export function clearSessionCookie(res: Response, actorType: ActorType) {
|
|
res.clearCookie(getSessionCookieName(actorType), {
|
|
httpOnly: true,
|
|
secure: process.env.NODE_ENV === 'production',
|
|
sameSite: actorType === 'admin' ? 'strict' : 'lax',
|
|
path: '/',
|
|
})
|
|
}
|