SPB Git

Repository Public

route.ts 2.0 KB 36 lines typescript

1import { apiSession, clientCtx, json, loginEvent, revokeUserSessions } from "@/lib/auth";2import { sql } from "@/lib/db";3import { audit } from "@/lib/jobs";4import { handle } from "../_lib";56export const runtime = "nodejs";7export const dynamic = "force-dynamic";89/** Sessions actives de l'utilisateur. `id` = 16 premiers hex du token_hash (non secret, ne permet pas d'usurper la session). */10export async function GET(req: Request) {11  return handle(async () => {12    const s = await apiSession(req);13    if (s.error) return s.error;14    const rows = await sql<{ token_hash: string; created_at: Date; last_seen_at: Date; expires_at: Date; user_agent: string | null; passkey_label: string | null }[]>`15      SELECT s.token_hash, s.created_at, s.last_seen_at, s.expires_at, s.user_agent, p.label AS passkey_label16      FROM sessions s LEFT JOIN passkeys p ON p.id = s.passkey_id17      WHERE s.user_id = ${s.session.user.id} AND s.revoked_at IS NULL AND s.expires_at > now() AND s.last_seen_at > now() - interval '12 hours'18      ORDER BY s.last_seen_at DESC`;19    return json({ sessions: rows.map((r) => ({20      id: r.token_hash.slice(0, 16), current: r.token_hash === s.session.tokenHash, createdAt: r.created_at.toISOString(), lastSeenAt: r.last_seen_at.toISOString(),21      expiresAt: r.expires_at.toISOString(), userAgent: r.user_agent ?? "", passkeyLabel: r.passkey_label,22    })) });23  });24}25/** Révoque toutes les autres sessions (la session courante reste ouverte). */26export async function DELETE(req: Request) {27  return handle(async () => {28    const s = await apiSession(req);29    if (s.error) return s.error;30    const revoked = await revokeUserSessions(s.session.user.id, s.session.tokenHash);31    await loginEvent({ userId: s.session.user.id, kind: "revoke", ctx: clientCtx(req), detail: { what: "other_sessions", count: revoked } });32    await audit(s.session.user.id, "session.revoke_others", "user", s.session.user.id, { count: revoked });33    return json({ ok: true, revoked });34  });35}36
Files main
Menu

Theme