A minimal starter for ATProto logins in Astro
at main 938 B view raw
1import type { APIRoute } from 'astro' 2import { getOAuthClient } from '../../../lib/context' 3import { getSession } from '../../../lib/session' 4 5export const GET: APIRoute = async (context) => { 6 try { 7 const oauthClient = getOAuthClient(context.cookies) 8 const url = new URL(context.request.url) 9 const params = new URLSearchParams(url.search) 10 11 const session = getSession(context.cookies) 12 13 if (session.did) { 14 try { 15 const oauthSession = await oauthClient.restore(session.did) 16 if (oauthSession) await oauthSession.signOut() 17 } catch (err) { 18 console.warn('OAuth restore failed during callback:', err) 19 } 20 } 21 22 const oauth = await oauthClient.callback(params) 23 session.did = oauth.session.did 24 await session.save() 25 26 return context.redirect('/') 27 } catch (err) { 28 console.error('OAuth callback failed:', err) 29 return context.redirect('/?error=login_failed') 30 } 31}