Files
JayuCloud/Frontend/src/router/index.ts
T

72 lines
1.8 KiB
TypeScript
Raw Normal View History

import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
2026-02-24 09:59:22 +08:00
component: () => import('../views/HomeView.vue'),
2026-02-24 10:57:00 +08:00
meta: { requiresAuth: true }
2026-02-24 09:59:22 +08:00
},
{
path: '/auth',
name: 'auth',
component: () => import('../views/AuthView.vue'),
meta: { requiresAuth: false, hideNav: true }
},
{
path: '/products',
name: 'products',
2026-02-24 09:59:22 +08:00
component: () => import('../views/ProductListView.vue'),
meta: { requiresAuth: true }
},
{
path: '/product/:id',
name: 'product-detail',
2026-02-24 09:59:22 +08:00
component: () => import('../views/ProductDetailView.vue'),
meta: { requiresAuth: true, hideNav: true }
},
{
path: '/cart',
name: 'cart',
2026-02-24 09:59:22 +08:00
component: () => import('../views/CartView.vue'),
meta: { requiresAuth: true }
},
{
path: '/profile',
name: 'profile',
2026-02-24 09:59:22 +08:00
component: () => import('../views/ProfileView.vue'),
meta: { requiresAuth: true }
},
{
path: '/wishlist',
name: 'wishlist',
2026-02-24 09:59:22 +08:00
component: () => import('../views/WishlistView.vue'),
meta: { requiresAuth: true }
},
{
path: '/settings',
name: 'settings',
2026-02-24 09:59:22 +08:00
component: () => import('../views/SettingsView.vue'),
meta: { requiresAuth: true, hideNav: true }
}
]
})
2026-02-24 09:59:22 +08:00
router.beforeEach((to, _from, next) => {
const token = localStorage.getItem('accessToken')
const requiresAuth = to.meta.requiresAuth as boolean
if (requiresAuth && !token) {
next('/auth')
} else if (to.path === '/auth' && token) {
next('/')
2026-02-24 10:57:00 +08:00
} else if (to.path === '/' && !token) {
next('/auth')
2026-02-24 09:59:22 +08:00
} else {
next()
}
})
export default router