mirror of
https://seed.radicle.dev/rad:z4D5UCArafTzTQpDZNQRuqswh3ury.git
synced 2026-11-28 22:30:27 +08:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { invoke } from "@app/lib/invoke";
|
|
import { activeRouteStore } from "@app/lib/router";
|
|
import { get } from "svelte/store";
|
|
import * as router from "@app/lib/router";
|
|
|
|
let intervalId: ReturnType<typeof setTimeout>;
|
|
|
|
export function dynamicInterval(callback: () => void, period: number) {
|
|
clearTimeout(intervalId);
|
|
|
|
intervalId = setTimeout(() => {
|
|
callback();
|
|
dynamicInterval(callback, period);
|
|
}, period);
|
|
}
|
|
|
|
let lock = false;
|
|
|
|
export async function checkAuth() {
|
|
try {
|
|
if (lock) {
|
|
return;
|
|
}
|
|
lock = true;
|
|
await invoke("authenticate");
|
|
if (get(activeRouteStore).resource === "authenticationError") {
|
|
window.history.back();
|
|
}
|
|
dynamicInterval(checkAuth, import.meta.env.VITE_AUTH_LONG_DELAY || 30_000);
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} catch (e: any) {
|
|
if (get(activeRouteStore).resource !== "authenticationError") {
|
|
await router.push({
|
|
resource: "authenticationError",
|
|
params: {
|
|
error: e.err,
|
|
hint: e.hint,
|
|
},
|
|
});
|
|
dynamicInterval(checkAuth, 1000);
|
|
}
|
|
} finally {
|
|
lock = false;
|
|
}
|
|
}
|