fix: 修复登录成功但未跳转内部页面的竞态
All checks were successful
Docker Build and Push / build-image (push) Successful in 6m12s

LoginPage.onSubmit 在 setUser 异步提交前手动 navigate(from),
切到受保护页时 user 仍为 null, 被 RequireAuth 立即踢回 /login,
导致提示登录成功却进不去内部页面。改为依赖组件顶部
<Navigate to={from} replace/> 在 user 就绪后可靠跳转。
This commit is contained in:
cheney 2026-08-08 05:37:40 +08:00
parent 60628775ac
commit 44d60471ca

View File

@ -1,5 +1,5 @@
import { useState } from 'react' import { useState } from 'react'
import { useNavigate, useLocation, Navigate } from 'react-router-dom' import { useLocation, Navigate } from 'react-router-dom'
import { Button, Card, Checkbox, Input, Toast } from 'antd-mobile' import { Button, Card, Checkbox, Input, Toast } from 'antd-mobile'
import { useAuth } from '../auth/AuthContext.jsx' import { useAuth } from '../auth/AuthContext.jsx'
@ -7,7 +7,6 @@ import { useAuth } from '../auth/AuthContext.jsx'
// 使 antd-mobile: Input / Checkbox / Button / Card / Toast // 使 antd-mobile: Input / Checkbox / Button / Card / Toast
export default function LoginPage() { export default function LoginPage() {
const { user, login } = useAuth() const { user, login } = useAuth()
const navigate = useNavigate()
const location = useLocation() const location = useLocation()
const from = location.state?.from?.pathname || '/' const from = location.state?.from?.pathname || '/'
@ -17,6 +16,9 @@ export default function LoginPage() {
const [busy, setBusy] = useState(false) const [busy, setBusy] = useState(false)
const [err, setErr] = useState('') const [err, setErr] = useState('')
// user , <Navigate to={from} replace/>
// onSubmit navigate: setUser , ,
// user null RequireAuth /login, ""
if (user) return <Navigate to={from} replace /> if (user) return <Navigate to={from} replace />
async function onSubmit(e) { async function onSubmit(e) {
@ -26,7 +28,6 @@ export default function LoginPage() {
try { try {
await login(email.trim(), password, remember) await login(email.trim(), password, remember)
Toast.show({ icon: 'success', content: '登录成功' }) Toast.show({ icon: 'success', content: '登录成功' })
navigate(from, { replace: true })
} catch (e) { } catch (e) {
setErr(e.message) setErr(e.message)
} finally { } finally {