19 lines
544 B
TypeScript
19 lines
544 B
TypeScript
import { db } from "./db.ts";
|
|
|
|
// Reads the user's code from an "Authorization: Bearer <code>" header
|
|
export async function authenticate(req: Request) {
|
|
const code = req.headers.get("Authorization")?.match(/^Bearer (\S+)$/)?.[1];
|
|
if (!code) {
|
|
return undefined;
|
|
}
|
|
return await db.get_entry_by_index("users", "code", code);
|
|
}
|
|
|
|
export function unauthorized() {
|
|
return Response.json({ error: "Invalid code" }, { status: 401 });
|
|
}
|
|
|
|
export function forbidden() {
|
|
return Response.json({ error: "Only admins can do this" }, { status: 403 });
|
|
}
|