@@ -15,39 +15,40 @@ export async function POST(req) {
1515 if ( ! email || ! password ) {
1616 return new Response ( JSON . stringify ( { success : false , message : 'Email and password are required' } ) , { status : 400 } )
1717 }
18+ if ( ! captchaToken ) {
19+ return new Response ( JSON . stringify ( { success : false , message : 'Captcha token missing' } ) , { status : 400 } )
20+ }
1821
19- if ( action === 'signup' ) {
20- if ( ! captchaToken ) {
21- return new Response ( JSON . stringify ( { success : false , message : 'Captcha token missing' } ) , { status : 400 } )
22- }
23-
24- // Verify Turnstile token
25- const verifyRes = await fetch ( 'https://challenges.cloudflare.com/turnstile/v0/siteverify' , {
26- method : 'POST' ,
27- headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
28- body : new URLSearchParams ( {
29- secret : process . env . TURNSTILE_SECRET_KEY ,
30- response : captchaToken ,
31- } ) ,
32- } )
33-
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- }
22+ // Verify Turnstile token for both signup and login
23+ const verifyRes = await fetch ( 'https://challenges.cloudflare.com/turnstile/v0/siteverify' , {
24+ method : 'POST' ,
25+ headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
26+ body : new URLSearchParams ( {
27+ secret : process . env . TURNSTILE_SECRET_KEY ,
28+ response : captchaToken ,
29+ } ) ,
30+ } )
31+ const verifyData = await verifyRes . json ( )
32+ if ( ! verifyData . success ) {
33+ return new Response ( JSON . stringify ( { success : false , message : 'Captcha verification failed' } ) , { status : 400 } )
34+ }
3835
36+ if ( action === 'signup' ) {
3937 // Create Supabase user
4038 const { user, error } = await supabase . auth . admin . createUser ( { email, password } )
4139 if ( error ) {
4240 return new Response ( JSON . stringify ( { success : false , message : error . message } ) , { status : 400 } )
4341 }
44-
4542 return new Response ( JSON . stringify ( { success : true , message : 'Signup successful! Check your email.' } ) , { status : 200 } )
4643 }
4744
48- // Login stays frontend-only
4945 else if ( action === 'login' ) {
50- return new Response ( JSON . stringify ( { success : false , message : 'Use frontend login with anon key' } ) , { status : 400 } )
46+ // Authenticate user using Supabase admin API (signInWithPassword)
47+ const { data, error } = await supabase . auth . admin . signInWithPassword ( { email, password } )
48+ if ( error ) {
49+ return new Response ( JSON . stringify ( { success : false , message : error . message } ) , { status : 400 } )
50+ }
51+ return new Response ( JSON . stringify ( { success : true , message : 'Login successful.' } ) , { status : 200 } )
5152 }
5253
5354 // Invalid action
0 commit comments