Feat: Signup endpoint for user registration

This commit is contained in:
2025-10-02 11:07:51 -07:00
parent 9c94f66753
commit 6c2b224326

17
functions/api/signup.js Normal file
View File

@@ -0,0 +1,17 @@
const json = (d, o) => new Response(JSON.stringify(d), { ...o, headers: { 'Content-Type': 'application/json', ...(o?.headers || {}) } });
export async function onRequestPost({ request, env }) {
try {
const { username, email, pass_hash } = await request.json();
if (!username || !email || !pass_hash) return json({ error: 'Missing fields' }, { status: 400 });
await env.D1_SPCHCAP.prepare(
'INSERT INTO users (username, email, pass_hash, ip_address) VALUES (?, ?, ?, ?)'
).bind(username, email, pass_hash, request.headers.get('CF-Connecting-IP')).run();
return json({ success: true }, { status: 201 });
} catch (e) {
const msg = e.message?.includes('UNIQUE') ? 'Username or email taken' : e.message;
return json({ error: { message: msg } }, { status: e.message?.includes('UNIQUE') ? 409 : 500 });
}
}