vaibhav07112004 commited on
Commit
214b8ea
Β·
verified Β·
1 Parent(s): 3694423

Update app/auth/login/page.tsx

Browse files
Files changed (1) hide show
  1. 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
- // Add this useEffect to handle URL errors
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
- const response = await fetch("/api/auth/login", {
 
 
 
 
 
 
37
  method: "POST",
38
- headers: { "Content-Type": "application/json" },
39
- body: JSON.stringify({ email, password }),
40
  })
41
 
42
- const data = await response.json()
43
-
44
- if (data.success) {
45
- localStorage.setItem("user", JSON.stringify(data.user))
46
- router.push("/")
47
  } else {
48
- setError(data.error || "Login failed")
 
49
  }
50
  } catch (error) {
 
51
  setError("Network error. Please try again.")
52
  } finally {
53
  setIsLoading(false)
54
  }
55
  }
56
 
57
- // Function to clear Google session cookies
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 with account selection
64
- window.location.href = "/api/auth/google"
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 = "/api/auth/google?force_prompt=true"
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
  >