Graphical PDS migrator for AT Protocol

fix logout

Changed files
+27 -17
routes
+4 -7
main.ts
···
const url = new URL(ctx.req.url);
const needsAuth = url.pathname.startsWith("/migrate");
-
// Skip auth check for login page and API endpoints
-
if (url.pathname === "/login" || url.pathname.startsWith("/api/")) {
+
// Skip auth check if not a protected route
+
if (!needsAuth || url.pathname === "/login" || url.pathname.startsWith("/api/")) {
return ctx.next();
}
···
const isAuthenticated = json && typeof json === 'object' && json.did;
ctx.state.auth = isAuthenticated;
-
if (needsAuth && !isAuthenticated) {
+
if (!isAuthenticated) {
console.log("[auth] Authentication required but not authenticated");
return ctx.redirect("/login");
}
···
} catch (err) {
console.error("[auth] Middleware error:", err);
ctx.state.auth = false;
-
if (needsAuth) {
-
return ctx.redirect("/login");
-
}
-
return ctx.next();
+
return ctx.redirect("/login");
}
});
app.use(authMiddleware);
+22 -3
routes/api/logout.ts
···
-
import { destroyAllSessions } from "../../lib/sessions.ts";
+
import { getSession } from "../../lib/sessions.ts";
+
import { oauthClient } from "../../lib/oauth/client.ts";
import { define } from "../../utils.ts";
export const handler = define.handlers({
async POST(ctx) {
-
await destroyAllSessions(ctx.req)
+
const req = ctx.req;
+
+
try {
+
const response = new Response(null, { status: 200 });
+
const session = await getSession(req, response);
+
+
if (session.did) {
+
// Try to revoke both types of sessions - the one that doesn't exist will just no-op
+
await Promise.all([
+
oauthClient.revoke(session.did).catch(console.error)
+
]);
+
// Then destroy the iron session
+
session.destroy();
+
}
-
return new Response("All Sessions Destroyed")
+
return response;
+
} catch (error: unknown) {
+
const err = error instanceof Error ? error : new Error(String(error));
+
console.error("Logout failed:", err.message);
+
return new Response("Logout failed", { status: 500 });
+
}
},
});
+1 -7
routes/api/me.ts
···
});
}
-
console.log("[/api/me] Got agent, checking authentication");
const session = await agent.com.atproto.server.getSession();
-
console.log("[/api/me] Session info:", {
-
did: session.data.did,
-
handle: session.data.handle
-
});
const handle = await resolver.resolveDidToHandle(session.data.did);
-
console.log("[/api/me] Resolved handle:", handle);
const responseData = {
did: session.data.did,
···
});
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
-
console.error("[/api/me] Error:", {
+
console.error("[/api/me] Error:", {
error: message,
stack: err instanceof Error ? err.stack : undefined,
url: req.url,