also looking at this
Deploy: uniform auth session model (main → production)
#7
282 files
+3028
-64856
| @@ -0,0 +1,57 @@ | ||
| 1 | import process from 'node:process' | |
| 2 | import { config } from '@stacksjs/config' | |
| 3 | ||
| 4 | /** | |
| 5 | * Build the `Set-Cookie` header value that mirrors an issued bearer token | |
| 6 | * into an HttpOnly cookie. The SPA's `useAuth` composable only stores the | |
| 7 | * bearer in localStorage, which is invisible during server-side rendering — | |
| 8 | * the dashboard's `.stx` pages (rendered directly from SQLite, no client | |
| 9 | * hydration) resolve "who's logged in" from this cookie instead. | |
| 10 | * | |
| 11 | * Shared by every auth action that mints a session (LoginAction, | |
| 12 | * RegisterAction, VerifyTwoFactorLoginAction) so the cookie contract stays | |
| 13 | * identical across all three entry points into an authenticated session. | |
| 14 | */ | |
| 15 | export function buildAuthCookie(token: string, expiresInSeconds?: number): string { | |
| 16 | const name = config.auth?.defaultTokenName || 'auth-token' | |
| 17 | const maxAge = Math.max(1, Math.floor(expiresInSeconds ?? (config.auth?.tokenExpiry ?? 60 * 60 * 1000) / 1000)) | |
| 18 | const env = (process.env.APP_ENV ?? process.env.NODE_ENV ?? '').toLowerCase() | |
| 19 | const isLocal = env === '' || env === 'local' || env === 'development' || env === 'dev' || env === 'test' || env === 'testing' | |
| 20 | ||
| 21 | const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`] | |
| 22 | if (!isLocal) | |
| 23 | parts.push('Secure') | |
| 24 | ||
| 25 | return parts.join('; ') | |
| 26 | } | |
| 27 | ||
| 28 | /** | |
| 29 | * Clear the auth cookie {@link buildAuthCookie} sets — used by LogoutAction. | |
| 30 | */ | |
| 31 | export function clearAuthCookie(): string { | |
| 32 | const name = config.auth?.defaultTokenName || 'auth-token' | |
| 33 | return `${name}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0` | |
| 34 | } | |
| 35 | ||
| 36 | /** Baseline session: a week. */ | |
| 37 | const WEEK_MINUTES = 7 * 24 * 60 | |
| 38 | /** "Keep me signed in": a month. */ | |
| 39 | const MONTH_MINUTES = 30 * 24 * 60 | |
| 40 | ||
| 41 | /** | |
| 42 | * Session length for a fresh login, in minutes, from the login form's | |
| 43 | * "remember me" checkbox. Passed to `Auth.loginUsingId(id, { expiresInMinutes })`, | |
| 44 | * which stamps BOTH the `oauth_access_tokens.expires_at` row and (via the | |
| 45 | * returned `expiresIn`) the cookie's Max-Age from the same number — so the | |
| 46 | * whole session, not just the cookie, honours the tier. Nothing extends either | |
| 47 | * value afterwards (getUserFromToken leaves expires_at alone), so this is the | |
| 48 | * real session cap. | |
| 49 | * | |
| 50 | * Unchecked is the baseline week; checked is 30 days. Accepts whatever the JSON | |
| 51 | * body carries for `remember` — boolean true, or '1'/'true'/'on'/'yes'. | |
| 52 | */ | |
| 53 | export function sessionExpiryMinutes(remember: unknown): number { | |
| 54 | const on = remember === true | |
| 55 | || ['1', 'true', 'on', 'yes'].includes(String(remember ?? '').toLowerCase()) | |
| 56 | return on ? MONTH_MINUTES : WEEK_MINUTES | |
| 57 | } | |