2019-08-14 09:48:44 +00:00
|
|
|
import VueRouter from 'vue-router';
|
|
|
|
|
2019-08-25 14:29:28 +00:00
|
|
|
import { frontendURL } from '../helper/URLHelper';
|
2022-03-03 15:19:51 +00:00
|
|
|
import { clearBrowserSessionCookies } from '../store/utils/api';
|
2022-04-14 15:24:26 +00:00
|
|
|
import authRoute from './auth/auth.routes';
|
|
|
|
import dashboard from './dashboard/dashboard.routes';
|
|
|
|
import login from './login/login.routes';
|
|
|
|
import store from '../store';
|
2022-08-03 06:10:03 +00:00
|
|
|
import { validateLoggedInRoutes } from '../helper/routeHelpers';
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
const routes = [...login.routes, ...dashboard.routes, ...authRoute.routes];
|
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
|
|
|
const routeValidators = [
|
|
|
|
{
|
|
|
|
protected: false,
|
|
|
|
loggedIn: true,
|
2022-04-14 15:24:26 +00:00
|
|
|
handler: (_, getters) => {
|
|
|
|
const user = getters.getCurrentUser;
|
2021-11-23 12:07:01 +00:00
|
|
|
return `accounts/${user.account_id}/dashboard`;
|
|
|
|
},
|
2019-10-05 09:07:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: true,
|
|
|
|
loggedIn: false,
|
|
|
|
handler: () => 'login',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: true,
|
|
|
|
loggedIn: true,
|
2022-08-03 06:10:03 +00:00
|
|
|
handler: (to, getters) =>
|
|
|
|
validateLoggedInRoutes(to, getters.getCurrentUser, window.roleWiseRoutes),
|
2019-10-05 09:07:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
protected: false,
|
|
|
|
loggedIn: false,
|
|
|
|
handler: () => null,
|
|
|
|
},
|
|
|
|
];
|
2019-09-05 06:54:26 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
export const validateAuthenticateRoutePermission = (
|
|
|
|
to,
|
|
|
|
from,
|
|
|
|
next,
|
|
|
|
{ getters }
|
|
|
|
) => {
|
|
|
|
const isLoggedIn = getters.isLoggedIn;
|
2019-10-05 09:07:05 +00:00
|
|
|
const isProtectedRoute = !unProtectedRoutes.includes(to.name);
|
|
|
|
const strategy = routeValidators.find(
|
|
|
|
validator =>
|
|
|
|
validator.protected === isProtectedRoute &&
|
|
|
|
validator.loggedIn === isLoggedIn
|
|
|
|
);
|
2022-04-14 15:24:26 +00:00
|
|
|
const nextRoute = strategy.handler(to, getters);
|
2019-10-05 09:07:05 +00:00
|
|
|
return nextRoute ? next(frontendURL(nextRoute)) : next();
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
2022-03-03 15:19:51 +00:00
|
|
|
const validateSSOLoginParams = to => {
|
|
|
|
const isLoginRoute = to.name === 'login';
|
|
|
|
const { email, sso_auth_token: ssoAuthToken } = to.query || {};
|
|
|
|
const hasValidSSOParams = email && ssoAuthToken;
|
|
|
|
return isLoginRoute && hasValidSSOParams;
|
|
|
|
};
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
export const validateRouteAccess = (to, from, next, { getters }) => {
|
|
|
|
// Disable navigation to signup page if signups are disabled
|
|
|
|
// Signup route has an attribute (requireSignupEnabled)
|
|
|
|
// defined in it's route definition
|
2020-02-29 05:50:33 +00:00
|
|
|
if (
|
|
|
|
window.chatwootConfig.signupEnabled !== 'true' &&
|
|
|
|
to.meta &&
|
|
|
|
to.meta.requireSignupEnabled
|
|
|
|
) {
|
2022-04-14 15:24:26 +00:00
|
|
|
return next(frontendURL('login'));
|
2022-03-03 15:19:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
// For routes which doesn't care about authentication, skip validation
|
2019-09-05 06:54:26 +00:00
|
|
|
if (authIgnoreRoutes.includes(to.name)) {
|
|
|
|
return next();
|
|
|
|
}
|
2022-04-14 15:24:26 +00:00
|
|
|
|
|
|
|
return validateAuthenticateRoutePermission(to, from, next, { getters });
|
2019-09-05 06:54:26 +00:00
|
|
|
};
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
export const initalizeRouter = () => {
|
|
|
|
const userAuthentication = store.dispatch('setUser');
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
if (validateSSOLoginParams(to)) {
|
|
|
|
clearBrowserSessionCookies();
|
|
|
|
next();
|
|
|
|
return;
|
2021-05-28 13:51:16 +00:00
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
userAuthentication.then(() => {
|
|
|
|
if (!to.name) {
|
|
|
|
const { isLoggedIn, getCurrentUser: user } = store.getters;
|
|
|
|
if (isLoggedIn) {
|
|
|
|
return next(frontendURL(`accounts/${user.account_id}/dashboard`));
|
|
|
|
}
|
|
|
|
return next('/app/login');
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateRouteAccess(to, from, next, store);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
export default router;
|