TypeScript · 2099 bytes Raw Blame History
1 import { createRouter, createWebHistory } from 'vue-router'
2 import { useAuthStore } from '@/stores/auth'
3
4 const router = createRouter({
5 history: createWebHistory(import.meta.env.BASE_URL),
6 routes: [
7 {
8 path: '/',
9 name: 'dashboard',
10 component: () => import('@/views/DashboardView.vue'),
11 meta: { requiresAuth: true },
12 },
13 {
14 path: '/files',
15 name: 'files',
16 component: () => import('@/views/FilesView.vue'),
17 meta: { requiresAuth: true },
18 },
19 {
20 path: '/files/:path*',
21 name: 'files-path',
22 component: () => import('@/views/FilesView.vue'),
23 meta: { requiresAuth: true },
24 props: true,
25 },
26 {
27 path: '/upload',
28 name: 'upload',
29 component: () => import('@/views/UploadView.vue'),
30 meta: { requiresAuth: true },
31 },
32 {
33 path: '/settings',
34 name: 'settings',
35 component: () => import('@/views/SettingsView.vue'),
36 meta: { requiresAuth: true },
37 },
38 {
39 path: '/login',
40 name: 'login',
41 component: () => import('@/views/LoginView.vue'),
42 meta: { requiresGuest: true },
43 },
44 {
45 path: '/about',
46 name: 'about',
47 component: () => import('@/views/AboutView.vue'),
48 },
49 {
50 path: '/:pathMatch(.*)*',
51 name: 'not-found',
52 component: () => import('@/views/NotFoundView.vue'),
53 },
54 ],
55 })
56
57 // Navigation guards
58 router.beforeEach(async (to, from, next) => {
59 const authStore = useAuthStore()
60
61 // Initialize auth state if needed
62 if (!authStore.initialized) {
63 await authStore.initializeAuth()
64 }
65
66 const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
67 const requiresGuest = to.matched.some(record => record.meta.requiresGuest)
68
69 if (requiresAuth && !authStore.isAuthenticated) {
70 // Redirect to login page
71 next({ name: 'login', query: { redirect: to.fullPath } })
72 } else if (requiresGuest && authStore.isAuthenticated) {
73 // Redirect authenticated users away from guest pages
74 next({ name: 'dashboard' })
75 } else {
76 next()
77 }
78 })
79
80 export default router