fix(frontend): distinguish initial authentication state on login page mount

- Add wasUserPresentRef to detect if the user was already authenticated on initial page load.
- Guard the useEffect redirect block so that it only redirects automatically if the user was already authenticated when mounting.
- Prevent duplicate concurrent router.replace calls from canceling each other when logging in via the form.
This commit is contained in:
ryan
2026-06-13 11:09:55 +08:00
parent 0df4712831
commit 9a6bb04bf5
+7 -4
View File
@@ -41,6 +41,7 @@ export function LoginPage() {
const [loginSuccess, setLoginSuccess] = useState(false)
const redirectedRef = useRef(false)
const callbackProcessedRef = useRef(false)
const wasUserPresentRef = useRef(!!user)
const resolveRedirectTarget = useCallback(() => {
const callbackUrl = searchParams.get('callbackUrl')
@@ -71,10 +72,12 @@ export function LoginPage() {
}
if (user) {
if (!redirectedRef.current) {
redirectedRef.current = true
router.replace(resolveRedirectTargetRef.current())
setIsCheckingSession(false)
if (wasUserPresentRef.current) {
if (!redirectedRef.current) {
redirectedRef.current = true
router.replace(resolveRedirectTargetRef.current())
setIsCheckingSession(false)
}
}
return
}