Cleanup routes (#26)

This commit is contained in:
Pranav Raj S 2019-09-05 12:24:26 +05:30 committed by GitHub
parent 129febf29e
commit a108ae7789
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;