add a global navigation guard
This commit is contained in:
parent
77e7a7d93a
commit
50b32df6aa
|
@ -7,6 +7,7 @@ import CoachRegistration from '../pages/coaches/CoachRegistration';
|
||||||
import ContactCoach from '../pages/requests/ContactCoach';
|
import ContactCoach from '../pages/requests/ContactCoach';
|
||||||
import RequestsRecieved from '../pages/requests/RequestsRecieved';
|
import RequestsRecieved from '../pages/requests/RequestsRecieved';
|
||||||
import NotFound from '../pages/NotFound';
|
import NotFound from '../pages/NotFound';
|
||||||
|
import store from '../store/index.js';
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(),
|
history: createWebHistory(),
|
||||||
|
@ -19,11 +20,29 @@ const router = createRouter({
|
||||||
props: true,
|
props: true,
|
||||||
children: [{ path: 'contact', component: ContactCoach }]
|
children: [{ path: 'contact', component: ContactCoach }]
|
||||||
},
|
},
|
||||||
{ path: '/register', component: CoachRegistration },
|
{
|
||||||
{ path: '/requests', component: RequestsRecieved },
|
path: '/register',
|
||||||
{ path: '/auth', component: UserAuth },
|
component: CoachRegistration,
|
||||||
|
meta: { requiresAuth: true }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/requests',
|
||||||
|
component: RequestsRecieved,
|
||||||
|
meta: { requiresAuth: true }
|
||||||
|
},
|
||||||
|
{ path: '/auth', component: UserAuth, meta: { requiresUnAuth: true } },
|
||||||
{ path: '/:notFound(.*)', component: NotFound }
|
{ path: '/:notFound(.*)', component: NotFound }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.beforeEach(function(to, _, next) {
|
||||||
|
if (to.meta.requiresAuth && !store.getters.isAuthenticated) {
|
||||||
|
next('/auth');
|
||||||
|
} else if (to.meta.requiresUnAuth && store.getters.isAuthenticated) {
|
||||||
|
next('/coaches');
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|
Reference in New Issue