import { createOrGetWorkOSUser, sendMagicAuthCode } from '@/lib/auth/workosAuth'; import { validateEmail } from '@/lib/unescoFormHelper'; import type { APIRoute } from 'astro'; export const POST: APIRoute = async ({ request }) => { const headers = { 'Content-Type': 'application/json' }; try { const { email } = await request.json(); if (!email || !validateEmail(email)) { return new Response( JSON.stringify({ success: false, message: "I think there's a typo in your email address 😅", }), { status: 400, headers } ); } const { userId: workosUserId } = await createOrGetWorkOSUser(email); if (!workosUserId) { return new Response( JSON.stringify({ success: false, message: 'WorkOS says they failed ("WorkOS System Error"). Text me on Instagram (@palacejunwon), then I\'ll contact WorkOS to fix this problem!', }), { status: 500, headers } ); } const { error } = await sendMagicAuthCode(email); if (error) { return new Response( JSON.stringify({ success: false, message: 'WorkOS says they failed ("Magic Code Error"). Text me on Instagram (@palacejunwon), then I\'ll contact WorkOS to fix this problem!', }), { status: 500, headers } ); } return new Response( JSON.stringify({ success: true, message: 'Verification code was sent to your email', workosUserId, }), { status: 200, headers } ); } catch (error) { return new Response( JSON.stringify({ success: false, message: 'WorkOS says they failed ("Email Error"). Text me on Instagram (@palacejunwon), then I\'ll contact WorkOS to fix this problem!', }), { status: 500, headers } ); } };