Skip to content

Commit 9957faa

Browse files
committed
Fix : added turnsite to only signup only
1 parent c21149a commit 9957faa

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

app/api/auth/route.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const supabase = createClient(
77

88
export 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
}

app/login/page.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function LoginPage() {
4141

4242
try {
4343
if (isLogin) {
44-
// Login with frontend anon key
44+
// Login using frontend anon key only, no captcha
4545
const { error } = await supabase.auth.signInWithPassword({ email, password })
4646
if (error) throw error
4747
router.push('/dashboard')
@@ -56,13 +56,13 @@ export default function LoginPage() {
5656
})
5757

5858
const data = await res.json()
59-
if (!res.ok) throw new Error(data.message)
59+
if (!data.success) throw new Error(data.message || 'Signup failed')
6060

6161
alert(data.message)
6262
setIsLogin(true) // switch to login after signup
6363
}
6464
} catch (err) {
65-
setError(err.message)
65+
setError(err.message || 'Something went wrong')
6666
} finally {
6767
setLoading(false)
6868
}
@@ -141,6 +141,7 @@ export default function LoginPage() {
141141
</div>
142142
)}
143143

144+
{/* Turnstile only for signup */}
144145
{!isLogin && (
145146
<div className="flex justify-center">
146147
<Turnstile
@@ -203,7 +204,8 @@ export default function LoginPage() {
203204
onClick={handleGoogleSignIn}
204205
className="w-full flex items-center justify-center py-3 px-4 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-200 font-medium hover:bg-gray-50 dark:hover:bg-gray-600 transition-all"
205206
>
206-
Continue with Google
207+
<img src="./google.webp" width={24}></img>
208+
<span className='mx-2'>Continue with Google</span>
207209
</button>
208210

209211
<div className="text-center text-xs text-gray-500 dark:text-gray-400 mt-6">

public/google.webp

1.74 KB
Loading

0 commit comments

Comments
 (0)