60 lines
1.2 KiB
TypeScript
60 lines
1.2 KiB
TypeScript
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
||
|
|
import { useAuthStore } from '@/stores/auth'
|
||
|
|
|
||
|
|
const router = createRouter({
|
||
|
|
history: createWebHistory(),
|
||
|
|
routes: [
|
||
|
|
{
|
||
|
|
path: '/login',
|
||
|
|
name: 'Login',
|
||
|
|
component: () => import('@/views/LoginView.vue'),
|
||
|
|
meta: { public: true },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/',
|
||
|
|
component: () => import('@/components/Layout.vue'),
|
||
|
|
redirect: '/dashboard',
|
||
|
|
children: [
|
||
|
|
{
|
||
|
|
path: 'dashboard',
|
||
|
|
name: 'Dashboard',
|
||
|
|
component: () => import('@/views/DashboardView.vue'),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: 'feeds',
|
||
|
|
name: 'Feeds',
|
||
|
|
component: () => import('@/views/FeedsView.vue'),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: 'articles',
|
||
|
|
name: 'Articles',
|
||
|
|
component: () => import('@/views/ArticlesView.vue'),
|
||
|
|
},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
],
|
||
|
|
})
|
||
|
|
|
||
|
|
router.beforeEach(async (to) => {
|
||
|
|
const authStore = useAuthStore()
|
||
|
|
|
||
|
|
if (to.meta.public) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!authStore.isAuthenticated) {
|
||
|
|
const hasToken = !!localStorage.getItem('token')
|
||
|
|
if (hasToken) {
|
||
|
|
const ok = await authStore.fetchUser()
|
||
|
|
if (ok) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return '/login'
|
||
|
|
}
|
||
|
|
|
||
|
|
return true
|
||
|
|
})
|
||
|
|
|
||
|
|
export default router
|