Redirect root path to Home component

This commit is contained in:
Dede Fuji Abdul 2023-11-02 11:11:38 +07:00
parent d89d1b9a4e
commit 6a81c1686f

View File

@ -700,8 +700,10 @@ export const routes: RouteRecordRaw[] = [
component: NotFoundView component: NotFoundView
}, },
{ {
path: ':pathMatch(.*)*', path: '/',
redirect: '/404' redirect: {
name: 'Home'
}
} }
] ]
@ -805,48 +807,32 @@ router.beforeEach((to, from, next) => {
const auth = useAuthStore(); const auth = useAuthStore();
// if to is not found, redirect to 404 // if to is not found, redirect to 404
// if (to.matched.length === 0) { if (to.matched.length === 0) {
// if (to.path === '/') { if (to.path === '/') {
// if (auth.isLoggedIn) { if (auth.isLoggedIn) {
// next('/home') next('/home')
// } else { } else {
// next('/login') 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') {
// next('/home')
// } else {
// next()
// }
// } else {
// // if to is 404, redirect to 404
// if (to.path !== '/404' && to.path !== '/login') {
// next('/login')
// } else {
// next()
// }
// }
// }
if (auth.isLoggedIn) {
// if to is login and user is logged in, redirect to home
if (to.path === '/login') {
next('/home')
} else { } else {
next() next('/404')
} }
} else { } else {
// if to is 404, redirect to 404 // if to is not login and user is not logged in, redirect to login
if (to.path !== '/404' && to.path !== '/login') { if (auth.isLoggedIn) {
next('/login') // if to is login and user is logged in, redirect to home
if (to.path === '/login') {
next('/home')
} else {
next()
}
} else { } else {
next() // if to is 404, redirect to 404
if (to.path !== '/404' && to.path !== '/login') {
next('/login')
} else {
next()
}
} }
} }
}) })