Add direct login button handler
This commit is contained in:
parent
56e49b27a2
commit
8d075ac401
@ -2,6 +2,7 @@ const HOTNESS_AUTH_TOKEN_KEY = "video-hotness-auth-token-v1";
|
|||||||
const authGate = document.querySelector("#auth-gate");
|
const authGate = document.querySelector("#auth-gate");
|
||||||
const authForm = document.querySelector("#auth-form");
|
const authForm = document.querySelector("#auth-form");
|
||||||
const authPassword = document.querySelector("#auth-password");
|
const authPassword = document.querySelector("#auth-password");
|
||||||
|
const authSubmit = document.querySelector("#auth-submit");
|
||||||
const authMessage = document.querySelector("#auth-message");
|
const authMessage = document.querySelector("#auth-message");
|
||||||
const form = document.querySelector("#collect-form");
|
const form = document.querySelector("#collect-form");
|
||||||
const input = document.querySelector("#program-name");
|
const input = document.querySelector("#program-name");
|
||||||
@ -110,12 +111,18 @@ let resolveTimer = 0;
|
|||||||
let resolveRequestId = 0;
|
let resolveRequestId = 0;
|
||||||
let temporaryQueryItems = [];
|
let temporaryQueryItems = [];
|
||||||
let appStarted = false;
|
let appStarted = false;
|
||||||
|
let authSubmitting = false;
|
||||||
|
|
||||||
authForm?.addEventListener("submit", async (event) => {
|
authForm?.addEventListener("submit", async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await submitAccessPassword();
|
await submitAccessPassword();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
authSubmit?.addEventListener("click", async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
await submitAccessPassword();
|
||||||
|
});
|
||||||
|
|
||||||
for (const [platform, element] of Object.entries(urlInputs)) {
|
for (const [platform, element] of Object.entries(urlInputs)) {
|
||||||
element.addEventListener("input", () => {
|
element.addEventListener("input", () => {
|
||||||
dirtyUrlInputs.add(platform);
|
dirtyUrlInputs.add(platform);
|
||||||
@ -2138,11 +2145,14 @@ async function ensureAccessAuth() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function submitAccessPassword() {
|
async function submitAccessPassword() {
|
||||||
|
if (authSubmitting) return;
|
||||||
const password = authPassword?.value || "";
|
const password = authPassword?.value || "";
|
||||||
if (!password.trim()) {
|
if (!password.trim()) {
|
||||||
showAuthGate("请输入访问密码");
|
showAuthGate("请输入访问密码");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
authSubmitting = true;
|
||||||
|
if (authSubmit) authSubmit.disabled = true;
|
||||||
setAuthMessage("正在验证...");
|
setAuthMessage("正在验证...");
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/auth/login", {
|
const response = await fetch("/api/auth/login", {
|
||||||
@ -2154,10 +2164,14 @@ async function submitAccessPassword() {
|
|||||||
if (!response.ok) throw new Error(payload.error || "访问密码不正确");
|
if (!response.ok) throw new Error(payload.error || "访问密码不正确");
|
||||||
if (payload.token) localStorage.setItem(HOTNESS_AUTH_TOKEN_KEY, payload.token);
|
if (payload.token) localStorage.setItem(HOTNESS_AUTH_TOKEN_KEY, payload.token);
|
||||||
if (authPassword) authPassword.value = "";
|
if (authPassword) authPassword.value = "";
|
||||||
|
setAuthMessage("登录成功,正在进入...");
|
||||||
hideAuthGate();
|
hideAuthGate();
|
||||||
startApp();
|
startApp();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showAuthGate(error.message || "访问密码不正确");
|
showAuthGate(error.message || "访问密码不正确");
|
||||||
|
} finally {
|
||||||
|
authSubmitting = false;
|
||||||
|
if (authSubmit) authSubmit.disabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ const HOTNESS_AUTH_TOKEN_KEY = "video-hotness-auth-token-v1";
|
|||||||
const authGate = document.querySelector("#auth-gate");
|
const authGate = document.querySelector("#auth-gate");
|
||||||
const authForm = document.querySelector("#auth-form");
|
const authForm = document.querySelector("#auth-form");
|
||||||
const authPassword = document.querySelector("#auth-password");
|
const authPassword = document.querySelector("#auth-password");
|
||||||
|
const authSubmit = document.querySelector("#auth-submit");
|
||||||
const authMessage = document.querySelector("#auth-message");
|
const authMessage = document.querySelector("#auth-message");
|
||||||
const form = document.querySelector("#collect-form");
|
const form = document.querySelector("#collect-form");
|
||||||
const input = document.querySelector("#program-name");
|
const input = document.querySelector("#program-name");
|
||||||
@ -62,12 +63,18 @@ let activeName = "";
|
|||||||
let dirtyUrlInputs = new Set();
|
let dirtyUrlInputs = new Set();
|
||||||
let deferredInstallPrompt = null;
|
let deferredInstallPrompt = null;
|
||||||
let appStarted = false;
|
let appStarted = false;
|
||||||
|
let authSubmitting = false;
|
||||||
|
|
||||||
authForm?.addEventListener("submit", async (event) => {
|
authForm?.addEventListener("submit", async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await submitAccessPassword();
|
await submitAccessPassword();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
authSubmit?.addEventListener("click", async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
await submitAccessPassword();
|
||||||
|
});
|
||||||
|
|
||||||
for (const [platform, element] of Object.entries(urlInputs)) {
|
for (const [platform, element] of Object.entries(urlInputs)) {
|
||||||
element.addEventListener("input", () => {
|
element.addEventListener("input", () => {
|
||||||
dirtyUrlInputs.add(platform);
|
dirtyUrlInputs.add(platform);
|
||||||
@ -734,11 +741,14 @@ async function ensureAccessAuth() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function submitAccessPassword() {
|
async function submitAccessPassword() {
|
||||||
|
if (authSubmitting) return;
|
||||||
const password = authPassword?.value || "";
|
const password = authPassword?.value || "";
|
||||||
if (!password.trim()) {
|
if (!password.trim()) {
|
||||||
showAuthGate("请输入访问密码");
|
showAuthGate("请输入访问密码");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
authSubmitting = true;
|
||||||
|
if (authSubmit) authSubmit.disabled = true;
|
||||||
setAuthMessage("正在验证...");
|
setAuthMessage("正在验证...");
|
||||||
try {
|
try {
|
||||||
const response = await fetch(apiUrl("/api/auth/login"), {
|
const response = await fetch(apiUrl("/api/auth/login"), {
|
||||||
@ -750,10 +760,14 @@ async function submitAccessPassword() {
|
|||||||
if (!response.ok) throw new Error(payload.error || "访问密码不正确");
|
if (!response.ok) throw new Error(payload.error || "访问密码不正确");
|
||||||
if (payload.token) localStorage.setItem(HOTNESS_AUTH_TOKEN_KEY, payload.token);
|
if (payload.token) localStorage.setItem(HOTNESS_AUTH_TOKEN_KEY, payload.token);
|
||||||
if (authPassword) authPassword.value = "";
|
if (authPassword) authPassword.value = "";
|
||||||
|
setAuthMessage("登录成功,正在进入...");
|
||||||
hideAuthGate();
|
hideAuthGate();
|
||||||
await startApp();
|
await startApp();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showAuthGate(error.message || "访问密码不正确");
|
showAuthGate(error.message || "访问密码不正确");
|
||||||
|
} finally {
|
||||||
|
authSubmitting = false;
|
||||||
|
if (authSubmit) authSubmit.disabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,10 +31,13 @@ test("desktop page has a password gate and sends auth token with API calls", ()
|
|||||||
|
|
||||||
test("desktop login submit is bound before the rest of the app can fail", () => {
|
test("desktop login submit is bound before the rest of the app can fail", () => {
|
||||||
const authBinding = desktopJs.indexOf('authForm?.addEventListener("submit"');
|
const authBinding = desktopJs.indexOf('authForm?.addEventListener("submit"');
|
||||||
|
const authClickBinding = desktopJs.indexOf('authSubmit?.addEventListener("click"');
|
||||||
const collectBinding = desktopJs.indexOf('form.addEventListener("submit"');
|
const collectBinding = desktopJs.indexOf('form.addEventListener("submit"');
|
||||||
assert.ok(authBinding > -1, "auth submit binding should exist");
|
assert.ok(authBinding > -1, "auth submit binding should exist");
|
||||||
|
assert.ok(authClickBinding > -1, "auth button click binding should exist");
|
||||||
assert.ok(collectBinding > -1, "collect submit binding should exist");
|
assert.ok(collectBinding > -1, "collect submit binding should exist");
|
||||||
assert.ok(authBinding < collectBinding, "auth binding must run before normal app bindings");
|
assert.ok(authBinding < collectBinding, "auth binding must run before normal app bindings");
|
||||||
|
assert.ok(authClickBinding < collectBinding, "auth click binding must run before normal app bindings");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("mobile page has the same password gate for cloud use", () => {
|
test("mobile page has the same password gate for cloud use", () => {
|
||||||
@ -49,8 +52,11 @@ test("mobile page has the same password gate for cloud use", () => {
|
|||||||
|
|
||||||
test("mobile login submit is bound before normal capture events", () => {
|
test("mobile login submit is bound before normal capture events", () => {
|
||||||
const authBinding = mobileJs.indexOf('authForm?.addEventListener("submit"');
|
const authBinding = mobileJs.indexOf('authForm?.addEventListener("submit"');
|
||||||
|
const authClickBinding = mobileJs.indexOf('authSubmit?.addEventListener("click"');
|
||||||
const collectBinding = mobileJs.indexOf('form.addEventListener("submit"');
|
const collectBinding = mobileJs.indexOf('form.addEventListener("submit"');
|
||||||
assert.ok(authBinding > -1, "auth submit binding should exist");
|
assert.ok(authBinding > -1, "auth submit binding should exist");
|
||||||
|
assert.ok(authClickBinding > -1, "auth button click binding should exist");
|
||||||
assert.ok(collectBinding > -1, "collect submit binding should exist");
|
assert.ok(collectBinding > -1, "collect submit binding should exist");
|
||||||
assert.ok(authBinding < collectBinding, "auth binding must run before normal app bindings");
|
assert.ok(authBinding < collectBinding, "auth binding must run before normal app bindings");
|
||||||
|
assert.ok(authClickBinding < collectBinding, "auth click binding must run before normal app bindings");
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user