36 lines
916 B
TypeScript
36 lines
916 B
TypeScript
import { createRouter, createWebHistory } from 'vue-router';
|
|
import MainRoutes from './MainRoutes';
|
|
import AuthRoutes from './AuthRoutes';
|
|
import { useAuthStore } from '@/stores/auth';
|
|
|
|
export const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes: [
|
|
MainRoutes,
|
|
AuthRoutes
|
|
]
|
|
});
|
|
|
|
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) => {
|
|
const publicPages = ['/auth/login'];
|
|
const authRequired = !publicPages.includes(to.path);
|
|
const auth: AuthStore = useAuthStore();
|
|
|
|
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();
|
|
}
|
|
});
|