diff --git a/src/api/API.ts b/src/api/API.ts index 8067199..301bf5a 100644 --- a/src/api/API.ts +++ b/src/api/API.ts @@ -527,8 +527,12 @@ export class API { ); const resp = await fetch(request); await checkResponse(resp); - const data = (await resp.json()) as { data: NewTOTPResp }; - return data.data; + try { + const data = (await resp.json()) as { data: NewTOTPResp }; + return data.data; + } catch { + return null; + } } async deleteTOTP(baseMount: string, name: string): Promise { diff --git a/src/main.tsx b/src/main.tsx index d04177a..faf4ab8 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -35,6 +35,7 @@ import { playground } from "./playground"; import { render } from "preact"; import { settings } from "./globalSettings"; import i18next from "i18next"; +import { prePageChecks } from "./pageUtils"; async function onLoad(): Promise { document.documentElement.dir = settings.pageDirection; @@ -70,6 +71,19 @@ async function onLoad(): Promise { } } }, 5000); + + + // DO NOT DO THIS + // THIS IS BAD + // I DO NOT RECOMMEND THIS + // WE NEED A BETTER WAY + // THIS IS **TRASH** + // DO NOT + // BAD + setInterval(async () => { + if (["/set_language", "/set_vault_url", "/unseal", "/login", "/me", "/pw_gen"].includes(getCurrentUrl())) return; + await prePageChecks(api, settings); + }, 1000) } document.addEventListener( diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 5b98b5c..84b6590 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -5,34 +5,42 @@ import ClipboardJS from "clipboard"; import UIkit from "uikit"; import i18next from "i18next"; -async function prePageChecksReal(api: API, settings: Settings) { +async function prePageChecksReal(api: API, settings: Settings): Promise { if (settings.language.length == 0) { + console.log("set languge") route("/set_language", true); - throw new Error("Language Not Set"); + return true } - if (!settings.apiURL) { - route("/set_vault_url", true); - throw new Error("Vault URL Not Set"); + if (settings.apiURL.length == 0) { + route("/set_vault_url", false); + return true } const sealStatus = await api.getSealStatus(); if (sealStatus.sealed) { route("/unseal", true); - throw new Error("Vault Sealed"); + return true } try { await api.lookupSelf(); } catch (e) { route("/login", true); - throw e; + return true; } + return false; } +// ????????????????????????????? +// return trues if checks failed +// return false if checks passed +// ????????????????????????????? export async function prePageChecks(api: API, settings: Settings): Promise { try { - await prePageChecksReal(api, settings); + if (await prePageChecksReal(api, settings)) { + return false; + } } catch (e) { console.log("OHNO", e); return false; diff --git a/src/pages.tsx b/src/pages.tsx index fa6388c..2714999 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -90,7 +90,7 @@ export const Main = () => ( diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index 7a46e05..37f3e09 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -24,7 +24,7 @@ export class Settings { get apiURL(): string | null { const apiurl = this.storage.getItem("apiURL") || ""; - return apiurl.length > 0 ? apiurl : null; + return apiurl; } set apiURL(value: string) { this.storage.setItem("apiURL", value); diff --git a/src/ui/pages/Home.tsx b/src/ui/pages/Home.tsx index 0ec4dbe..2608557 100644 --- a/src/ui/pages/Home.tsx +++ b/src/ui/pages/Home.tsx @@ -17,8 +17,6 @@ type HomeState = { export class Home extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.api, this.props.settings))) return; - let selfTokenInfo: TokenInfo; try { selfTokenInfo = await this.props.api.lookupSelf();