fix: 平台登录默认显示验证码,管理员跳过验证码
This commit is contained in:
@@ -117,6 +117,8 @@ async def login(body: dict) -> dict:
|
||||
mode = _setting(session).login_mode
|
||||
if mode != "nqi":
|
||||
raise _bad("账号或密码不正确")
|
||||
if str(body.get("sessionId") or ""):
|
||||
return await nqi_finish(body)
|
||||
started = await nqi_start({"username": username, "purpose": "login"})
|
||||
if started.get("status") == "captcha":
|
||||
return {**started, "status": "need_captcha"}
|
||||
|
||||
@@ -129,10 +129,10 @@ def test_admin_is_judged_locally_when_users_use_nqi(client: TestClient, monkeypa
|
||||
raise NqiError("不应访问平台")
|
||||
|
||||
monkeypatch.setattr("app.api.http.NqiClient.open_login", fail_open)
|
||||
wrong = client.post("/api/auth/login", json={"username": "admin", "password": "wrong"})
|
||||
wrong = client.post("/api/auth/login", json={"username": "admin", "password": "wrong", "sessionId": "captcha-session", "captcha": "abcd"})
|
||||
assert wrong.status_code == 400
|
||||
assert wrong.json()["detail"] == "登录失败"
|
||||
ok = client.post("/api/auth/login", json={"username": "admin", "password": "admin-pass"})
|
||||
ok = client.post("/api/auth/login", json={"username": "admin", "password": "admin-pass", "sessionId": "captcha-session", "captcha": "abcd"})
|
||||
assert ok.status_code == 200
|
||||
assert ok.json()["user"]["role"] == "admin"
|
||||
assert calls == []
|
||||
|
||||
@@ -35,16 +35,22 @@ const busy = ref(false);
|
||||
let countdownTimer = 0;
|
||||
|
||||
onMounted(async () => {
|
||||
if (props.purpose !== "pool") return;
|
||||
mode.value = "nqi";
|
||||
await startNqi();
|
||||
if (props.purpose === "pool") {
|
||||
mode.value = "nqi";
|
||||
await startNqi();
|
||||
return;
|
||||
}
|
||||
const result = await api<{ mode: string }>("/api/auth/mode");
|
||||
mode.value = result.mode;
|
||||
if (mode.value === "nqi") await startNqi();
|
||||
});
|
||||
|
||||
onUnmounted(() => window.clearInterval(countdownTimer));
|
||||
|
||||
function submitLogin() {
|
||||
if (busy.value || captchaLoading.value) return;
|
||||
if (busy.value) return;
|
||||
if (props.purpose === "pool") {
|
||||
if (captchaLoading.value) return;
|
||||
if (!sessionId.value) {
|
||||
startNqi();
|
||||
return;
|
||||
@@ -52,10 +58,6 @@ function submitLogin() {
|
||||
finishNqi();
|
||||
return;
|
||||
}
|
||||
if (sessionId.value) {
|
||||
finishNqi();
|
||||
return;
|
||||
}
|
||||
beginLogin();
|
||||
}
|
||||
|
||||
@@ -63,21 +65,34 @@ async function beginLogin() {
|
||||
busy.value = true;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string }>("/api/auth/login", {
|
||||
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string; message?: string }>("/api/auth/login", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username: username.value, password: password.value }),
|
||||
body: JSON.stringify({
|
||||
username: username.value,
|
||||
password: password.value,
|
||||
sessionId: sessionId.value,
|
||||
captcha: captcha.value,
|
||||
smsCode: smsCode.value,
|
||||
}),
|
||||
});
|
||||
if (result.status === "need_captcha") {
|
||||
smsRequired.value = false;
|
||||
stopCountdown();
|
||||
smsCode.value = "";
|
||||
captcha.value = "";
|
||||
if (result.captcha) captchaUrl.value = result.captcha;
|
||||
errorText.value = result.message || "请重新输入验证码";
|
||||
return;
|
||||
}
|
||||
if (result.status === "sms_required") {
|
||||
smsRequired.value = true;
|
||||
smsCode.value = "";
|
||||
smsHint.value = "";
|
||||
return;
|
||||
}
|
||||
if (result.status === "ok" && result.token && result.user) {
|
||||
saveSession(result.token, result.user);
|
||||
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
||||
return;
|
||||
}
|
||||
if (result.status === "need_captcha") {
|
||||
mode.value = "nqi";
|
||||
sessionId.value = result.sessionId || "";
|
||||
captchaUrl.value = result.captcha || "";
|
||||
captchaBroken.value = !captchaUrl.value;
|
||||
captcha.value = "";
|
||||
}
|
||||
} catch (error) {
|
||||
fail(error);
|
||||
|
||||
Reference in New Issue
Block a user