Initial commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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 });
|
||||
};
|
||||
Reference in New Issue
Block a user