sessionOf

tcmts.server.sessionOf · #7wvh57ju · term · runs on the server

uses #tcm/host (database)

The session the call's request names, if it's live.

import { SESSION_COOKIE } from "#codebase/tcmts/server";
import { database } from "#tcm/host";
import type { ServerCall } from "#tcm/host";

export function sessionOf(
  c: ServerCall,
): { id: string; data: Record<string, unknown> } | undefined {
  const id = c.request?.cookies[SESSION_COOKIE];
  if (!id) return undefined;
  const row = database("sessions")
    .prepare("SELECT data, expires FROM sessions WHERE id = ?")
    .get(id) as { data: string; expires: number } | undefined;
  if (!row || row.expires <= Date.now()) return undefined;
  return { id, data: JSON.parse(row.data) as Record<string, unknown> };
}

uses

used by