designtopack/src/router/router.js

34 lines
754 B
JavaScript
Raw Normal View History

import { createWebHistory, createRouter } from 'vue-router';
import routes from './routes';
import { useApiStore } from '../stores/api';
import { usePageStore } from '../stores/page';
import { useUserStore } from '../stores/user';
2024-09-17 17:03:13 +02:00
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach(async (to, from, next) => {
const pageStore = usePageStore();
const userStore = useUserStore();
2024-09-17 17:03:13 +02:00
const api = useApiStore();
2024-09-17 17:03:13 +02:00
try {
2024-10-28 15:33:52 +01:00
const res = await api.fetchData(to.path);
2024-09-17 17:03:13 +02:00
if (to.path === '/login' && res.user) {
location.href = '/';
}
pageStore.page = res.page;
userStore.user = res.user;
next();
2024-09-17 17:03:13 +02:00
} catch (error) {
console.error(error);
next(false);
}
});
export { router };