● Zion Boggan
repos/Stepping App/packages/api/src/context.ts
zionboggan.com ↗
25 lines · typescript
History for this file →
1
import { getDb, type Database } from "@stepping/db";
2
import { DEV_USER_ID } from "@stepping/config";
3
import { readSessionCookie, validateSession, type SessionUser } from "./auth";
4
 
5
export interface Context {
6
  db: Database;
7
  user: SessionUser | null;
8
}
9
 
10
export async function createContext({
11
  headers,
12
}: {
13
  headers: Headers;
14
}): Promise<Context> {
15
  const db = getDb();
16
 
17
  const token = readSessionCookie(headers.get("cookie"));
18
  let user = await validateSession(db, token);
19
 
20
  if (!user && process.env.STEPPING_DEV_AUTH === "1") {
21
    user = { id: DEV_USER_ID, role: "member" };
22
  }
23
 
24
  return { db, user };
25
}