1
0

More refactoring

This commit is contained in:
Zef Hemel 2023-07-27 12:37:39 +02:00
parent 4d0f36d475
commit bfce343e7b
2 changed files with 18 additions and 7 deletions

View File

@ -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" });
}
}

View File

@ -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);