mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-31 19:17:48 +00:00
Cleanup routes (#26)
This commit is contained in:
@@ -7,7 +7,6 @@ import dashboard from './dashboard/dashboard.routes';
|
|||||||
import authRoute from './auth/auth.routes';
|
import authRoute from './auth/auth.routes';
|
||||||
import { frontendURL } from '../helper/URLHelper';
|
import { frontendURL } from '../helper/URLHelper';
|
||||||
|
|
||||||
/* Vue Routes */
|
|
||||||
const routes = [
|
const routes = [
|
||||||
...login.routes,
|
...login.routes,
|
||||||
...dashboard.routes,
|
...dashboard.routes,
|
||||||
@@ -54,39 +53,47 @@ const authIgnoreRoutes = [
|
|||||||
'auth_password_edit',
|
'auth_password_edit',
|
||||||
];
|
];
|
||||||
|
|
||||||
const redirectUser = (to, from, next) => {
|
const validateAuthenticateRoutePermission = (to, from, next) => {
|
||||||
// If auth ignore go to page
|
|
||||||
if (authIgnoreRoutes.indexOf(to.name) > -1) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
// Check accesibility
|
|
||||||
const isLoggedIn = auth.isLoggedIn();
|
const isLoggedIn = auth.isLoggedIn();
|
||||||
const currentUser = auth.getCurrentUser();
|
const currentUser = auth.getCurrentUser();
|
||||||
if (isLoggedIn) {
|
|
||||||
|
const isAnUnprotectedRoute = unProtectedRoutes.includes(to.name);
|
||||||
|
if (isAnUnprotectedRoute && isLoggedIn) {
|
||||||
|
return next(frontendURL('dashboard'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const isAProtectedRoute = !unProtectedRoutes.includes(to.name);
|
||||||
|
if (isAProtectedRoute && !isLoggedIn) {
|
||||||
|
return next(frontendURL('login'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAProtectedRoute && isLoggedIn) {
|
||||||
// Check if next route is accessible by given role
|
// Check if next route is accessible by given role
|
||||||
const isAccessible =
|
const isAccessible = window.roleWiseRoutes[currentUser.role].includes(
|
||||||
window.roleWiseRoutes[currentUser.role].indexOf(to.name) > -1;
|
to.name
|
||||||
|
);
|
||||||
if (!isAccessible) {
|
if (!isAccessible) {
|
||||||
return next(frontendURL('dashboard'));
|
return next(frontendURL('dashboard'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If unprotected and loggedIn -> redirect
|
|
||||||
if (unProtectedRoutes.indexOf(to.name) !== -1 && isLoggedIn) {
|
|
||||||
return next(frontendURL('dashboard'));
|
|
||||||
}
|
|
||||||
if (unProtectedRoutes.indexOf(to.name) === -1 && !isLoggedIn) {
|
|
||||||
return next(frontendURL('login'));
|
|
||||||
}
|
|
||||||
return next();
|
return next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const validateRouteAccess = (to, from, next) => {
|
||||||
|
if (authIgnoreRoutes.includes(to.name)) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
return validateAuthenticateRoutePermission(to, from, next);
|
||||||
|
};
|
||||||
|
|
||||||
// protecting routes
|
// protecting routes
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
if (!to.name) {
|
if (!to.name) {
|
||||||
return next(frontendURL('dashboard'));
|
return next(frontendURL('dashboard'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirectUser(to, from, next);
|
return validateRouteAccess(to, from, next);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user