This commit is contained in:
Dede Fuji Abdul
2023-10-16 14:29:26 +07:00
parent 4ee87a0aa9
commit d80e8d2b5a
9 changed files with 979 additions and 96 deletions

View File

@ -12,7 +12,7 @@ const router = createRouter({
linkActiveClass: 'active',
routes: [
{
path: '/',
path: '/home',
name: 'Home',
'meta': { requiresAuth: true },
component: HomeView,
@ -295,9 +295,9 @@ const router = createRouter({
path: '/logout',
name: 'Logout',
beforeEnter(to, from, next) {
const auth = useAuthStore();
auth.logout();
next('/login');
const auth = useAuthStore()
auth.logout()
next('/login')
},
redirect: '/logout'
},
@ -319,26 +319,36 @@ const router = createRouter({
]
})
router.beforeEach((to, _from) => {
router.beforeEach((to, from, next) => {
// redirect to login page if not logged in and trying to access a restricted page
const publicPages = ['/login'];
const authRequired = !publicPages.includes(to.path);
const auth = useAuthStore();
// if to is not found, redirect to 404
if (to.matched.length === 0) {
return '/404';
} else {
// if to is not login and user is not logged in, redirect to login
if (!auth.isLoggedIn) {
// if to is 404, redirect to 404
if (to.path !== '/404' && to.path !== '/login') {
return '/login';
if (to.path === '/') {
if (auth.isLoggedIn) {
next('/home')
} else {
next('/login')
}
} else {
next('/404')
}
} else {
// if to is not login and user is not logged in, redirect to login
if (auth.isLoggedIn) {
// if to is login and user is logged in, redirect to home
if (to.path === '/login') {
return '/';
next('/home')
} else {
next()
}
} else {
// if to is 404, redirect to 404
if (to.path !== '/404' && to.path !== '/login') {
next('/login')
} else {
next()
}
}
}