2019-08-14 09:48:44 +00:00
|
|
|
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';
|
2019-08-25 14:29:28 +00:00
|
|
|
import { frontendURL } from '../helper/URLHelper';
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
const routes = [
|
|
|
|
...login.routes,
|
|
|
|
...dashboard.routes,
|
|
|
|
...authRoute.routes,
|
|
|
|
{
|
|
|
|
path: '/',
|
2021-05-28 13:51:16 +00:00
|
|
|
redirect: '/app',
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-05-28 13:51:16 +00:00
|
|
|
export const router = new VueRouter({ mode: 'history', routes });
|
2019-08-14 09:48:44 +00:00
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2019-10-05 09:07:05 +00:00
|
|
|
function routeIsAccessibleFor(route, role) {
|
|
|
|
return window.roleWiseRoutes[role].includes(route);
|
|
|
|
}
|
2019-09-05 06:54:26 +00:00
|
|
|
|
2019-10-05 09:07:05 +00:00
|
|
|
const routeValidators = [
|
|
|
|
{
|
|
|
|
protected: false,
|
|
|
|
loggedIn: true,
|
|
|
|
handler: () => 'dashboard',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: true,
|
|
|
|
loggedIn: false,
|
|
|
|
handler: () => 'login',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: true,
|
|
|
|
loggedIn: true,
|
|
|
|
handler: to => {
|
|
|
|
const user = auth.getCurrentUser();
|
|
|
|
const isAccessible = routeIsAccessibleFor(to, user.role);
|
|
|
|
return isAccessible ? null : 'dashboard';
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: false,
|
|
|
|
loggedIn: false,
|
|
|
|
handler: () => null,
|
|
|
|
},
|
|
|
|
];
|
2019-09-05 06:54:26 +00:00
|
|
|
|
2019-10-14 18:48:46 +00:00
|
|
|
export const validateAuthenticateRoutePermission = (to, from, next) => {
|
2019-10-05 09:07:05 +00:00
|
|
|
const isLoggedIn = auth.isLoggedIn();
|
|
|
|
const isProtectedRoute = !unProtectedRoutes.includes(to.name);
|
|
|
|
const strategy = routeValidators.find(
|
|
|
|
validator =>
|
|
|
|
validator.protected === isProtectedRoute &&
|
|
|
|
validator.loggedIn === isLoggedIn
|
|
|
|
);
|
|
|
|
const nextRoute = strategy.handler(to.name);
|
|
|
|
return nextRoute ? next(frontendURL(nextRoute)) : next();
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
2019-09-05 06:54:26 +00:00
|
|
|
const validateRouteAccess = (to, from, next) => {
|
2020-02-29 05:50:33 +00:00
|
|
|
if (
|
|
|
|
window.chatwootConfig.signupEnabled !== 'true' &&
|
|
|
|
to.meta &&
|
|
|
|
to.meta.requireSignupEnabled
|
|
|
|
) {
|
2020-03-09 17:57:10 +00:00
|
|
|
const user = auth.getCurrentUser();
|
|
|
|
next(frontendURL(`accounts/${user.account_id}/dashboard`));
|
2020-02-29 05:50:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:54:26 +00:00
|
|
|
if (authIgnoreRoutes.includes(to.name)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return validateAuthenticateRoutePermission(to, from, next);
|
|
|
|
};
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
// protecting routes
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
if (!to.name) {
|
2020-03-09 17:57:10 +00:00
|
|
|
const user = auth.getCurrentUser();
|
2021-05-28 13:51:16 +00:00
|
|
|
if (user) {
|
|
|
|
return next(frontendURL(`accounts/${user.account_id}/dashboard`));
|
|
|
|
}
|
|
|
|
return next('/app/login');
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:54:26 +00:00
|
|
|
return validateRouteAccess(to, from, next);
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|