Spaces:
Sleeping
Sleeping
Update app/auth/login/page.tsx
Browse files- app/auth/login/page.tsx +24 -16
app/auth/login/page.tsx
CHANGED
@@ -18,7 +18,7 @@ export default function LoginPage() {
|
|
18 |
const [error, setError] = useState("")
|
19 |
const router = useRouter()
|
20 |
|
21 |
-
//
|
22 |
useEffect(() => {
|
23 |
const urlParams = new URLSearchParams(window.location.search)
|
24 |
const urlError = urlParams.get("error")
|
@@ -27,41 +27,48 @@ export default function LoginPage() {
|
|
27 |
}
|
28 |
}, [])
|
29 |
|
|
|
30 |
const handleLogin = async (e: React.FormEvent) => {
|
31 |
e.preventDefault()
|
32 |
setIsLoading(true)
|
33 |
setError("")
|
34 |
|
35 |
try {
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
method: "POST",
|
38 |
-
|
39 |
-
|
40 |
})
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
localStorage.setItem("user", JSON.stringify(data.user))
|
46 |
-
router.push("/")
|
47 |
} else {
|
48 |
-
|
|
|
49 |
}
|
50 |
} catch (error) {
|
|
|
51 |
setError("Network error. Please try again.")
|
52 |
} finally {
|
53 |
setIsLoading(false)
|
54 |
}
|
55 |
}
|
56 |
|
57 |
-
//
|
58 |
const handleGoogleSignIn = () => {
|
59 |
-
// Clear any existing Google session
|
60 |
document.cookie = "G_AUTHUSER_H=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"
|
61 |
document.cookie = "G_ENABLED_IDPS=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"
|
62 |
|
63 |
-
// Redirect to Google OAuth
|
64 |
-
window.location.href =
|
65 |
}
|
66 |
|
67 |
return (
|
@@ -183,6 +190,7 @@ export default function LoginPage() {
|
|
183 |
Sign in with Google
|
184 |
</Button>
|
185 |
|
|
|
186 |
<div className="text-center mt-2">
|
187 |
<button
|
188 |
type="button"
|
@@ -195,8 +203,8 @@ export default function LoginPage() {
|
|
195 |
const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim()
|
196 |
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/"
|
197 |
}
|
198 |
-
// Force account selection
|
199 |
-
window.location.href =
|
200 |
}}
|
201 |
className="text-sm text-gray-400 hover:text-pink-400 flex items-center justify-center gap-1 mx-auto"
|
202 |
>
|
|
|
18 |
const [error, setError] = useState("")
|
19 |
const router = useRouter()
|
20 |
|
21 |
+
// β
UPDATED: Handle URL errors from OAuth redirects
|
22 |
useEffect(() => {
|
23 |
const urlParams = new URLSearchParams(window.location.search)
|
24 |
const urlError = urlParams.get("error")
|
|
|
27 |
}
|
28 |
}, [])
|
29 |
|
30 |
+
// β
UPDATED: Email/Password login with backend integration
|
31 |
const handleLogin = async (e: React.FormEvent) => {
|
32 |
e.preventDefault()
|
33 |
setIsLoading(true)
|
34 |
setError("")
|
35 |
|
36 |
try {
|
37 |
+
// β
FIXED: Use FormData for backend compatibility
|
38 |
+
const formData = new FormData()
|
39 |
+
formData.append('email', email)
|
40 |
+
formData.append('password', password)
|
41 |
+
|
42 |
+
// β
FIXED: Call your actual backend endpoint
|
43 |
+
const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/auth/login`, {
|
44 |
method: "POST",
|
45 |
+
body: formData,
|
46 |
+
credentials: 'include', // β
CRITICAL: Send cookies
|
47 |
})
|
48 |
|
49 |
+
if (response.ok) {
|
50 |
+
console.log('β
Login successful')
|
51 |
+
router.push("/") // Redirect to home
|
|
|
|
|
52 |
} else {
|
53 |
+
const errorData = await response.json()
|
54 |
+
setError(errorData.detail || 'Login failed')
|
55 |
}
|
56 |
} catch (error) {
|
57 |
+
console.error('β Login error:', error)
|
58 |
setError("Network error. Please try again.")
|
59 |
} finally {
|
60 |
setIsLoading(false)
|
61 |
}
|
62 |
}
|
63 |
|
64 |
+
// β
UPDATED: Google OAuth with backend integration
|
65 |
const handleGoogleSignIn = () => {
|
66 |
+
// Clear any existing Google session cookies
|
67 |
document.cookie = "G_AUTHUSER_H=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"
|
68 |
document.cookie = "G_ENABLED_IDPS=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"
|
69 |
|
70 |
+
// β
FIXED: Redirect to your backend Google OAuth endpoint
|
71 |
+
window.location.href = `${process.env.NEXT_PUBLIC_BACKEND_URL}/auth/google`
|
72 |
}
|
73 |
|
74 |
return (
|
|
|
190 |
Sign in with Google
|
191 |
</Button>
|
192 |
|
193 |
+
{/* β
UPDATED: Use different Google account with backend */}
|
194 |
<div className="text-center mt-2">
|
195 |
<button
|
196 |
type="button"
|
|
|
203 |
const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim()
|
204 |
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/"
|
205 |
}
|
206 |
+
// β
FIXED: Force account selection with backend endpoint
|
207 |
+
window.location.href = `${process.env.NEXT_PUBLIC_BACKEND_URL}/auth/google?prompt=select_account`
|
208 |
}}
|
209 |
className="text-sm text-gray-400 hover:text-pink-400 flex items-center justify-center gap-1 mx-auto"
|
210 |
>
|