diff --git a/fe/src/App.vue b/fe/src/App.vue index 272b659..4b111c5 100644 --- a/fe/src/App.vue +++ b/fe/src/App.vue @@ -3,7 +3,7 @@ import {RouterView, useRoute} from 'vue-router' import HomeHeader from '@/views/components/HomeHeader/HomeHeader.vue' import HomeAside from '@/views/components/HomeAside/HomeAside.vue'; import RecoverySetupPrompt from '@/views/components/RecoverySetupPrompt/RecoverySetupPrompt.vue' -import {ref, watch, onMounted} from 'vue' +import {ref, watch} from 'vue' import {useGlobalStatusStore} from "@/stores/useGlobalStatusStore"; const route = useRoute() @@ -11,12 +11,6 @@ const pageName = ref(route.name) const globalStatus = useGlobalStatusStore(); const standalonePages = ['login', 'login-verification', 'force-password-change', 'setup', 'forgot-password', 'reset-blocked'] -onMounted(() => { - if (!standalonePages.includes(route.name)) { - globalStatus.init(() => {}); - } -}); - watch( () => route.fullPath, () => { diff --git a/fe/src/main.js b/fe/src/main.js index e0e2b44..9a98b3c 100644 --- a/fe/src/main.js +++ b/fe/src/main.js @@ -29,7 +29,12 @@ if (isReservedResetPath(window.location.pathname)) { } const app = createApp(App) +const pinia = createPinia() + +app.use(pinia) app.use(router) -app.use(createPinia()) -app.use(ElementPlus) -app.mount('#app') +app.use(ElementPlus) + +router.isReady().then(() => { + app.mount('#app') +}) diff --git a/fe/src/stores/useGlobalStatusStore.js b/fe/src/stores/useGlobalStatusStore.js index c23e00b..f9724fd 100644 --- a/fe/src/stores/useGlobalStatusStore.js +++ b/fe/src/stores/useGlobalStatusStore.js @@ -1,6 +1,8 @@ import { defineStore } from "pinia"; import { http } from "@/utils/axios"; +let authRefreshPromise = null; + const useGlobalStatusStore = defineStore("useGlobalStatusStore", { state() { return { @@ -27,33 +29,39 @@ const useGlobalStatusStore = defineStore("useGlobalStatusStore", { } }, actions: { - async refresh() { - if (this.authLoading) { - return this.isLogin; + refresh() { + if (authRefreshPromise) { + return authRefreshPromise; } this.authLoading = true; - try { - const response = await http.post("/api/v1/user/info", {}); - if (response.errorNo === 0 && response.data) { - this.setUser(response.data, false); - this.authChecked = true; - return true; + authRefreshPromise = (async () => { + try { + const response = await http.post("/api/v1/user/info", {}); + if (response.errorNo === 0 && response.data) { + this.setUser(response.data, false); + this.authChecked = true; + return true; + } + this.clear(); + return false; + } catch (error) { + this.clear(); + return false; + } finally { + this.authLoading = false; + authRefreshPromise = null; } - this.clear(); - return false; - } catch (error) { - this.clear(); - return false; - } finally { - this.authLoading = false; - } + })(); + + return authRefreshPromise; }, init(callback) { - this.refresh().then(() => { + return this.refresh().then((authenticated) => { if (typeof callback === "function") { - callback(); + callback(authenticated); } + return authenticated; }); }, clear() { diff --git a/fe/src/utils/axios.js b/fe/src/utils/axios.js index e92203c..5362b8d 100644 --- a/fe/src/utils/axios.js +++ b/fe/src/utils/axios.js @@ -63,7 +63,7 @@ http.interceptors.response.use(async (response) => { await router.replace({ path: '/login', query: { - redirect: router.currentRoute.fullPath + redirect: router.currentRoute.value.fullPath } }); } @@ -72,7 +72,7 @@ http.interceptors.response.use(async (response) => { await router.replace({ path: '/setup', query: { - redirect: router.currentRoute.fullPath + redirect: router.currentRoute.value.fullPath } }); } @@ -95,7 +95,7 @@ http.interceptors.response.use(async (response) => { await router.replace({ path: '/login', query: { - redirect: router.currentRoute.fullPath + redirect: router.currentRoute.value.fullPath } }); }