auth/session_queries.ts view source
number Extend session when it has less than this remaining (1 day in ms).
Auth session database queries.
Server-side sessions keyed by blake3 hash of the session token. The cookie contains the raw token; the database stores only the hash.
15 declarations
auth/session_queries.ts view source
number Extend session when it has less than this remaining (1 day in ms).
auth/session_queries.ts view source
number Session lifetime in milliseconds (30 days).
auth/session_queries.ts view source
(): string Generate a cryptographically random session token.
string a 32-byte base64url-encoded token
auth/session_queries.ts view source
(token: string): string Hash a session token to its storage key using blake3.
tokenthe raw session token
stringstring hex-encoded blake3 hash
auth/session_queries.ts view source
(deps: QueryDeps, token_hash: string, account_id: string, expires_at: Date): Promise<void> Create a new auth session.
depsquery dependencies
token_hashblake3 hash of the session token (use hash_session_token)
stringaccount_idthe account this session belongs to
stringexpires_atwhen the session expires
DatePromise<void> auth/session_queries.ts view source
(deps: QueryDeps): Promise<number> Delete expired sessions.
depsPromise<number> the number of sessions cleaned up
auth/session_queries.ts view source
(deps: QueryDeps, account_id: string, max_sessions: number): Promise<number> Enforce a per-account session limit by evicting the oldest sessions.
Keeps the newest max_sessions sessions and deletes the rest.
Race safety: this function must run inside a transaction alongside the
INSERT that created the new session. All callers satisfy this requirement:
- POST /login and POST /tokens/create use the default transaction: true
(framework-managed transaction wrapping in apply_route_specs)
- POST /bootstrap and POST /signup manage their own transactions
and pass the transaction-scoped deps to create_session_and_set_cookie
The transaction ensures the INSERT + enforce_limit pair is atomic — concurrent session creation cannot interleave between the two statements.
depsquery dependencies (must be transaction-scoped)
account_idthe account to enforce the limit for
stringmax_sessionsmaximum number of sessions to keep
numberPromise<number> the number of sessions evicted
auth/session_queries.ts view source
(deps: QueryDeps, token_hash: string): Promise<AuthSession | undefined> Get a session if it exists, is not expired, and has not been revoked.
depsquery dependencies
token_hashblake3 hash of the session token
stringPromise<AuthSession | undefined> auth/session_queries.ts view source
(deps: QueryDeps, limit?: number): Promise<(AuthSession & { username: string; })[]> List all active sessions across all accounts with usernames.
depsquery dependencies
limitmaximum entries to return
number200Promise<(AuthSession & { username: string; })[]> active sessions joined with account usernames, newest activity first
auth/session_queries.ts view source
(deps: QueryDeps, account_id: string, limit?: number): Promise<AuthSession[]> List sessions for an account, newest first.
depsaccount_idstringlimitnumber50Promise<AuthSession[]> auth/session_queries.ts view source
(deps: QueryDeps, account_id: string): Promise<number> Revoke all sessions for an account.
depsaccount_idstringPromise<number> the number of sessions revoked
auth/session_queries.ts view source
(deps: QueryDeps, token_hash: string): Promise<void> Revoke (delete) a session by its token hash.
No account_id constraint — caller must ensure the hash comes from a trusted source (e.g. the authenticated session cookie). For user-facing revocation of a specific session by ID, prefer query_session_revoke_for_account which includes an IDOR guard.
depstoken_hashstringPromise<void> auth/session_queries.ts view source
(deps: QueryDeps, token_hash: string, account_id: string): Promise<boolean> Revoke a session only if it belongs to the specified account.
Prevents cross-account session revocation.
depsquery dependencies
token_hashblake3 hash of the session token
stringaccount_idthe account that must own the session
stringPromise<boolean> true if a session was revoked, false if not found or wrong account
auth/session_queries.ts view source
(deps: QueryDeps, token_hash: string): Promise<void> Update last_seen_at and optionally extend expiry for a session.
Extends if less than AUTH_SESSION_EXTEND_THRESHOLD_MS remaining.
depsquery dependencies
token_hashblake3 hash of the session token
stringPromise<void> auth/session_queries.ts view source
(deps: QueryDeps, token_hash: string, pending_effects: Promise<void>[] | undefined, log: Logger): Promise<void> Touch a session without blocking the caller.
Errors are logged to console — session touching never breaks request flows.
Pass pending_effects (from c.var.pending_effects) to register
the promise for test flushing.
depsquery dependencies
token_hashblake3 hash of the session token
stringpending_effectsoptional array to register the effect for later awaiting
Promise<void>[] | undefinedlogthe logger instance
LoggerPromise<void> the settled promise (callers may ignore it — fire-and-forget semantics preserved)