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: [],
|
|
|
|
};
|
|
|
|
|
2021-11-23 12:07:01 +00:00
|
|
|
const getUserRole = ({ accounts } = {}, accountId) => {
|
|
|
|
const currentAccount = accounts.find(account => account.id === accountId);
|
|
|
|
return currentAccount ? currentAccount.role : null;
|
|
|
|
};
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
// 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,
|
2021-11-23 12:07:01 +00:00
|
|
|
handler: () => {
|
|
|
|
const user = auth.getCurrentUser();
|
|
|
|
return `accounts/${user.account_id}/dashboard`;
|
|
|
|
},
|
2019-10-05 09:07:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: true,
|
|
|
|
loggedIn: false,
|
|
|
|
handler: () => 'login',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: true,
|
|
|
|
loggedIn: true,
|
|
|
|
handler: to => {
|
|
|
|
const user = auth.getCurrentUser();
|
2021-11-23 12:07:01 +00:00
|
|
|
const userRole = getUserRole(user, Number(to.params.accountId));
|
|
|
|
const isAccessible = routeIsAccessibleFor(to.name, userRole);
|
|
|
|
return isAccessible ? null : `accounts/${to.params.accountId}/dashboard`;
|
2019-10-05 09:07:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
|
);
|
2021-11-23 12:07:01 +00:00
|
|
|
const nextRoute = strategy.handler(to);
|
2019-10-05 09:07:05 +00:00
|
|
|
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;
|