web/lib/api: wait for turnstile solution, refactor

now cobalt waits for turnstile for 15 seconds before showing an assistive dialog, instead of showing the dialog right away. much better ux!
This commit is contained in:
wukko
2025-05-23 20:37:47 +06:00
parent 1c5e038372
commit add0ab4adf
2 changed files with 41 additions and 31 deletions

View File

@@ -11,34 +11,56 @@ import { getServerInfo } from "$lib/api/server-info";
import type { Optional } from "$lib/types/generic";
import type { CobaltAPIResponse, CobaltErrorResponse, CobaltSaveRequestBody } from "$lib/types/api";
const waitForTurnstile = async () => {
return await new Promise((resolve, reject) => {
const unsub = turnstileSolved.subscribe((solved) => {
if (solved) {
unsub();
resolve(true);
}
});
// wait for turnstile to finish for 15 seconds
setTimeout(() => {
unsub();
reject(false);
}, 15 * 1000)
});
}
const getAuthorization = async () => {
const processing = get(settings).processing;
if (processing.enableCustomApiKey && processing.customApiKey.length > 0) {
return `Api-Key ${processing.customApiKey}`;
}
if (get(turnstileEnabled)) {
if (!get(turnstileSolved)) {
if (!get(turnstileEnabled)) {
return;
}
if (!get(turnstileSolved)) {
try {
await waitForTurnstile();
} catch {
return {
status: "error",
error: {
code: "error.captcha_ongoing"
code: "error.captcha_too_long"
}
} as CobaltErrorResponse;
}
const session = await getSession();
if (session) {
if ("error" in session) {
if (session.error.code !== "error.api.auth.not_configured") {
return session;
}
} else {
return `Bearer ${session.token}`;
}
}
}
if (processing.enableCustomApiKey && processing.customApiKey.length > 0) {
return `Api-Key ${processing.customApiKey}`;
const session = await getSession();
if (session) {
if ("error" in session) {
if (session.error.code !== "error.api.auth.not_configured") {
return session;
}
} else {
return `Bearer ${session.token}`;
}
}
}
@@ -100,25 +122,13 @@ const request = async (requestBody: CobaltSaveRequestBody, justRetried = false)
&& !justRetried
) {
resetSession();
await waitForTurnstile().catch(() => {});
await getAuthorization();
return request(requestBody, true);
}
return response;
}
const waitForTurnstile = async () => {
await getAuthorization();
return new Promise<void>(resolve => {
const unsub = turnstileSolved.subscribe(solved => {
if (solved) {
unsub();
resolve();
}
});
});
}
const probeCobaltTunnel = async (url: string) => {
const request = await fetch(`${url}&p=1`).catch(() => {});
if (request?.status === 200) {