A minimal starter for ATProto logins in Astro
1import type { APIRoute } from 'astro'
2import { getOAuthClient } from '../../lib/context'
3
4export const POST: APIRoute = async ({ request, cookies, redirect }) => {
5 try {
6 const oauthClient = getOAuthClient(cookies)
7 const formData = await request.formData()
8 const handle = formData.get('handle')
9
10 if (!handle || typeof handle !== 'string') {
11 return new Response('Invalid handle', { status: 400 })
12 }
13
14 const url = await oauthClient.authorize(handle, {
15 scope: 'atproto transition:generic',
16 })
17
18 return redirect(url.toString())
19 } catch (err) {
20 console.error('OAuth authorize failed:', err)
21 const error = err instanceof Error ? err.message : 'unexpected error'
22 return new Response(`Login failed: ${error}`, { status: 500 })
23 }
24}