File
Blob: src/worker/auth/cookies.ts
| 1 | import { deleteCookie, getCookie, setCookie } from "hono/cookie"; |
| 2 | |
| 3 | import type { AppContext } from "@/worker/hono"; |
| 4 | |
| 5 | export const SESSION_COOKIE_NAME = "anvil_session"; |
| 6 | export const SESSION_COOKIE_WIRE_NAME = "__Host-anvil_session"; |
| 7 | const SESSION_COOKIE_PREFIX = "host" as const; |
| 8 | |
| 9 | const SESSION_COOKIE_OPTIONS = { |
| 10 | path: "/" as const, |
| 11 | httpOnly: true, |
| 12 | secure: true, |
| 13 | sameSite: "Lax" as const, |
| 14 | prefix: SESSION_COOKIE_PREFIX, |
| 15 | }; |
| 16 | |
| 17 | export const readSessionCookie = (c: AppContext): string | null => |
| 18 | getCookie(c, SESSION_COOKIE_NAME, SESSION_COOKIE_PREFIX) ?? null; |
| 19 | |
| 20 | export const setSessionCookie = (c: AppContext, sessionId: string): void => { |
| 21 | setCookie(c, SESSION_COOKIE_NAME, sessionId, { |
| 22 | ...SESSION_COOKIE_OPTIONS, |
| 23 | maxAge: Number(c.env.AUTH_SESSION_TTL_SECONDS), |
| 24 | }); |
| 25 | }; |
| 26 | |
| 27 | export const clearSessionCookie = (c: AppContext): void => { |
| 28 | deleteCookie(c, SESSION_COOKIE_NAME, { |
| 29 | path: SESSION_COOKIE_OPTIONS.path, |
| 30 | secure: SESSION_COOKIE_OPTIONS.secure, |
| 31 | prefix: SESSION_COOKIE_PREFIX, |
| 32 | }); |
| 33 | }; |