···
import { App, fsRoutes, staticFiles } from "fresh";
import { define, type State } from "./utils.ts";
5
+
import { getSession } from "./lib/sessions.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/")) {
21
-
const me = await fetch(`${url.origin}/api/me`, {
22
-
credentials: "include",
24
-
"Cookie": ctx.req.headers.get("cookie") || ""
28
-
console.log("[auth] /api/me response:", {
30
-
statusText: me.statusText,
31
-
headers: Object.fromEntries(me.headers.entries())
22
+
const session = await getSession(ctx.req)
34
-
const json = await me.json();
35
-
console.log("[auth] /api/me response data:", json);
24
+
console.log("[auth] Session:", session);
37
-
const isAuthenticated = json && typeof json === 'object' && json.did;
26
+
const isAuthenticated = session !== null && session.did !== null;
ctx.state.auth = isAuthenticated;
console.log("[auth] Authentication required but not authenticated");
return ctx.redirect("/login");