init auth
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import LoginView from '../views/LoginView.vue'
|
||||
import RegisterView from '../views/RegisterView.vue'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
@@ -9,10 +11,21 @@ const router = createRouter({
|
||||
name: 'login',
|
||||
component: LoginView
|
||||
},
|
||||
{
|
||||
path: '/register',
|
||||
name: 'register',
|
||||
component: RegisterView
|
||||
},
|
||||
{
|
||||
path: '/admin/approvals',
|
||||
name: 'approvals',
|
||||
component: () => import('../views/UserApprovalView.vue'),
|
||||
meta: { requiresAdmin: true }
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
name: 'characters-home',
|
||||
component: () => import('../views/CharactersView.vue')
|
||||
name: 'home',
|
||||
component: () => import('../views/FlexibleGenerationView.vue')
|
||||
},
|
||||
{
|
||||
path: '/characters',
|
||||
@@ -40,23 +53,32 @@ const router = createRouter({
|
||||
component: () => import('../views/ImageGenerationView.vue')
|
||||
},
|
||||
{
|
||||
path: '/flexible-generation',
|
||||
name: 'flexible-generation',
|
||||
path: '/flexible',
|
||||
name: 'flexible',
|
||||
component: () => import('../views/FlexibleGenerationView.vue')
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
const isAuth = localStorage.getItem('auth_code')
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const authStore = useAuthStore()
|
||||
const publicPages = ['/login', '/register']
|
||||
const authRequired = !publicPages.includes(to.path)
|
||||
|
||||
if (to.name !== 'login' && !isAuth) {
|
||||
next({ name: 'login' })
|
||||
} else if (to.name === 'login' && isAuth) {
|
||||
next({ name: 'dashboard' })
|
||||
} else {
|
||||
next()
|
||||
if (authRequired) {
|
||||
if (!authStore.isAuthenticated) {
|
||||
return next('/login')
|
||||
}
|
||||
|
||||
// Check if route requires admin
|
||||
if (to.meta.requiresAdmin && !authStore.isAdmin()) {
|
||||
return next('/') // Redirect non-admins to home
|
||||
}
|
||||
} else if ((to.name === 'login' || to.name === 'register') && authStore.isAuthenticated) {
|
||||
return next('/')
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user