Initial commit
This commit is contained in:
60
admin/src/router/index.ts
Normal file
60
admin/src/router/index.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { useAuth } from '../composables/useAuth'
|
||||
import AdminLayout from '../components/AdminLayout.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes: [
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import('../pages/LoginPage.vue'),
|
||||
meta: { public: true },
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: AdminLayout,
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
name: 'events',
|
||||
component: () => import('../pages/EventsListPage.vue'),
|
||||
},
|
||||
{
|
||||
path: 'events/create',
|
||||
name: 'event-create',
|
||||
component: () => import('../pages/EventCreatePage.vue'),
|
||||
},
|
||||
{
|
||||
path: 'events/:id/edit',
|
||||
name: 'event-edit',
|
||||
component: () => import('../pages/EventEditPage.vue'),
|
||||
},
|
||||
{
|
||||
path: 'events/:id/uploads',
|
||||
name: 'event-uploads',
|
||||
component: () => import('../pages/EventUploadsPage.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
const { isAuthenticated, fetchUser } = useAuth()
|
||||
if (to.name === 'login' && isAuthenticated.value) {
|
||||
const redirect = (to.query.redirect as string) || '/'
|
||||
return { path: redirect }
|
||||
}
|
||||
if (!to.meta.public) {
|
||||
if (isAuthenticated.value) return true
|
||||
try {
|
||||
await fetchUser()
|
||||
} catch {
|
||||
return { name: 'login', query: { redirect: to.fullPath } }
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user