Merge pull request #25 from forumlify/feat/forgot-password-recovery-code
feat: add recovery-code password reset to login
This commit is contained in:
5 files changed
+179
-1
No files matched your search
@@ -755,6 +755,29 @@ svg {
|
|||||||
color: var(--glass-text-light);
|
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 {
|
.captcha-row {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
+83
-1
@@ -17,6 +17,11 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
// 登录表单
|
// 登录表单
|
||||||
const [loginEmail, setLoginEmail] = useState('');
|
const [loginEmail, setLoginEmail] = useState('');
|
||||||
const [loginPassword, setLoginPassword] = 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('');
|
const [regUsername, setRegUsername] = useState('');
|
||||||
@@ -33,6 +38,13 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
if (modal === 'register') {
|
if (modal === 'register') {
|
||||||
API.getCaptcha().then((c) => setRegCaptcha(c)).catch(() => {});
|
API.getCaptcha().then((c) => setRegCaptcha(c)).catch(() => {});
|
||||||
}
|
}
|
||||||
|
if (modal !== 'login') {
|
||||||
|
setAuthMode('login');
|
||||||
|
setResetEmail('');
|
||||||
|
setResetRecoveryCode('');
|
||||||
|
setResetPassword('');
|
||||||
|
setResetPasswordConfirm('');
|
||||||
|
}
|
||||||
}, [modal]);
|
}, [modal]);
|
||||||
|
|
||||||
if (!modal) return null;
|
if (!modal) return null;
|
||||||
@@ -57,6 +69,42 @@ 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 returnToLogin = () => {
|
||||||
|
setAuthMode('login');
|
||||||
|
setResetEmail('');
|
||||||
|
setResetRecoveryCode('');
|
||||||
|
setResetPassword('');
|
||||||
|
setResetPasswordConfirm('');
|
||||||
|
};
|
||||||
|
|
||||||
const handleRegister = async () => {
|
const handleRegister = async () => {
|
||||||
if (!regUsername.trim() || !regEmail.trim() || !regPassword) { toast('请填写完整信息', 'warning'); return; }
|
if (!regUsername.trim() || !regEmail.trim() || !regPassword) { toast('请填写完整信息', 'warning'); return; }
|
||||||
if (regPassword.length < 6) { toast('密码至少6位', 'warning'); return; }
|
if (regPassword.length < 6) { toast('密码至少6位', 'warning'); return; }
|
||||||
@@ -95,7 +143,7 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{modal === 'login' && (
|
{modal === 'login' && authMode === 'login' && (
|
||||||
<div className="modal active" onClick={close}>
|
<div className="modal active" onClick={close}>
|
||||||
<div className="modal-content">
|
<div className="modal-content">
|
||||||
<span className="close" onClick={onClose}><Icon name="close" size={20} /></span>
|
<span className="close" onClick={onClose}><Icon name="close" size={20} /></span>
|
||||||
@@ -104,6 +152,40 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
<input type="password" placeholder={t('auth.password')} value={loginPassword} onChange={(e) => setLoginPassword(e.target.value)}
|
<input type="password" placeholder={t('auth.password')} value={loginPassword} onChange={(e) => setLoginPassword(e.target.value)}
|
||||||
onKeyDown={(e) => { if (e.key === 'Enter') handleLogin(); }} />
|
onKeyDown={(e) => { if (e.key === 'Enter') handleLogin(); }} />
|
||||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleLogin}>{t('auth.login')}</button>
|
<button className="btn-primary" style={{ width: '100%' }} onClick={handleLogin}>{t('auth.login')}</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="auth-forgot-link"
|
||||||
|
onClick={() => {
|
||||||
|
setResetEmail(loginEmail);
|
||||||
|
setAuthMode('forgot');
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t('auth.forgotPassword')}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{modal === 'login' && authMode === 'forgot' && (
|
||||||
|
<div className="modal active" onClick={close}>
|
||||||
|
<div className="modal-content">
|
||||||
|
<span className="close" onClick={onClose}><Icon name="close" size={20} /></span>
|
||||||
|
<h2 style={{ marginBottom: 16 }}>{t('auth.resetTitle')}</h2>
|
||||||
|
<p className="auth-reset-hint">{t('auth.resetHint')}</p>
|
||||||
|
<input type="email" placeholder={t('auth.email')} value={resetEmail} onChange={(e) => setResetEmail(e.target.value)} />
|
||||||
|
<input type="text" placeholder={t('auth.recoveryCode')} value={resetRecoveryCode} onChange={(e) => setResetRecoveryCode(e.target.value)} />
|
||||||
|
<input type="password" placeholder={t('auth.newPassword')} value={resetPassword} onChange={(e) => setResetPassword(e.target.value)} />
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
placeholder={t('auth.confirmPassword')}
|
||||||
|
value={resetPasswordConfirm}
|
||||||
|
onChange={(e) => setResetPasswordConfirm(e.target.value)}
|
||||||
|
onKeyDown={(e) => { if (e.key === 'Enter') handleResetPassword(); }}
|
||||||
|
/>
|
||||||
|
<button className="btn-primary" style={{ width: '100%' }} onClick={handleResetPassword}>{t('auth.resetPassword')}</button>
|
||||||
|
<button type="button" className="auth-forgot-link" onClick={returnToLogin}>
|
||||||
|
{t('auth.backToLogin')}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -73,6 +73,18 @@
|
|||||||
"username": "Username",
|
"username": "Username",
|
||||||
"passwordHint": "Password (min 6 chars)",
|
"passwordHint": "Password (min 6 chars)",
|
||||||
"forgotPassword": "Forgot password?",
|
"forgotPassword": "Forgot password?",
|
||||||
|
"resetTitle": "Reset password with a recovery code",
|
||||||
|
"resetHint": "Enter your email and a recovery code generated in Settings to set a new password.",
|
||||||
|
"recoveryCode": "Recovery code",
|
||||||
|
"newPassword": "New password (min 6 characters)",
|
||||||
|
"confirmPassword": "Confirm new password",
|
||||||
|
"resetPassword": "Reset password",
|
||||||
|
"backToLogin": "Back to login",
|
||||||
|
"resetFillAll": "Please fill in all fields",
|
||||||
|
"passwordTooShort": "New password must be at least 6 characters",
|
||||||
|
"passwordMismatch": "The passwords do not match",
|
||||||
|
"resetSuccess": "Password reset. Please log in with your new password.",
|
||||||
|
"resetFailed": "Password reset failed: {{msg}}",
|
||||||
"captchaError": "Wrong captcha, please try again",
|
"captchaError": "Wrong captcha, please try again",
|
||||||
"registerSuccess": "Registration successful",
|
"registerSuccess": "Registration successful",
|
||||||
"loginSuccess": "Logged in",
|
"loginSuccess": "Logged in",
|
||||||
|
|||||||
@@ -73,6 +73,18 @@
|
|||||||
"username": "用户名",
|
"username": "用户名",
|
||||||
"passwordHint": "密码(至少6位)",
|
"passwordHint": "密码(至少6位)",
|
||||||
"forgotPassword": "忘记密码?",
|
"forgotPassword": "忘记密码?",
|
||||||
|
"resetTitle": "通过恢复码重置密码",
|
||||||
|
"resetHint": "输入邮箱和设置中生成的恢复码,即可设置新密码。",
|
||||||
|
"recoveryCode": "恢复码",
|
||||||
|
"newPassword": "新密码(至少6位)",
|
||||||
|
"confirmPassword": "确认新密码",
|
||||||
|
"resetPassword": "重置密码",
|
||||||
|
"backToLogin": "返回登录",
|
||||||
|
"resetFillAll": "请填写完整信息",
|
||||||
|
"passwordTooShort": "新密码至少6位",
|
||||||
|
"passwordMismatch": "两次输入的密码不一致",
|
||||||
|
"resetSuccess": "密码重置成功,请使用新密码登录",
|
||||||
|
"resetFailed": "密码重置失败:{{msg}}",
|
||||||
"captchaError": "验证码错误,请重新计算",
|
"captchaError": "验证码错误,请重新计算",
|
||||||
"registerSuccess": "注册成功",
|
"registerSuccess": "注册成功",
|
||||||
"loginSuccess": "登录成功",
|
"loginSuccess": "登录成功",
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ let replyId = null;
|
|||||||
|
|
||||||
const adminUser = { email: `admin${suffix}@test.com`, password: '123456', username: `admin${suffix}` };
|
const adminUser = { email: `admin${suffix}@test.com`, password: '123456', username: `admin${suffix}` };
|
||||||
const normalUser = { email: `user${suffix}@test.com`, password: '123456', username: `user${suffix}` };
|
const normalUser = { email: `user${suffix}@test.com`, password: '123456', username: `user${suffix}` };
|
||||||
|
const resetUser = { email: `reset${suffix}@test.com`, password: '123456', username: `reset${suffix}` };
|
||||||
|
|
||||||
test('注册第一个用户自动成为管理员', async () => {
|
test('注册第一个用户自动成为管理员', async () => {
|
||||||
const { status, data } = await api('/auth/register', {
|
const { status, data } = await api('/auth/register', {
|
||||||
@@ -67,6 +68,54 @@ test('注册普通用户', async () => {
|
|||||||
userId = data.user.id;
|
userId = data.user.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('恢复码可重置密码且只能使用一次', async () => {
|
||||||
|
const registered = await api('/auth/register', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(resetUser),
|
||||||
|
});
|
||||||
|
assert.equal(registered.status, 200);
|
||||||
|
|
||||||
|
const loggedIn = await api('/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: resetUser.email, password: resetUser.password }),
|
||||||
|
});
|
||||||
|
assert.equal(loggedIn.status, 200);
|
||||||
|
assert.ok(loggedIn.data.token);
|
||||||
|
|
||||||
|
const generated = await api('/auth/recovery-codes/generate', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { Authorization: `Bearer ${loggedIn.data.token}` },
|
||||||
|
});
|
||||||
|
assert.equal(generated.status, 200);
|
||||||
|
assert.equal(generated.data.codes.length, 10);
|
||||||
|
|
||||||
|
const recoveryCode = generated.data.codes[0];
|
||||||
|
const newPassword = '654321';
|
||||||
|
const reset = await api('/auth/reset-password', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: resetUser.email, recoveryCode, newPassword }),
|
||||||
|
});
|
||||||
|
assert.equal(reset.status, 200);
|
||||||
|
|
||||||
|
const oldLogin = await api('/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: resetUser.email, password: resetUser.password }),
|
||||||
|
});
|
||||||
|
assert.equal(oldLogin.status, 401);
|
||||||
|
|
||||||
|
const newLogin = await api('/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: resetUser.email, password: newPassword }),
|
||||||
|
});
|
||||||
|
assert.equal(newLogin.status, 200);
|
||||||
|
|
||||||
|
const reused = await api('/auth/reset-password', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: resetUser.email, recoveryCode, newPassword: resetUser.password }),
|
||||||
|
});
|
||||||
|
assert.equal(reused.status, 400);
|
||||||
|
});
|
||||||
|
|
||||||
test('密码少于6位被拒绝', async () => {
|
test('密码少于6位被拒绝', async () => {
|
||||||
const { status } = await api('/auth/register', {
|
const { status } = await api('/auth/register', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
Reference in new issue
Block a user