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 7/** 8 * Get the session for the given request. 9 * @param req - The request object 10 * @param res - The response object 11 * @param isMigration - Whether to get the migration session 12 * @returns The session 13 */ 14export async function getSession( 15 req: Request, 16 res: Response = new Response(), 17 isMigration: boolean = false 18): Promise<IronSession<OauthSession | CredentialSession>> { 19 if (isMigration) { 20 return await getCredentialSession(req, res, true); 21 } 22 const oauthSession = await getOauthSession(req); 23 const credentialSession = await getCredentialSession(req, res); 24 25 if (oauthSession.did) { 26 console.log("Oauth session found") 27 return oauthSession; 28 } 29 if (credentialSession.did) { 30 return credentialSession; 31 } 32 33 throw new Error("No session found"); 34} 35 36/** 37 * Get the session agent for the given request. 38 * @param req - The request object 39 * @param res - The response object 40 * @param isMigration - Whether to get the migration session 41 * @returns The session agent 42 */ 43export async function getSessionAgent( 44 req: Request, 45 res: Response = new Response(), 46 isMigration: boolean = false 47): Promise<Agent | null> { 48 if (isMigration) { 49 return await getCredentialSessionAgent(req, res, isMigration); 50 } 51 52 const oauthAgent = await getOauthSessionAgent(req); 53 const credentialAgent = await getCredentialSessionAgent(req, res, isMigration); 54 55 if (oauthAgent) { 56 return oauthAgent; 57 } 58 59 if (credentialAgent) { 60 return credentialAgent; 61 } 62 63 return null; 64} 65 66/** 67 * Destroy all sessions for the given request. 68 * @param req - The request object 69 */ 70export async function destroyAllSessions(req: Request) { 71 const oauthSession = await getOauthSession(req); 72 const credentialSession = await getCredentialSession(req); 73 const migrationSession = await getCredentialSession(req, new Response(), true); 74 75 if (oauthSession.did) { 76 oauthSession.destroy(); 77 } 78 if (credentialSession.did) { 79 credentialSession.destroy(); 80 } 81 if (migrationSession.did) { 82 migrationSession.destroy(); 83 } 84}