Graphical PDS migrator for AT Protocol
1import { isValidHandle } from "npm:@atproto/syntax";
2import { oauthClient, scope } from "../../../lib/oauth/client.ts";
3import { define } from "../../../utils.ts";
4
5function isValidUrl(url: string): boolean {
6 try {
7 const urlp = new URL(url);
8 // http or https
9 return urlp.protocol === "http:" || urlp.protocol === "https:";
10 } catch {
11 return false;
12 }
13}
14
15export const handler = define.handlers({
16 async POST(ctx) {
17 const data = await ctx.req.json();
18 const handle = data.handle;
19 if (
20 typeof handle !== "string" ||
21 !(isValidHandle(handle) || isValidUrl(handle) ||
22 handle.startsWith("did:"))
23 ) {
24 return new Response("Invalid Handle", { status: 400 });
25 }
26
27 // Initiate the OAuth flow
28 try {
29 const url = await oauthClient.authorize(handle, {
30 scope,
31 });
32 return Response.json({ redirectUrl: url.toString() });
33 } catch (err) {
34 console.error({ err }, "oauth authorize failed");
35 return new Response("Couldn't initiate login", { status: 500 });
36 }
37 },
38});