Graphical PDS migrator for AT Protocol
1import { isValidHandle } from 'npm:@atproto/syntax'
2import { oauthClient } 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 ) {
23 return new Response("Invalid Handle", {status: 400})
24 }
25
26 // Initiate the OAuth flow
27 try {
28 const url = await oauthClient.authorize(handle, {
29 scope: 'atproto transition:generic transition:chat.bsky',
30 })
31 return Response.json({ redirectUrl: url.toString() })
32 } catch (err) {
33 console.error({ err }, 'oauth authorize failed')
34 return new Response("Couldn't initiate login", {status: 500})
35 }
36 },
37});