mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 03:40:37 +08:00
Check auth status periodically
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
import Issues from "@app/views/repo/Issues.svelte";
|
||||
import Patch from "@app/views/repo/Patch.svelte";
|
||||
import Patches from "@app/views/repo/Patches.svelte";
|
||||
import { dynamicInterval, checkAuth } from "./lib/auth";
|
||||
|
||||
const activeRouteStore = router.activeRouteStore;
|
||||
|
||||
@@ -38,6 +39,7 @@
|
||||
try {
|
||||
await invoke("authenticate");
|
||||
void router.loadFromLocation();
|
||||
void dynamicInterval(checkAuth, 30_000);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} catch (e: any) {
|
||||
void router.push({
|
||||
@@ -47,6 +49,7 @@
|
||||
hint: e.hint,
|
||||
},
|
||||
});
|
||||
void dynamicInterval(checkAuth, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
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, 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user