Graphical PDS migrator for AT Protocol

fix middleware

Changed files
+74 -20
routes
api
+33 -7
main.ts
···
// this can also be defined via a file. feel free to delete this!
const authMiddleware = define.middleware(async (ctx) => {
const url = new URL(ctx.req.url);
-
if (url.pathname.startsWith("/migrate")) {
-
ctx.state.auth = true
+
const needsAuth = url.pathname.startsWith("/migrate");
+
+
// Skip auth check for login page and API endpoints
+
if (url.pathname === "/login" || url.pathname.startsWith("/api/")) {
+
return ctx.next();
}
-
if (ctx.state.auth) {
+
+
try {
const me = await fetch(`${url.origin}/api/me`, {
credentials: "include",
+
headers: {
+
"Cookie": ctx.req.headers.get("cookie") || ""
+
}
});
+
+
console.log("[auth] /api/me response:", {
+
status: me.status,
+
statusText: me.statusText,
+
headers: Object.fromEntries(me.headers.entries())
+
});
+
const json = await me.json();
-
if (json && typeof json === 'object' && json.did) {
-
return ctx.next();
-
} else {
+
console.log("[auth] /api/me response data:", json);
+
+
const isAuthenticated = json && typeof json === 'object' && json.did;
+
ctx.state.auth = isAuthenticated;
+
+
if (needsAuth && !isAuthenticated) {
+
console.log("[auth] Authentication required but not authenticated");
return ctx.redirect("/login");
}
+
+
return ctx.next();
+
} catch (err) {
+
console.error("[auth] Middleware error:", err);
+
ctx.state.auth = false;
+
if (needsAuth) {
+
return ctx.redirect("/login");
+
}
+
return ctx.next();
}
-
return ctx.next();
});
app.use(authMiddleware);
+41 -13
routes/api/me.ts
···
const req = ctx.req;
const res = new Response();
-
console.log("Cookies:", req.headers.get("cookie"));
+
try {
+
console.log("[/api/me] Request headers:", Object.fromEntries(req.headers.entries()));
-
const agent = await getSessionAgent(req, res);
-
if (!agent) {
-
console.log("No agent found");
-
return Response.json(null);
-
}
+
const agent = await getSessionAgent(req, res);
+
if (!agent) {
+
console.log("[/api/me] No agent found");
+
return new Response(JSON.stringify(null), {
+
status: 200,
+
headers: {
+
"Content-Type": "application/json",
+
"X-Response-Type": "null"
+
}
+
});
+
}
-
try {
-
console.log("Got agent, checking authentication");
+
console.log("[/api/me] Got agent, checking authentication");
const session = await agent.com.atproto.server.getSession();
-
console.log("Session info:", {
+
console.log("[/api/me] Session info:", {
did: session.data.did,
handle: session.data.handle
});
const handle = await resolver.resolveDidToHandle(session.data.did);
-
console.log("Resolved handle:", handle);
+
console.log("[/api/me] Resolved handle:", handle);
-
return Response.json({
+
const responseData = {
did: session.data.did,
handle
+
};
+
+
return new Response(JSON.stringify(responseData), {
+
status: 200,
+
headers: {
+
"Content-Type": "application/json",
+
"X-Response-Type": "user"
+
}
});
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
-
console.error({ err: message }, "Failed to fetch profile");
-
return Response.json(null);
+
console.error("[/api/me] Error:", {
+
error: message,
+
stack: err instanceof Error ? err.stack : undefined,
+
url: req.url,
+
method: req.method,
+
headers: Object.fromEntries(req.headers.entries())
+
});
+
+
return new Response(JSON.stringify(null), {
+
status: 200,
+
headers: {
+
"Content-Type": "application/json",
+
"X-Response-Type": "error",
+
"X-Error-Message": encodeURIComponent(message)
+
}
+
});
}
},
});