fix: Allow users to login even if they have access to more than 15 accounts (#4475)

This commit is contained in:
Pranav Raj S
2022-04-14 20:54:26 +05:30
committed by GitHub
parent 80e5d6d7a0
commit 0319b78eac
19 changed files with 368 additions and 350 deletions

View File

@@ -1,21 +1,13 @@
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';
import { frontendURL } from '../helper/URLHelper';
import { clearBrowserSessionCookies } from '../store/utils/api';
import authRoute from './auth/auth.routes';
import dashboard from './dashboard/dashboard.routes';
import login from './login/login.routes';
import store from '../store';
const routes = [
...login.routes,
...dashboard.routes,
...authRoute.routes,
{
path: '/',
redirect: '/app',
},
];
const routes = [...login.routes, ...dashboard.routes, ...authRoute.routes];
window.roleWiseRoutes = {
agent: [],
@@ -63,8 +55,8 @@ const routeValidators = [
{
protected: false,
loggedIn: true,
handler: () => {
const user = auth.getCurrentUser();
handler: (_, getters) => {
const user = getters.getCurrentUser;
return `accounts/${user.account_id}/dashboard`;
},
},
@@ -76,8 +68,8 @@ const routeValidators = [
{
protected: true,
loggedIn: true,
handler: to => {
const user = auth.getCurrentUser();
handler: (to, getters) => {
const user = getters.getCurrentUser;
const userRole = getUserRole(user, Number(to.params.accountId));
const isAccessible = routeIsAccessibleFor(to.name, userRole);
return isAccessible ? null : `accounts/${to.params.accountId}/dashboard`;
@@ -90,15 +82,20 @@ const routeValidators = [
},
];
export const validateAuthenticateRoutePermission = (to, from, next) => {
const isLoggedIn = auth.isLoggedIn();
export const validateAuthenticateRoutePermission = (
to,
from,
next,
{ getters }
) => {
const isLoggedIn = getters.isLoggedIn;
const isProtectedRoute = !unProtectedRoutes.includes(to.name);
const strategy = routeValidators.find(
validator =>
validator.protected === isProtectedRoute &&
validator.loggedIn === isLoggedIn
);
const nextRoute = strategy.handler(to);
const nextRoute = strategy.handler(to, getters);
return nextRoute ? next(frontendURL(nextRoute)) : next();
};
@@ -109,38 +106,47 @@ const validateSSOLoginParams = to => {
return isLoginRoute && hasValidSSOParams;
};
const validateRouteAccess = (to, from, next) => {
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
if (
window.chatwootConfig.signupEnabled !== 'true' &&
to.meta &&
to.meta.requireSignupEnabled
) {
const user = auth.getCurrentUser();
next(frontendURL(`accounts/${user.account_id}/dashboard`));
}
if (validateSSOLoginParams(to)) {
clearBrowserSessionCookies();
return next();
return next(frontendURL('login'));
}
// For routes which doesn't care about authentication, skip validation
if (authIgnoreRoutes.includes(to.name)) {
return next();
}
return validateAuthenticateRoutePermission(to, from, next);
return validateAuthenticateRoutePermission(to, from, next, { getters });
};
// protecting routes
router.beforeEach((to, from, next) => {
if (!to.name) {
const user = auth.getCurrentUser();
if (user) {
return next(frontendURL(`accounts/${user.account_id}/dashboard`));
export const initalizeRouter = () => {
const userAuthentication = store.dispatch('setUser');
router.beforeEach((to, from, next) => {
if (validateSSOLoginParams(to)) {
clearBrowserSessionCookies();
next();
return;
}
return next('/app/login');
}
return validateRouteAccess(to, from, next);
});
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);
});
});
};
export default router;