Graphical PDS migrator for AT Protocol
1/// <reference lib="deno.unstable" /> 2 3import { App, fsRoutes, staticFiles } from "fresh"; 4import { define, type State } from "./utils.ts"; 5import { getSession } from "./lib/sessions.ts"; 6 7export const app = new App<State>(); 8 9app.use(staticFiles()); 10 11// this can also be defined via a file. feel free to delete this! 12const authMiddleware = define.middleware(async (ctx) => { 13 const url = new URL(ctx.req.url); 14 const needsAuth = url.pathname.startsWith("/migrate"); 15 16 // Skip auth check if not a protected route 17 if (!needsAuth || url.pathname === "/login" || url.pathname.startsWith("/api/")) { 18 return ctx.next(); 19 } 20 21 try { 22 const session = await getSession(ctx.req) 23 24 console.log("[auth] Session:", session); 25 26 const isAuthenticated = session !== null && session.did !== null; 27 ctx.state.auth = isAuthenticated; 28 29 if (!isAuthenticated) { 30 console.log("[auth] Authentication required but not authenticated"); 31 return ctx.redirect("/login"); 32 } 33 34 return ctx.next(); 35 } catch (err) { 36 console.error("[auth] Middleware error:", err); 37 ctx.state.auth = false; 38 return ctx.redirect("/login"); 39 } 40}); 41app.use(authMiddleware); 42 43await fsRoutes(app, { 44 loadIsland: (path) => import(`./islands/${path}`), 45 loadRoute: (path) => import(`./routes/${path}`), 46}); 47 48if (import.meta.main) { 49 await app.listen(); 50}