mirror of
https://github.com/Nezumi-2711/uptime-monitoring.git
synced 2026-09-22 20:02:21 +00:00
23 lines
713 B
TypeScript
23 lines
713 B
TypeScript
import { getCookie } from "hono/cookie";
|
|
import { createMiddleware } from "hono/factory";
|
|
import { getDb } from "../db/client";
|
|
import { hasValidSession, SESSION_COOKIE } from "./session";
|
|
|
|
export type AuthVariables = {
|
|
authenticated: true;
|
|
};
|
|
|
|
export const requireAuth = createMiddleware<{
|
|
Bindings: Env;
|
|
Variables: AuthVariables;
|
|
}>(async (context, next) => {
|
|
const token = getCookie(context, SESSION_COOKIE);
|
|
if (!token) return context.json({ message: "Authentication required" }, 401);
|
|
|
|
const authenticated = await hasValidSession(getDb(context.env), token);
|
|
if (!authenticated) return context.json({ message: "Authentication required" }, 401);
|
|
|
|
context.set("authenticated", true);
|
|
await next();
|
|
});
|