Graphical PDS migrator for AT Protocol
1import { Agent } from "npm:@atproto/api";
2import { OauthSession, CredentialSession } from "./types.ts";
3import { getCredentialSession, getCredentialSessionAgent } from "./cred/sessions.ts";
4import { getOauthSession, getOauthSessionAgent } from "./oauth/sessions.ts";
5import { IronSession } from "npm:iron-session";
6
7export async function getSession(
8 req: Request,
9 res: Response = new Response(),
10 isMigration: boolean = false
11): Promise<IronSession<OauthSession | CredentialSession>> {
12 if (isMigration) {
13 return await getCredentialSession(req, res, true);
14 }
15 const oauthSession = await getOauthSession(req);
16 const credentialSession = await getCredentialSession(req, res);
17
18 if (oauthSession.did) {
19 console.log("Oauth session found")
20 return oauthSession;
21 }
22 if (credentialSession.did) {
23 return credentialSession;
24 }
25
26 throw new Error("No session found");
27}
28
29export async function getSessionAgent(
30 req: Request,
31 res: Response = new Response(),
32 isMigration: boolean = false
33): Promise<Agent | null> {
34 if (isMigration) {
35 return await getCredentialSessionAgent(req, res, isMigration);
36 }
37
38 const oauthAgent = await getOauthSessionAgent(req);
39 const credentialAgent = await getCredentialSessionAgent(req, res, isMigration);
40
41 if (oauthAgent) {
42 return oauthAgent;
43 }
44
45 if (credentialAgent) {
46 return credentialAgent;
47 }
48
49 return null;
50}
51
52export async function destroyAllSessions(req: Request) {
53 const oauthSession = await getOauthSession(req);
54 const credentialSession = await getCredentialSession(req);
55 const migrationSession = await getCredentialSession(req, new Response(), true);
56
57 if (oauthSession.did) {
58 oauthSession.destroy();
59 }
60 if (credentialSession.did) {
61 credentialSession.destroy();
62 }
63 if (migrationSession.did) {
64 migrationSession.destroy();
65 }
66}