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"; 5 6export const app = new App<State>(); 7 8app.use(staticFiles()); 9 10// this can also be defined via a file. feel free to delete this! 11const authMiddleware = define.middleware(async (ctx) => { 12 const url = new URL(ctx.req.url); 13 const needsAuth = url.pathname.startsWith("/migrate"); 14 15 // Skip auth check if not a protected route 16 if (!needsAuth || url.pathname === "/login" || url.pathname.startsWith("/api/")) { 17 return ctx.next(); 18 } 19 20 try { 21 const me = await fetch(`${url.origin}/api/me`, { 22 credentials: "include", 23 headers: { 24 "Cookie": ctx.req.headers.get("cookie") || "" 25 } 26 }); 27 28 console.log("[auth] /api/me response:", { 29 status: me.status, 30 statusText: me.statusText, 31 headers: Object.fromEntries(me.headers.entries()) 32 }); 33 34 const json = await me.json(); 35 console.log("[auth] /api/me response data:", json); 36 37 const isAuthenticated = json && typeof json === 'object' && json.did; 38 ctx.state.auth = isAuthenticated; 39 40 if (!isAuthenticated) { 41 console.log("[auth] Authentication required but not authenticated"); 42 return ctx.redirect("/login"); 43 } 44 45 return ctx.next(); 46 } catch (err) { 47 console.error("[auth] Middleware error:", err); 48 ctx.state.auth = false; 49 return ctx.redirect("/login"); 50 } 51}); 52app.use(authMiddleware); 53 54await fsRoutes(app, { 55 loadIsland: (path) => import(`./islands/${path}`), 56 loadRoute: (path) => import(`./routes/${path}`), 57}); 58 59if (import.meta.main) { 60 await app.listen(); 61}