登录链路去除邮箱查询,绑定邮箱改为SMTP验证码
1. 登录/会话/改密/重激活等主链路不再查询 email,仅找回密码与绑定邮箱功能查询,避免数据库未升级时登录500 2. 自助绑定邮箱改为验证码流程:发送验证码(当前密码+60秒限流)→ 校验验证码(10分钟有效、最多5次尝试)→ 绑定;解绑需当前密码 3. 重置密码保持一次性链接(30分钟、用后即删、重置后强制重新登录) 4. 新增 EMAIL_CODE_EXPIRE_MINUTES 配置,文档同步
This commit is contained in:
1 parent
7d22b2b922
commit
6c8fd2b432
11 files changed
+280
-57
No files matched your search
+77
-19
@@ -49,24 +49,32 @@ include __DIR__ . '/../includes/header.php';
|
||||
<div class="card">
|
||||
<div class="card-title">绑定邮箱(找回密码)</div>
|
||||
<p style="font-size:13px; color:var(--color-text-secondary); margin-bottom:12px;">
|
||||
绑定邮箱后,忘记密码时可在登录页通过“忘记密码”向该邮箱发送重置链接。
|
||||
绑定邮箱需通过邮箱验证码验证;忘记密码时可在登录页通过该邮箱获取一次性重置链接。
|
||||
</p>
|
||||
<div class="form-group">
|
||||
<label>当前绑定邮箱</label>
|
||||
<div id="currentEmail"><?php echo $bound_email ? htmlspecialchars($bound_email) : '未绑定'; ?></div>
|
||||
</div>
|
||||
<form id="emailForm">
|
||||
<div class="form-group">
|
||||
<label>新邮箱</label>
|
||||
<input type="email" id="bindEmail" maxlength="100" placeholder="留空则解除绑定">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>当前密码 <span style="color:red;">*</span></label>
|
||||
<input type="password" id="emailPassword" required>
|
||||
<small>安全验证:绑定邮箱需要输入当前登录密码</small>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">保存邮箱</button>
|
||||
</form>
|
||||
<div class="form-group">
|
||||
<label>新邮箱</label>
|
||||
<input type="email" id="bindEmail" maxlength="100" placeholder="请输入要绑定的邮箱">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>当前密码 <span style="color:red;">*</span></label>
|
||||
<input type="password" id="emailPassword">
|
||||
<small>安全验证:发送验证码需要输入当前登录密码</small>
|
||||
</div>
|
||||
<div style="display:flex; gap:8px; flex-wrap:wrap;">
|
||||
<button type="button" class="btn btn-secondary" onclick="sendBindEmailCode()">发送验证码</button>
|
||||
</div>
|
||||
<div class="form-group" style="margin-top:16px;">
|
||||
<label>邮箱验证码</label>
|
||||
<input type="text" id="emailCode" maxlength="6" inputmode="numeric" placeholder="请输入6位验证码">
|
||||
</div>
|
||||
<div style="display:flex; gap:8px; flex-wrap:wrap;">
|
||||
<button type="button" class="btn btn-primary" onclick="confirmBindEmail()">确认绑定</button>
|
||||
<button type="button" class="btn btn-outline-danger" onclick="unbindEmail()">解除绑定</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -101,28 +109,78 @@ document.getElementById('passwordForm').addEventListener('submit', async (e) =>
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('emailForm').addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
async function sendBindEmailCode() {
|
||||
const email = document.getElementById('bindEmail').value.trim();
|
||||
const password = document.getElementById('emailPassword').value;
|
||||
if (!email) {
|
||||
showToast('请输入要绑定的邮箱', 'warning');
|
||||
return;
|
||||
}
|
||||
if (!password) {
|
||||
showToast('请输入当前密码', 'warning');
|
||||
return;
|
||||
}
|
||||
const res = await apiPost('/api/auth/bind-email', {
|
||||
const res = await apiPost('/api/auth/email/send-code', {
|
||||
password: password,
|
||||
email: email
|
||||
});
|
||||
if (res && res.success) {
|
||||
showToast(res.message || '验证码已发送,请查收邮箱');
|
||||
document.getElementById('emailCode').focus();
|
||||
} else {
|
||||
showToast(res?.message || '验证码发送失败', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmBindEmail() {
|
||||
const email = document.getElementById('bindEmail').value.trim();
|
||||
const code = document.getElementById('emailCode').value.trim();
|
||||
if (!email) {
|
||||
showToast('请输入要绑定的邮箱', 'warning');
|
||||
return;
|
||||
}
|
||||
if (!code) {
|
||||
showToast('请输入邮箱验证码', 'warning');
|
||||
return;
|
||||
}
|
||||
const res = await apiPost('/api/auth/email/confirm', {
|
||||
email: email,
|
||||
code: code
|
||||
});
|
||||
if (res && res.success) {
|
||||
const bound = res.data && res.data.email ? res.data.email : '';
|
||||
document.getElementById('currentEmail').textContent = bound || '未绑定';
|
||||
document.getElementById('bindEmail').value = '';
|
||||
document.getElementById('emailPassword').value = '';
|
||||
showToast(res.message || '邮箱保存成功');
|
||||
document.getElementById('emailCode').value = '';
|
||||
showToast(res.message || '邮箱绑定成功');
|
||||
} else {
|
||||
showToast(res?.message || '邮箱保存失败', 'error');
|
||||
showToast(res?.message || '邮箱绑定失败', 'error');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function unbindEmail() {
|
||||
const password = document.getElementById('emailPassword').value;
|
||||
if (!password) {
|
||||
showToast('请输入当前密码', 'warning');
|
||||
return;
|
||||
}
|
||||
if (!confirm('确定解除绑定邮箱吗?解除后将无法通过邮箱找回密码。')) {
|
||||
return;
|
||||
}
|
||||
const res = await apiPost('/api/auth/email/unbind', {
|
||||
password: password
|
||||
});
|
||||
if (res && res.success) {
|
||||
document.getElementById('currentEmail').textContent = '未绑定';
|
||||
document.getElementById('bindEmail').value = '';
|
||||
document.getElementById('emailPassword').value = '';
|
||||
document.getElementById('emailCode').value = '';
|
||||
showToast(res.message || '邮箱已解除绑定');
|
||||
} else {
|
||||
showToast(res?.message || '解除绑定失败', 'error');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<?php include __DIR__ . '/../includes/footer.php'; ?>
|
||||
Reference in new issue
Block a user