Graphical PDS migrator for AT Protocol
1/// <reference lib="deno.unstable" />
2
3import { App, staticFiles } from "fresh";
4import { define, type State } from "./utils.ts";
5import { getSession } from "./lib/sessions.ts";
6
7export const app = new App<State>()
8 .use(staticFiles())
9 .fsRoutes();
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 (
18 !needsAuth || url.pathname === "/login" || url.pathname.startsWith("/api/")
19 ) {
20 return ctx.next();
21 }
22
23 try {
24 const session = await getSession(ctx.req);
25
26 console.log("[auth] Session:", session);
27
28 const isAuthenticated = session !== null && session.did !== null;
29 ctx.state.auth = isAuthenticated;
30
31 if (!isAuthenticated) {
32 console.log("[auth] Authentication required but not authenticated");
33 return ctx.redirect("/login");
34 }
35
36 return ctx.next();
37 } catch (err) {
38 console.error("[auth] Middleware error:", err);
39 ctx.state.auth = false;
40 return ctx.redirect("/login");
41 }
42});
43app.use(authMiddleware);
44
45if (import.meta.main) {
46 await app.listen();
47}