Files
devodytyji/src/pages/api/auth/register.ts
T
2026-08-14 08:51:55 +03:00

39 lines
1.0 KiB
TypeScript

import type { APIRoute } from 'astro';
export const prerender = false;
const json = (body: unknown, status = 200) =>
new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
});
export const POST: APIRoute = async ({ request, locals }) => {
const supabase = locals.supabase;
let body: { email?: string; password?: string; fullName?: string };
try {
body = await request.json();
} catch {
return json({ error: 'bad_request' }, 400);
}
const { email, password, fullName } = body;
if (!email || !password) {
return json({ error: 'bad_request' }, 400);
}
const { data, error } = await supabase.auth.signUp({
email,
password,
options: { data: { full_name: fullName || null } },
});
if (error) {
const message = error.message?.toLowerCase() ?? '';
const status = message.includes('already') ? 409 : 400;
return json({ error: message.includes('already') ? 'email_exists' : 'generic' }, status);
}
return json({ ok: true, needsConfirmation: !data.session });
};