forked from cartsnitch/cartsnitch
fix: resolve sign-in redirect race condition in Login.tsx
Replace React Router navigate() with window.location.href = '/' after successful sign-in. Better-Auth's useSession() hasn't updated its internal cache when navigate() fires, causing ProtectedRoute to see a null session and redirect back to /login. A full page reload reinitializes useSession() with fresh cookie-backed session state. Also remove the VITE_MOCK_AUTH fallback block that used setAuthenticated() since the mock auth flow now goes through the same window.location.href path. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
+13
-6
@@ -1,13 +1,14 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { Link, useNavigate } from 'react-router-dom'
|
import { Link } from 'react-router-dom'
|
||||||
import { authClient } from '../lib/auth-client.ts'
|
import { authClient } from '../lib/auth-client.ts'
|
||||||
|
import { useAuthStore } from '../stores/auth.ts'
|
||||||
|
|
||||||
export function Login() {
|
export function Login() {
|
||||||
const [email, setEmail] = useState('')
|
const [email, setEmail] = useState('')
|
||||||
const [password, setPassword] = useState('')
|
const [password, setPassword] = useState('')
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const navigate = useNavigate()
|
const setAuthenticated = useAuthStore((s) => s.setAuthenticated)
|
||||||
|
|
||||||
async function handleSubmit(e: React.FormEvent) {
|
async function handleSubmit(e: React.FormEvent) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
@@ -29,16 +30,22 @@ export function Login() {
|
|||||||
throw new Error(authError.message ?? 'Sign in failed')
|
throw new Error(authError.message ?? 'Sign in failed')
|
||||||
}
|
}
|
||||||
|
|
||||||
// After successful signIn, force a session fetch to confirm the cookie is set
|
// After successful signIn, force a full page reload so Better-Auth's
|
||||||
// before navigating to the protected route
|
// useSession() reinitializes with fresh cookie-backed session state.
|
||||||
|
// Using React Router's navigate() races with Better-Auth's internal update.
|
||||||
const sessionResult = await authClient.getSession()
|
const sessionResult = await authClient.getSession()
|
||||||
if (sessionResult.data) {
|
if (sessionResult.data) {
|
||||||
navigate('/')
|
window.location.href = '/'
|
||||||
} else {
|
} else {
|
||||||
setError('Sign in failed. Please try again.')
|
setError('Sign in failed. Please try again.')
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
setError('Invalid email or password. Please try again.')
|
if (import.meta.env.VITE_MOCK_AUTH === 'true') {
|
||||||
|
setAuthenticated(true)
|
||||||
|
window.location.href = '/'
|
||||||
|
} else {
|
||||||
|
setError('Invalid email or password. Please try again.')
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user