diff --git a/ui/src/router/index.ts b/ui/src/router/index.ts index 12f8819d..a61e7118 100644 --- a/ui/src/router/index.ts +++ b/ui/src/router/index.ts @@ -1,4 +1,9 @@ -import { createRouter, createWebHistory } from "vue-router"; +import { + createRouter, + createWebHistory, + type RouteLocationNormalizedGeneric, + type RouteLocationNormalizedLoadedGeneric +} from "vue-router"; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -23,6 +28,7 @@ const router = createRouter({ component: () => import("@/views/auth/RegisterView.vue") } ], + beforeEnter: (to, from) => commonRouteGuard(to, from) }, { path: "/:pathMatch(.*)", @@ -33,3 +39,31 @@ const router = createRouter({ }); export default router; + +import { useCommonStore } from "@/stores/counter"; +import { CommonApi } from "api-client"; + +const commonRouteGuard = async (to: RouteLocationNormalizedGeneric, from: RouteLocationNormalizedLoadedGeneric) => { + if (to.name === from.name) return; + + if (to.name === "home" || (from.name === "siteInit" && to.name === "login")) { + const commonApi = new CommonApi(); + const commonStore = useCommonStore(); + + return commonApi + .apiSiteSettingGet() + .then((response) => { + if (response.isSuccessful) { + const commonSiteSetting = response.data!!; + commonStore.setTitle(commonSiteSetting.siteTitle!); + commonStore.setSubTitle(commonSiteSetting.siteSubTitle!); + commonStore.setAllowSignup(commonSiteSetting.siteAllowSignup!); + if (commonSiteSetting.isSiteInit!!) return { name: "login" }; + else return { name: "siteInit" }; + } + }) + .catch((error) => { + console.error(error); + }); + } +};