Skip to content

Commit

Permalink
feat(ui): add commonRouteGuard for route protection and initialization
Browse files Browse the repository at this point in the history
Add a route guard to handle site settings retrieval and ensure proper
navigation based on site initialization status. This enhances security
and dynamic content management.
  • Loading branch information
ShiinaKin committed Oct 28, 2024
1 parent 0643090 commit fb1120f
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion ui/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -23,6 +28,7 @@ const router = createRouter({
component: () => import("@/views/auth/RegisterView.vue")
}
],
beforeEnter: (to, from) => commonRouteGuard(to, from)
},
{
path: "/:pathMatch(.*)",
Expand All @@ -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);
});
}
};

0 comments on commit fb1120f

Please sign in to comment.