Skip to content

Commit 627b045

Browse files
committed
Fix : added turnsite to the login part
1 parent 9957faa commit 627b045

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

app/api/auth/route.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

app/login/page.jsx

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,27 @@ export default function LoginPage() {
4040
setError('')
4141

4242
try {
43+
if (!captchaToken) throw new Error('Please complete captcha')
44+
45+
const res = await fetch('/auth', {
46+
method: 'POST',
47+
headers: { 'Content-Type': 'application/json' },
48+
body: JSON.stringify({
49+
email,
50+
password,
51+
captchaToken,
52+
action: isLogin ? 'login' : 'signup'
53+
}),
54+
})
55+
56+
const data = await res.json()
57+
if (!data.success) throw new Error(data.message || 'Action failed')
58+
4359
if (isLogin) {
44-
// Login using frontend anon key only, no captcha
45-
const { error } = await supabase.auth.signInWithPassword({ email, password })
46-
if (error) throw error
4760
router.push('/dashboard')
4861
} else {
49-
// Signup via API route with Turnstile
50-
if (!captchaToken) throw new Error('Please complete captcha')
51-
52-
const res = await fetch('/auth', {
53-
method: 'POST',
54-
headers: { 'Content-Type': 'application/json' },
55-
body: JSON.stringify({ email, password, captchaToken, action: 'signup' }),
56-
})
57-
58-
const data = await res.json()
59-
if (!data.success) throw new Error(data.message || 'Signup failed')
60-
6162
alert(data.message)
62-
setIsLogin(true) // switch to login after signup
63+
setIsLogin(true)
6364
}
6465
} catch (err) {
6566
setError(err.message || 'Something went wrong')
@@ -141,15 +142,13 @@ export default function LoginPage() {
141142
</div>
142143
)}
143144

144-
{/* Turnstile only for signup */}
145-
{!isLogin && (
146-
<div className="flex justify-center">
147-
<Turnstile
148-
siteKey={process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY}
149-
onSuccess={(token) => setCaptchaToken(token)}
150-
/>
151-
</div>
152-
)}
145+
{/* Turnstile for both login and signup */}
146+
<div className="flex justify-center">
147+
<Turnstile
148+
siteKey={process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY}
149+
onSuccess={(token) => setCaptchaToken(token)}
150+
/>
151+
</div>
153152

154153
<button
155154
onClick={handleAuth}

0 commit comments

Comments
 (0)