Cleanup routes (#26)
This commit is contained in:
parent
129febf29e
commit
a108ae7789
1 changed files with 25 additions and 18 deletions
|
@ -7,7 +7,6 @@ import dashboard from './dashboard/dashboard.routes';
|
|||
import authRoute from './auth/auth.routes';
|
||||
import { frontendURL } from '../helper/URLHelper';
|
||||
|
||||
/* Vue Routes */
|
||||
const routes = [
|
||||
...login.routes,
|
||||
...dashboard.routes,
|
||||
|
@ -54,39 +53,47 @@ const authIgnoreRoutes = [
|
|||
'auth_password_edit',
|
||||
];
|
||||
|
||||
const redirectUser = (to, from, next) => {
|
||||
// If auth ignore go to page
|
||||
if (authIgnoreRoutes.indexOf(to.name) > -1) {
|
||||
return next();
|
||||
}
|
||||
// Check accesibility
|
||||
const validateAuthenticateRoutePermission = (to, from, next) => {
|
||||
const isLoggedIn = auth.isLoggedIn();
|
||||
const currentUser = auth.getCurrentUser();
|
||||
if (isLoggedIn) {
|
||||
|
||||
const isAnUnprotectedRoute = unProtectedRoutes.includes(to.name);
|
||||
if (isAnUnprotectedRoute && isLoggedIn) {
|
||||
return next(frontendURL('dashboard'));
|
||||
}
|
||||
|
||||
const isAProtectedRoute = !unProtectedRoutes.includes(to.name);
|
||||
if (isAProtectedRoute && !isLoggedIn) {
|
||||
return next(frontendURL('login'));
|
||||
}
|
||||
|
||||
if (isAProtectedRoute && isLoggedIn) {
|
||||
// Check if next route is accessible by given role
|
||||
const isAccessible =
|
||||
window.roleWiseRoutes[currentUser.role].indexOf(to.name) > -1;
|
||||
const isAccessible = window.roleWiseRoutes[currentUser.role].includes(
|
||||
to.name
|
||||
);
|
||||
if (!isAccessible) {
|
||||
return next(frontendURL('dashboard'));
|
||||
}
|
||||
}
|
||||
// If unprotected and loggedIn -> redirect
|
||||
if (unProtectedRoutes.indexOf(to.name) !== -1 && isLoggedIn) {
|
||||
return next(frontendURL('dashboard'));
|
||||
}
|
||||
if (unProtectedRoutes.indexOf(to.name) === -1 && !isLoggedIn) {
|
||||
return next(frontendURL('login'));
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
const validateRouteAccess = (to, from, next) => {
|
||||
if (authIgnoreRoutes.includes(to.name)) {
|
||||
return next();
|
||||
}
|
||||
return validateAuthenticateRoutePermission(to, from, next);
|
||||
};
|
||||
|
||||
// protecting routes
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (!to.name) {
|
||||
return next(frontendURL('dashboard'));
|
||||
}
|
||||
|
||||
return redirectUser(to, from, next);
|
||||
return validateRouteAccess(to, from, next);
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
|
Loading…
Reference in a new issue