2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
import VueRouter from 'vue-router';
|
|
|
|
|
|
|
|
import auth from '../api/auth';
|
|
|
|
import login from './login/login.routes';
|
|
|
|
import dashboard from './dashboard/dashboard.routes';
|
|
|
|
import authRoute from './auth/auth.routes';
|
|
|
|
|
|
|
|
/* Vue Routes */
|
|
|
|
const routes = [
|
|
|
|
...login.routes,
|
|
|
|
...dashboard.routes,
|
|
|
|
...authRoute.routes,
|
|
|
|
{
|
|
|
|
path: '/',
|
|
|
|
redirect: '/u/dashboard',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
window.roleWiseRoutes = {
|
|
|
|
agent: [],
|
|
|
|
administrator: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
// generateRoleWiseRoute - updates window object with agent/admin route
|
2019-08-21 07:29:56 +00:00
|
|
|
const generateRoleWiseRoute = route => {
|
|
|
|
route.forEach(element => {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (element.children) {
|
|
|
|
generateRoleWiseRoute(element.children);
|
|
|
|
}
|
|
|
|
if (element.roles) {
|
2019-08-21 07:29:56 +00:00
|
|
|
element.roles.forEach(roleEl => {
|
2019-08-14 09:48:44 +00:00
|
|
|
window.roleWiseRoutes[roleEl].push(element.name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
// Create a object of routes
|
|
|
|
// accessible by each role.
|
|
|
|
// returns an object with roles as keys and routeArr as values
|
|
|
|
generateRoleWiseRoute(routes);
|
|
|
|
|
|
|
|
const router = new VueRouter({
|
|
|
|
mode: 'history',
|
|
|
|
routes, // short for routes: routes
|
|
|
|
});
|
|
|
|
|
2019-08-21 07:29:56 +00:00
|
|
|
const unProtectedRoutes = ['login', 'auth_signup', 'auth_reset_password'];
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
const authIgnoreRoutes = [
|
|
|
|
'auth_confirmation',
|
|
|
|
'pushBack',
|
|
|
|
'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 isLoggedIn = auth.isLoggedIn();
|
|
|
|
const currentUser = auth.getCurrentUser();
|
|
|
|
if (isLoggedIn) {
|
|
|
|
// Check if next route is accessible by given role
|
2019-08-21 07:29:56 +00:00
|
|
|
const isAccessible =
|
|
|
|
window.roleWiseRoutes[currentUser.role].indexOf(to.name) > -1;
|
2019-08-14 09:48:44 +00:00
|
|
|
if (!isAccessible) {
|
|
|
|
return next('/u/dashboard');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If unprotected and loggedIn -> redirect
|
|
|
|
if (unProtectedRoutes.indexOf(to.name) !== -1 && isLoggedIn) {
|
|
|
|
return next('/u/dashboard');
|
2019-08-21 07:29:56 +00:00
|
|
|
}
|
|
|
|
if (unProtectedRoutes.indexOf(to.name) === -1 && !isLoggedIn) {
|
2019-08-14 09:48:44 +00:00
|
|
|
return next('/u/login');
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
|
|
|
|
// protecting routes
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
if (!to.name) {
|
|
|
|
return next('/u/dashboard');
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirectUser(to, from, next);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|