馃 distributed transcription service
thistle.dunkirk.sh
1// Helper functions for route authentication and error handling
2
3import type { User } from "./auth";
4import { getSessionFromRequest, getUserBySession } from "./auth";
5import { AuthErrors } from "./errors";
6
7export interface AuthenticatedRequest extends Request {
8 user: User;
9}
10
11export function requireAuth(req: Request): User {
12 const sessionId = getSessionFromRequest(req);
13 if (!sessionId) {
14 throw AuthErrors.required();
15 }
16
17 const user = getUserBySession(sessionId);
18 if (!user) {
19 throw AuthErrors.invalidSession();
20 }
21
22 return user;
23}