diff --git a/src/App.svelte b/src/App.svelte index 52f853c..d9db3ea 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -18,14 +18,8 @@ event.preventDefault(); const host = event.target.host.value; - client.init(host).then(() => { - if (client.instance.type === server_types.INCOMPATIBLE) { - console.error("Server " + client.instance.host + " is not supported - " + client.instance.version); - alert("Sorry, this app is not compatible with " + client.instance.host + "!"); - return; - } - console.log("Server is \"" + client.instance.type + "\" (or compatible)."); - client.save(); + client.init(host).then(res => { + if (!res) return; let oauth_url = client.getOAuthUrl(); location = oauth_url; }); diff --git a/src/client/client.js b/src/client/client.js index d49ce2e..91f19b8 100644 --- a/src/client/client.js +++ b/src/client/client.js @@ -40,14 +40,20 @@ export class Client { client = new Client(); window.peekie = client; client.load(); - if (client.instance) client.#configureAPI(); + if (client.instance && client.instance !== server_types.INCOMPATIBLE) + client.#configureAPI(); return client; } async init(host) { if (host.startsWith("https://")) host = host.substring(8); - const url = "https://" + host + "/api/v1/instance"; - const data = await fetch(url).then(res => res.json()); + const url = `https://${host}/api/v1/instance`; + const data = await fetch(url).then(res => res.json()).catch(error => { console.log(error) }); + if (!data) { + console.error(`Failed to connect to ${host}`); + alert(`Failed to connect to ${host}! Please try again later.`); + return false; + } this.instance = { host: host, version: data.version, @@ -62,6 +68,14 @@ export class Client { } } + if (this.instance.type == server_types.INCOMPATIBLE) { + console.error(`Server ${host} is not supported - ${data.version}`); + alert(`Sorry, this app is not compatible with ${host}!`); + return false; + } + + console.log(`Server is "${client.instance.type}" (or compatible).`); + this.#configureAPI(); this.app = await this.api.createApp(host); @@ -70,7 +84,9 @@ export class Client { return false; } - return this.auth; + this.save(); + + return true; } #configureAPI() {