2023-08-11 01:10:35 +00:00
|
|
|
import { useGlobalInfoStore } from "@/stores/global-info";
|
|
|
|
import { useUserStore } from "@/stores/user";
|
2022-09-30 09:46:19 +00:00
|
|
|
import type { Router } from "vue-router";
|
|
|
|
|
|
|
|
export function setupCheckStatesGuard(router: Router) {
|
2024-09-28 10:19:41 +00:00
|
|
|
router.beforeEach(async (to, _, next) => {
|
2023-08-11 01:10:35 +00:00
|
|
|
const userStore = useUserStore();
|
|
|
|
const { globalInfo } = useGlobalInfoStore();
|
|
|
|
const { userInitialized, dataInitialized } = globalInfo || {};
|
|
|
|
|
|
|
|
if (to.name === "Setup" && userInitialized) {
|
|
|
|
next({ name: "Dashboard" });
|
2022-10-11 09:00:14 +00:00
|
|
|
return;
|
2022-09-30 09:46:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 01:10:35 +00:00
|
|
|
if (to.name === "SetupInitialData" && dataInitialized) {
|
|
|
|
next({ name: "Dashboard" });
|
|
|
|
return;
|
|
|
|
}
|
2022-09-30 09:46:19 +00:00
|
|
|
|
2023-08-11 01:10:35 +00:00
|
|
|
if (userInitialized === false && to.name !== "Setup") {
|
2022-09-30 09:46:19 +00:00
|
|
|
next({ name: "Setup" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-11 01:10:35 +00:00
|
|
|
if (
|
|
|
|
dataInitialized === false &&
|
|
|
|
!userStore.isAnonymous &&
|
|
|
|
to.name !== "SetupInitialData"
|
|
|
|
) {
|
|
|
|
next({ name: "SetupInitialData" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-30 09:46:19 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|