Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Prevent duplicate cloud login requests

Daniel59c79c93c413
diff --git a/app/src/config/tabs/accountAuth.test.ts b/app/src/config/tabs/accountAuth.test.ts
index 8cdf7fb160..95ea057936 100644
--- a/app/src/config/tabs/accountAuth.test.ts
+++ b/app/src/config/tabs/accountAuth.test.ts
@@ -6,6 +6,7 @@ test("login requires agreement and a non-empty password", () => {
assert.equal(isAccountLoginDisabled(false, "password"), true);
assert.equal(isAccountLoginDisabled(true, ""), true);
assert.equal(isAccountLoginDisabled(true, "password"), false);
+ assert.equal(isAccountLoginDisabled(true, "password", true), true);
});
test("Enter submits account authentication", () => {
diff --git a/app/src/config/tabs/accountAuth.ts b/app/src/config/tabs/accountAuth.ts
index 0dbd709e9a..e41dde2989 100644
--- a/app/src/config/tabs/accountAuth.ts
+++ b/app/src/config/tabs/accountAuth.ts
@@ -1,5 +1,5 @@
-export const isAccountLoginDisabled = (agreed: boolean, password: string) => {
- return !agreed || password.length === 0;
+export const isAccountLoginDisabled = (agreed: boolean, password: string, submitting = false) => {
+ return submitting || !agreed || password.length === 0;
};
export const bindAccountAuthEnter = (input: HTMLInputElement, submit: () => void) => {
diff --git a/app/src/config/tabs/accountUi.ts b/app/src/config/tabs/accountUi.ts
index 9d49ff399c..029bcce117 100644
--- a/app/src/config/tabs/accountUi.ts
+++ b/app/src/config/tabs/accountUi.ts
@@ -415,11 +415,15 @@ const bindAccountAuthForm = (
const login2Btn = authFormRoot.querySelector("#login2") as HTMLButtonElement;
let token = "";
let needCaptcha = "";
+ let submitting = false;
+ let updateLoginButton = () => {
+ loginBtn.disabled = submitting;
+ };
if (mode === "login") {
const agreeLoginCheckbox = authFormRoot.querySelector("#agreeLogin") as HTMLInputElement;
- const updateLoginButton = () => {
- loginBtn.disabled = isAccountLoginDisabled(agreeLoginCheckbox.checked, userPasswordInput.value);
+ updateLoginButton = () => {
+ loginBtn.disabled = isAccountLoginDisabled(agreeLoginCheckbox.checked, userPasswordInput.value, submitting);
};
agreeLoginCheckbox.addEventListener("change", updateLoginButton);
userPasswordInput.addEventListener("input", updateLoginButton);
@@ -441,9 +445,15 @@ const bindAccountAuthForm = (
bindAccountAuthEnter(captchaInput, () => loginBtn.click());
bindAccountAuthEnter(twofactorAuthCodeInput, () => login2Btn.click());
+ const finishSubmitting = () => {
+ submitting = false;
+ updateLoginButton();
+ login2Btn.disabled = false;
+ };
+
const completeLogin = (response: IWebSocketData) => {
if (mode === "login") {
- fetchPost("/api/setting/getCloudUser", {
+ return fetchPost("/api/setting/getCloudUser", {
token: response.data.token,
}, (userResponse) => {
const action = resolveCloudUserRefresh(userResponse.code, userResponse.data, userNameInput.value.trim());
@@ -459,9 +469,16 @@ const bindAccountAuthForm = (
} else if (mode === "deactivate") {
confirmDeactivateAccount();
}
+ return Promise.resolve();
};
loginBtn.addEventListener("click", () => {
+ if (submitting) {
+ return;
+ }
+ submitting = true;
+ updateLoginButton();
+ let completing = false;
fetchPost("/api/account/login", {
userName: userNameInput.value.trim(),
userPassword: md5(userPasswordInput.value),
@@ -487,10 +504,21 @@ const bindAccountAuthForm = (
token = loginResponse.data.token;
return;
}
- completeLogin(loginResponse);
+ completing = true;
+ completeLogin(loginResponse).finally(finishSubmitting);
+ }).finally(() => {
+ if (!completing) {
+ finishSubmitting();
+ }
});
});
login2Btn.addEventListener("click", () => {
+ if (submitting) {
+ return;
+ }
+ submitting = true;
+ login2Btn.disabled = true;
+ let completing = false;
fetchPost("/api/setting/login2faCloudUser", {
code: twofactorAuthCodeInput.value,
token,
@@ -500,7 +528,12 @@ const bindAccountAuthForm = (
twofactorAuthCodeInput.select();
return;
}
- completeLogin(faResponse);
+ completing = true;
+ completeLogin(faResponse).finally(finishSubmitting);
+ }).finally(() => {
+ if (!completing) {
+ finishSubmitting();
+ }
});
});
};
diff --git a/kernel/model/cloud_service.go b/kernel/model/cloud_service.go
index 6d87745993..5ffa052682 100644
--- a/kernel/model/cloud_service.go
+++ b/kernel/model/cloud_service.go
@@ -674,7 +674,8 @@ func Login(userName, password, captcha string, cloudRegion int) (ret *gulu.Resul
}
result := map[string]any{}
- request := httpclient.NewCloudRequest30s()
+ // 登录请求不是幂等操作,关闭自动重试以避免重复登录。
+ request := httpclient.NewCloudRequest30s().SetRetryCount(0)
resp, err := request.
SetSuccessResult(&result).
SetBody(map[string]string{"userName": userName, "userPassword": password, "captcha": captcha}).
@@ -711,7 +712,8 @@ func Login(userName, password, captcha string, cloudRegion int) (ret *gulu.Resul
func Login2fa(token, code string) (ret *gulu.Result) {
result := map[string]any{}
- request := httpclient.NewCloudRequest30s()
+ // 两步验证登录不是幂等操作,关闭自动重试以避免重复登录。
+ request := httpclient.NewCloudRequest30s().SetRetryCount(0)
resp, err := request.
SetSuccessResult(&result).
SetBody(map[string]string{"twofactorAuthCode": code}).