diff --git a/resources/js/api/index.ts b/resources/js/api/index.ts index d305a37..7b20853 100644 --- a/resources/js/api/index.ts +++ b/resources/js/api/index.ts @@ -59,10 +59,14 @@ export class ApiService { const appStore = useAppStore(); return new Promise((resolve, reject) => { + if (!appStore.config.url) { + reject({ field: 'Error', message: 'No URL provided' }); + } + ApiService.request({ url: `${appStore.config.url}graphql${schema}`, data, - credentials: useAppStore().isMultiTenant ? 'include' : 'omit', + credentials: appStore.isMultiTenant ? 'include' : 'omit', }).then((res: any) => { if (res.errors) { const message = res.errors[0].message; diff --git a/resources/js/components/pages/Setup.vue b/resources/js/components/pages/Setup.vue index 147ed16..6874670 100644 --- a/resources/js/components/pages/Setup.vue +++ b/resources/js/components/pages/Setup.vue @@ -31,7 +31,7 @@ diff --git a/resources/js/components/pages/auth/Login.vue b/resources/js/components/pages/auth/Login.vue index 752d632..5df6c4b 100644 --- a/resources/js/components/pages/auth/Login.vue +++ b/resources/js/components/pages/auth/Login.vue @@ -91,9 +91,10 @@ const login = async () => { isLoading.value = false; return; } - snackbar.success({ title: 'Logged in successfully', save: false }); - await appStore.init(); - redirectToCollections(); + if (await appStore.init()) { + snackbar.success({ title: 'Logged in successfully', save: false }); + redirectToCollections(); + } } catch (e) { if (snackbarErrors(e)) return; snackbar.error({ @@ -118,7 +119,7 @@ const checkVerified = () => { (async () => { checkVerified(); - if (appStore.loggedIn) { + if (appStore.hasValidConfig) { redirectToCollections(); } })(); diff --git a/resources/js/components/pages/auth/Register.vue b/resources/js/components/pages/auth/Register.vue index 7e61313..253b9b5 100644 --- a/resources/js/components/pages/auth/Register.vue +++ b/resources/js/components/pages/auth/Register.vue @@ -119,7 +119,7 @@ const register = async () => { }; (async () => { - if (appStore.loggedIn) { + if (appStore.hasValidConfig) { redirectToLogin(); } })(); diff --git a/resources/js/components/pages/auth/RequestResetPassword.vue b/resources/js/components/pages/auth/RequestResetPassword.vue index 5a95d3b..8f5808b 100644 --- a/resources/js/components/pages/auth/RequestResetPassword.vue +++ b/resources/js/components/pages/auth/RequestResetPassword.vue @@ -93,7 +93,7 @@ const requestReset = async () => { }; (async () => { - if (appStore.loggedIn) { + if (appStore.hasValidConfig) { redirectToCollections(); } })(); diff --git a/resources/js/components/pages/auth/ResetPassword.vue b/resources/js/components/pages/auth/ResetPassword.vue index dd83caa..990c7bf 100644 --- a/resources/js/components/pages/auth/ResetPassword.vue +++ b/resources/js/components/pages/auth/ResetPassword.vue @@ -106,7 +106,7 @@ const resetPassword = async () => { (async () => { email.value = route.query.email as string; - if (appStore.loggedIn) { + if (appStore.hasValidConfig) { redirectToCollections(); } })(); diff --git a/resources/js/store/index.ts b/resources/js/store/index.ts index 6a350fc..627641e 100644 --- a/resources/js/store/index.ts +++ b/resources/js/store/index.ts @@ -21,11 +21,7 @@ const RPC_URLS = { }; const parseConfigURL = (url: string): URL => { - try { - return new URL(url); - } catch { - return new URL('https://' + url); - } + return new URL(url); }; const updateTransaction = async ({ @@ -115,7 +111,6 @@ export const useAppStore = defineStore('app', { this.setConfig(); if (!this.config.url) return false; - if (this.isMultiTenant && this.loggedIn) await this.getUser(); const urlConfig = await this.checkURL(this.config.url); this.config.network = urlConfig.network; this.config.packages = Object.entries(urlConfig.packages).map(([key, value]: any[]) => { @@ -131,14 +126,17 @@ export const useAppStore = defineStore('app', { link, }; }); - if (this.hasBeamPackage) this.addBeamNavigation(); if (this.hasFuelTanksPackage) this.addFuelTanksNavigation(); if (this.hasMarketplacePackage) this.addMarketplaceNavigation(); + this.loggedIn = true; + if (this.isMultiTenant && this.loggedIn) await this.getUser(); + return await this.fetchCollectionIds(); } catch (error: any) { snackbar.error({ title: error }); + this.clearLogin(); } return false; @@ -146,23 +144,34 @@ export const useAppStore = defineStore('app', { async setupAccount({ url, authorization_token }: { url: URL; authorization_token: string }) { this.url = url; this.authorization_token = authorization_token; - this.config.authorization_token = authorization_token; - this.loggedIn = true; return await this.init(); }, setConfig() { - if (appConfig?.url) this.config.url = parseConfigURL(appConfig.url); - else if (window?.bootstrap?.hostname) this.config.url = parseConfigURL(window.location.origin); - else this.config.url = this.url; + if (appConfig?.tenant) { + this.config.tenant = appConfig.tenant === 'true'; + } - if (appConfig?.authorization_token?.length) this.config.authorization_token = appConfig.authorization_token; - else this.config.authorization_token = this.authorization_token; + if (appConfig?.url) { + this.config.url = parseConfigURL(appConfig.url); + } else if (window?.bootstrap?.hostname) { + this.config.url = parseConfigURL(window.location.origin); + } else { + this.config.url = this.url; + } - if (appConfig?.tenant) this.config.tenant = appConfig.tenant === 'true'; + if (!this.config.tenant && appConfig?.authorization_token?.length) { + this.config.authorization_token = appConfig.authorization_token; + } else if (!this.config.tenant) { + this.config.authorization_token = this.authorization_token; + } - if (appConfig.websocket.length) this.config.webSocket = appConfig.websocket; - if (appConfig.channel.length) this.config.channel = appConfig.channel; + if (appConfig.websocket.length) { + this.config.webSocket = appConfig.websocket; + } + if (appConfig.channel.length) { + this.config.channel = appConfig.channel; + } }, async checkURL(url: URL) { try { @@ -431,7 +440,7 @@ export const useAppStore = defineStore('app', { return state.loggedIn && state.user?.apiTokens?.length > 0 && state.user?.account; } - return state.loggedIn && state.config.url && state.config.authorization_token.length > 0; + return state.loggedIn && state.config.url; }, isMultiTenant(state: AppState) { return state.config.tenant; diff --git a/resources/js/util/auth.guard.ts b/resources/js/util/auth.guard.ts index 1f9cbc2..0cfa75f 100644 --- a/resources/js/util/auth.guard.ts +++ b/resources/js/util/auth.guard.ts @@ -7,7 +7,6 @@ export function initAuthGuard(router: Router) { const validConfig = appStore.hasValidConfig; const isMultiTenant = appStore.isMultiTenant; - const isLoggedIn = appStore.loggedIn; const requiresAuth = to.matched.some((record) => record.meta.requiresAuth); const requiresToken = to.matched.some((record) => record.meta.requiresToken); @@ -21,21 +20,21 @@ export function initAuthGuard(router: Router) { return; } - if (requiresAuth && !isLoggedIn) { + if (requiresAuth && !validConfig) { next({ name: 'platform.auth.login' }); } else if (requiresToken && appStore.user && !validConfig) { next({ name: 'platform.user.settings' }); - } else if (to.name == 'platform.auth.login' && isLoggedIn) { + } else if (to.name == 'platform.auth.login' && validConfig) { next({ name: 'platform.collections' }); } else { next(); } } else { - if (requiresAuth && (!validConfig || !isLoggedIn)) { + if (requiresAuth && !validConfig) { next({ name: 'platform.setup' }); - } else if (to.name == 'platform.auth.login' && !isLoggedIn) { + } else if (to.name == 'platform.auth.login' && !validConfig) { next({ name: 'platform.setup' }); - } else if (to.name == 'platform.setup' && validConfig && isLoggedIn) { + } else if (to.name == 'platform.setup' && validConfig) { next({ name: 'platform.collections' }); } else { next();