| import { createRouter, createWebHashHistory } from 'vue-router'; |
| import MainRoutes from './MainRoutes'; |
| import AuthRoutes from './AuthRoutes'; |
| import ChatBoxRoutes from './ChatBoxRoutes'; |
| import { useAuthStore } from '@/stores/auth'; |
| import { useRouterLoadingStore } from '@/stores/routerLoading'; |
|
|
| export const router = createRouter({ |
| history: createWebHashHistory(import.meta.env.BASE_URL), |
| routes: [ |
| MainRoutes, |
| AuthRoutes, |
| ChatBoxRoutes |
| ] |
| }); |
|
|
| interface AuthStore { |
| username: string; |
| returnUrl: string | null; |
| login(username: string, password: string): Promise<void>; |
| logout(): void; |
| has_token(): boolean; |
| } |
|
|
| router.beforeEach(async (to, from, next) => { |
| if (from.name && from.path !== to.path) { |
| const loadingStore = useRouterLoadingStore(); |
| loadingStore.start(); |
| } |
|
|
| const publicPages = ['/auth/login']; |
| const authRequired = !publicPages.includes(to.path); |
| const auth: AuthStore = useAuthStore(); |
|
|
| |
| if (to.path === '/auth/login' && auth.has_token()) { |
| return next(auth.returnUrl || '/'); |
| } |
|
|
| if (to.matched.some((record) => record.meta.requiresAuth)) { |
| if (authRequired && !auth.has_token()) { |
| auth.returnUrl = to.fullPath; |
| return next('/auth/login'); |
| } else next(); |
| } else { |
| next(); |
| } |
| }); |
|
|
| router.afterEach(() => { |
| const loadingStore = useRouterLoadingStore(); |
| loadingStore.finish(); |
| }); |
|
|