web/api: re-request session if server claims it's invalid

This commit is contained in:
jj 2025-03-30 17:41:28 +00:00
parent 1f768df4ec
commit 59665af44a
No known key found for this signature in database
2 changed files with 28 additions and 4 deletions

View File

@ -3,7 +3,7 @@ import { get } from "svelte/store";
import settings from "$lib/state/settings";
import lazySettingGetter from "$lib/settings/lazy-get";
import { getSession } from "$lib/api/session";
import { getSession, resetSession } from "$lib/api/session";
import { currentApiURL } from "$lib/api/api-url";
import { turnstileEnabled, turnstileSolved } from "$lib/state/turnstile";
import cachedInfo from "$lib/state/server-info";
@ -43,10 +43,10 @@ const getAuthorization = async () => {
}
}
const request = async (url: string) => {
const request = async (url: string, justRetried = false) => {
const getSetting = lazySettingGetter(get(settings));
const request = {
const requestBody = {
url,
downloadMode: getSetting("save", "downloadMode"),
@ -100,7 +100,7 @@ const request = async (url: string) => {
method: "POST",
redirect: "manual",
signal: AbortSignal.timeout(20000),
body: JSON.stringify(request),
body: JSON.stringify(requestBody),
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
@ -119,9 +119,31 @@ const request = async (url: string) => {
}
});
if (
response?.status === 'error'
&& response?.error.code === 'error.api.auth.jwt.invalid'
&& !justRetried
) {
resetSession();
await waitForTurnstile().catch(() => {});
return request(url, 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) {

View File

@ -62,3 +62,5 @@ export const getSession = async () => {
}
return newSession;
}
export const resetSession = () => cache = undefined;