import { App, fsRoutes, staticFiles } from "fresh";
import { define, type State } from "./utils.ts";
export const app = new App<State>();
const authMiddleware = define.middleware(async (ctx) => {
const url = new URL(ctx.req.url);
const needsAuth = url.pathname.startsWith("/migrate");
// Skip auth check if not a protected route
if (!needsAuth || url.pathname === "/login" || url.pathname.startsWith("/api/")) {
-
const me = await fetch(`${url.origin}/api/me`, {
-
"Cookie": ctx.req.headers.get("cookie") || ""
-
console.log("[auth] /api/me response:", {
-
statusText: me.statusText,
-
headers: Object.fromEntries(me.headers.entries())
-
const json = await me.json();
-
console.log("[auth] /api/me response data:", json);
-
const isAuthenticated = json && typeof json === 'object' && json.did;
ctx.state.auth = isAuthenticated;
console.log("[auth] Authentication required but not authenticated");
return ctx.redirect("/login");