Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Refactor periodic authentication checking
Rūdolfs Ošiņš committed 1 year ago
commit af776b554f8575d7f67b83d116562f0246943a3c
parent 18a3d6fec3568a35899b6f3bc9f39db53a464922
2 files changed +16 -31
modified src/App.svelte
@@ -23,7 +23,7 @@
  import Patch from "@app/views/repo/Patch.svelte";
  import Patches from "@app/views/repo/Patches.svelte";
  import Repos from "./views/home/Repos.svelte";
-
  import { dynamicInterval, checkAuth, authenticated } from "./lib/auth";
+
  import { authenticated, checkAuthPeriodically } from "./lib/auth";

  const activeRouteStore = router.activeRouteStore;

@@ -43,26 +43,7 @@
      });
    }

-
    try {
-
      await invoke("authenticate");
-
      authenticated.set(true);
-
      void router.loadFromLocation();
-
      void dynamicInterval(
-
        checkAuth,
-
        import.meta.env.VITE_AUTH_LONG_DELAY || 30_000,
-
      );
-
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
-
    } catch (e: any) {
-
      authenticated.set(false);
-
      void router.push({
-
        resource: "authenticationError",
-
        params: {
-
          error: e.err,
-
          hint: e.hint,
-
        },
-
      });
-
      void dynamicInterval(checkAuth, 1000);
-
    }
+
    await checkAuthPeriodically(true);

    if (window.__TAURI_OS_PLUGIN_INTERNALS__ && platform() === "macos") {
      await setNotificationBadge();
modified src/lib/auth.ts
@@ -5,11 +5,12 @@ import * as router from "@app/lib/router";
import { activeRouteStore } from "@app/lib/router";
import { invoke } from "@app/lib/invoke";

-
let intervalId: ReturnType<typeof setTimeout>;
-

export const authenticated = writable<boolean>(false);

-
export function dynamicInterval(callback: () => void, period: number) {
+
let lock = false;
+
let intervalId: ReturnType<typeof setTimeout>;
+

+
function dynamicInterval(callback: () => void, period: number) {
  clearTimeout(intervalId);

  intervalId = setTimeout(() => {
@@ -18,9 +19,7 @@ export function dynamicInterval(callback: () => void, period: number) {
  }, period);
}

-
let lock = false;
-

-
export async function checkAuth() {
+
export async function checkAuthPeriodically(appLaunch: boolean = false) {
  try {
    if (lock) {
      return;
@@ -28,14 +27,19 @@ export async function checkAuth() {
    lock = true;
    await invoke("authenticate");
    authenticated.set(true);
-
    if (get(activeRouteStore).resource === "authenticationError") {
+
    if (appLaunch) {
+
      await router.loadFromLocation();
+
    } else if (get(activeRouteStore).resource === "authenticationError") {
      window.history.back();
    }
-
    dynamicInterval(checkAuth, import.meta.env.VITE_AUTH_LONG_DELAY || 30_000);
+
    dynamicInterval(
+
      checkAuthPeriodically,
+
      import.meta.env.VITE_AUTH_LONG_DELAY || 30_000,
+
    );
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  } catch (e: any) {
    authenticated.set(false);
-
    if (get(activeRouteStore).resource !== "authenticationError") {
+
    if (appLaunch || get(activeRouteStore).resource !== "authenticationError") {
      await router.push({
        resource: "authenticationError",
        params: {
@@ -43,7 +47,7 @@ export async function checkAuth() {
          hint: e.hint,
        },
      });
-
      dynamicInterval(checkAuth, 1000);
+
      dynamicInterval(checkAuthPeriodically, 1000);
    }
  } finally {
    lock = false;