@@ -7,14 +7,19 @@ const supabase = createClient(
77
88export async function POST ( req ) {
99 try {
10- const { email, password, captchaToken, action } = await req . json ( )
10+ // Parse JSON body safely
11+ const body = await req . json ( )
12+ const { email, password, captchaToken, action } = body || { }
1113
12- if ( ! email || ! password )
13- return new Response ( JSON . stringify ( { message : 'Email and password required' } ) , { status : 400 } )
14+ // Validate required fields
15+ if ( ! email || ! password ) {
16+ return new Response ( JSON . stringify ( { success : false , message : 'Email and password are required' } ) , { status : 400 } )
17+ }
1418
1519 if ( action === 'signup' ) {
16- if ( ! captchaToken )
17- return new Response ( JSON . stringify ( { message : 'Captcha token missing' } ) , { status : 400 } )
20+ if ( ! captchaToken ) {
21+ return new Response ( JSON . stringify ( { success : false , message : 'Captcha token missing' } ) , { status : 400 } )
22+ }
1823
1924 // Verify Turnstile token
2025 const verifyRes = await fetch ( 'https://challenges.cloudflare.com/turnstile/v0/siteverify' , {
@@ -26,27 +31,32 @@ export async function POST(req) {
2631 } ) ,
2732 } )
2833
29- const data = await verifyRes . json ( )
30- if ( ! data . success )
31- return new Response ( JSON . stringify ( { message : 'Captcha verification failed' } ) , { status : 400 } )
34+ const verifyData = await verifyRes . json ( )
35+ if ( ! verifyData . success ) {
36+ return new Response ( JSON . stringify ( { success : false , message : 'Captcha verification failed' } ) , { status : 400 } )
37+ }
3238
3339 // Create Supabase user
3440 const { user, error } = await supabase . auth . admin . createUser ( { email, password } )
35- if ( error )
36- return new Response ( JSON . stringify ( { message : error . message } ) , { status : 400 } )
41+ if ( error ) {
42+ return new Response ( JSON . stringify ( { success : false , message : error . message } ) , { status : 400 } )
43+ }
3744
38- return new Response ( JSON . stringify ( { message : 'Signup successful! Check your email.' } ) , { status : 200 } )
45+ return new Response ( JSON . stringify ( { success : true , message : 'Signup successful! Check your email.' } ) , { status : 200 } )
3946 }
4047
48+ // Login stays frontend-only
4149 else if ( action === 'login' ) {
42- return new Response ( JSON . stringify ( { message : 'Use frontend login with anon key' } ) , { status : 400 } )
50+ return new Response ( JSON . stringify ( { success : false , message : 'Use frontend login with anon key' } ) , { status : 400 } )
4351 }
4452
53+ // Invalid action
4554 else {
46- return new Response ( JSON . stringify ( { message : 'Invalid action' } ) , { status : 400 } )
55+ return new Response ( JSON . stringify ( { success : false , message : 'Invalid action' } ) , { status : 400 } )
4756 }
57+
4858 } catch ( err ) {
49- console . error ( err )
50- return new Response ( JSON . stringify ( { message : 'Internal server error' } ) , { status : 500 } )
59+ console . error ( 'API Error:' , err )
60+ return new Response ( JSON . stringify ( { success : false , message : 'Internal server error' } ) , { status : 500 } )
5161 }
5262}
0 commit comments