无密码账号验证码登录后强制设置密码
- 登录/注册/me 接口返回 has_password 标志 - 新增 POST /api/auth/set-password:登录态直接设密码(无需邮箱验证码) - 前端:has_password=false 的用户登录后弹出不可关闭的设置密码层,设置成功才能进入应用 - 测试 +4(47/47):标志返回、端点鉴权、改密生效、旧密码失效
This commit is contained in:
Vendored
+39
@@ -139,9 +139,48 @@
|
||||
function enterApp(me) {
|
||||
state.me = me;
|
||||
$("#topbar-name").textContent = me.display_name;
|
||||
if (me.has_password === false) {
|
||||
// 无密码老账号:强制先设置密码,设置成功前不能进入应用
|
||||
showForcePassword();
|
||||
return;
|
||||
}
|
||||
show("feed");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 强制设置密码
|
||||
function showForcePassword() {
|
||||
$("#force-password-overlay").classList.remove("hidden");
|
||||
}
|
||||
|
||||
$("#force-password-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const f = e.target;
|
||||
const err = $("#force-password-error");
|
||||
err.textContent = "";
|
||||
if (f.password.value !== f.confirm_password.value) {
|
||||
err.textContent = "两次输入的密码不一致";
|
||||
f.confirm_password.focus();
|
||||
return;
|
||||
}
|
||||
const btn = f.querySelector('[type="submit"]');
|
||||
btn.disabled = true;
|
||||
try {
|
||||
await api("/auth/set-password", {
|
||||
method: "POST",
|
||||
body: { password: f.password.value, confirm_password: f.confirm_password.value },
|
||||
});
|
||||
state.me = { ...state.me, has_password: true };
|
||||
$("#force-password-overlay").classList.add("hidden");
|
||||
f.reset();
|
||||
toast("密码已设置,以后可用密码登录");
|
||||
show("feed");
|
||||
} catch (error) {
|
||||
err.textContent = error.message;
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------- 验证码(注册/登录共用)
|
||||
function bindSendCode(btnId, emailSel, hintSel, errSel, purpose) {
|
||||
let cooldownUntil = 0; // 发出成功后的冷却截止时间戳,期间按钮保持禁用
|
||||
|
||||
Reference in New Issue
Block a user