From 63db81e498df098731c3a870c27748c5ebe28c98 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Thu, 13 Aug 2026 15:13:30 +0800 Subject: [PATCH] feat: add recovery-code password reset to login --- app/globals.css | 23 +++++++++++++++ components/Modals.js | 70 +++++++++++++++++++++++++++++++++++++++++++- locales/en.json | 12 ++++++++ locales/zh.json | 12 ++++++++ test/api.test.js | 46 +++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) diff --git a/app/globals.css b/app/globals.css index f15dd92..c9b5cf8 100644 --- a/app/globals.css +++ b/app/globals.css @@ -755,6 +755,29 @@ svg { color: var(--glass-text-light); } +.auth-forgot-link { + display: block; + width: fit-content; + margin: 12px auto 0; + padding: 0; + border: 0; + background: none; + color: var(--primary); + font-size: 13px; + cursor: pointer; +} + +.auth-forgot-link:hover { + text-decoration: underline; +} + +.auth-reset-hint { + margin: -8px 0 16px; + color: var(--glass-text-secondary); + font-size: 13px; + line-height: 1.5; +} + .captcha-row { display: flex; align-items: center; diff --git a/components/Modals.js b/components/Modals.js index 17569c2..4a68c4d 100644 --- a/components/Modals.js +++ b/components/Modals.js @@ -17,6 +17,11 @@ export default function Modals({ modal, onClose, reportPostId }) { // 登录表单 const [loginEmail, setLoginEmail] = useState(''); const [loginPassword, setLoginPassword] = useState(''); + const [authMode, setAuthMode] = useState('login'); + const [resetEmail, setResetEmail] = useState(''); + const [resetRecoveryCode, setResetRecoveryCode] = useState(''); + const [resetPassword, setResetPassword] = useState(''); + const [resetPasswordConfirm, setResetPasswordConfirm] = useState(''); // 注册表单 const [regUsername, setRegUsername] = useState(''); @@ -33,6 +38,7 @@ export default function Modals({ modal, onClose, reportPostId }) { if (modal === 'register') { API.getCaptcha().then((c) => setRegCaptcha(c)).catch(() => {}); } + if (modal !== 'login') setAuthMode('login'); }, [modal]); if (!modal) return null; @@ -57,6 +63,34 @@ export default function Modals({ modal, onClose, reportPostId }) { } }; + const handleResetPassword = async () => { + if (!resetEmail.trim() || !resetRecoveryCode.trim() || !resetPassword || !resetPasswordConfirm) { + toast(t('auth.resetFillAll'), 'warning'); + return; + } + if (resetPassword.length < 6) { + toast(t('auth.passwordTooShort'), 'warning'); + return; + } + if (resetPassword !== resetPasswordConfirm) { + toast(t('auth.passwordMismatch'), 'warning'); + return; + } + try { + await API.resetPassword(resetEmail.trim(), resetRecoveryCode.trim(), resetPassword); + setAuthMode('login'); + setLoginEmail(resetEmail.trim()); + setLoginPassword(''); + setResetEmail(''); + setResetRecoveryCode(''); + setResetPassword(''); + setResetPasswordConfirm(''); + toast(t('auth.resetSuccess'), 'success'); + } catch (err) { + toast(t('auth.resetFailed', { msg: err.message }), 'error'); + } + }; + const handleRegister = async () => { if (!regUsername.trim() || !regEmail.trim() || !regPassword) { toast('请填写完整信息', 'warning'); return; } if (regPassword.length < 6) { toast('密码至少6位', 'warning'); return; } @@ -95,7 +129,7 @@ export default function Modals({ modal, onClose, reportPostId }) { return ( <> - {modal === 'login' && ( + {modal === 'login' && authMode === 'login' && (
{t('auth.resetHint')}
+ setResetEmail(e.target.value)} /> + setResetRecoveryCode(e.target.value)} /> + setResetPassword(e.target.value)} /> + setResetPasswordConfirm(e.target.value)} + onKeyDown={(e) => { if (e.key === 'Enter') handleResetPassword(); }} + /> + +