import { Context, Next } from "hono"; import { Env, Variables } from "../types"; export async function authMiddleware( c: Context<{ Bindings: Env; Variables: Variables }>, next: Next, ) { if (!c.env.AUTH_TOKEN) { return c.json({ success: false, error: "Authentication token is not specified in worker secrets.", }, 500); } const authToken = c.req.header("Authorization"); if (!authToken || !authToken.startsWith("Bearer ")) { return c.json( { success: false, error: "Authentication token missing or malformed. Expected 'Bearer '.", }, 401, { "WWW-Authenticate": "Bearer", }, ); } const token = authToken.split(" ")[1]; if (token !== c.env.AUTH_TOKEN) { return c.json({ success: false, error: "Authentication token provided is invalid.", }, 401); } await next(); }