From bfce343e7bb213da65e8150a8e0976bf0426ff10 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Thu, 27 Jul 2023 12:37:39 +0200 Subject: [PATCH] More refactoring --- common/spaces/http_space_primitives.ts | 8 +++++++- web/client.ts | 17 +++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/common/spaces/http_space_primitives.ts b/common/spaces/http_space_primitives.ts index 0d80d581..3d7d1981 100644 --- a/common/spaces/http_space_primitives.ts +++ b/common/spaces/http_space_primitives.ts @@ -33,7 +33,7 @@ export class HttpSpacePrimitives implements SpacePrimitives { result.url, ); location.href = result.url; - throw new Error("Invalid credentials"); + throw new Error("Not authenticated"); } return result; } catch (e: any) { @@ -155,4 +155,10 @@ export class HttpSpacePrimitives implements SpacePrimitives { perm: (res.headers.get("X-Permission") as "rw" | "ro") || "rw", }; } + + // Used to check if the server is reachable and the user is authenticated + // If not: throws an error or invokes a redirect + async ping() { + await this.authenticatedFetch(`${this.url}/SETTINGS.md`, { method: "GET" }); + } } diff --git a/web/client.ts b/web/client.ts index 58dd87b1..1bc25baf 100644 --- a/web/client.ts +++ b/web/client.ts @@ -134,7 +134,7 @@ export class Client { this.focus(); - // This constructor will always be followed by an invocatition of init() + // This constructor will always be followed by an (async) invocatition of init() } /** @@ -145,17 +145,22 @@ export class Client { // Load settings this.settings = await this.loadSettings(); - this.initNavigator(); - // Pinging a remote space to ensure we're authenticated properly, if not will result in a redirect to auth page try { - await this.remoteSpacePrimitives.getFileMeta("SETTINGS"); - } catch { + await this.remoteSpacePrimitives.ping(); + } catch (e: any) { + if (e.message === "Not authenticated") { + console.warn("Not authenticated, redirecting to auth page"); + return; + } console.warn( - "Could not reach remote server, either we're offline or not authenticated", + "Could not reach remote server, we're offline or the server is down", + e, ); } + this.initNavigator(); + await this.reloadPlugs(); this.loadCustomStyles().catch(console.error);