mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
Move src to dashboard (#152)
This commit is contained in:
115
app/javascript/dashboard/routes/index.js
Normal file
115
app/javascript/dashboard/routes/index.js
Normal file
@@ -0,0 +1,115 @@
|
||||
/* eslint no-console: 0 */
|
||||
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';
|
||||
|
||||
const routes = [
|
||||
...login.routes,
|
||||
...dashboard.routes,
|
||||
...authRoute.routes,
|
||||
{
|
||||
path: '/',
|
||||
redirect: frontendURL('dashboard'),
|
||||
},
|
||||
];
|
||||
|
||||
window.roleWiseRoutes = {
|
||||
agent: [],
|
||||
administrator: [],
|
||||
};
|
||||
|
||||
// generateRoleWiseRoute - updates window object with agent/admin route
|
||||
const generateRoleWiseRoute = route => {
|
||||
route.forEach(element => {
|
||||
if (element.children) {
|
||||
generateRoleWiseRoute(element.children);
|
||||
}
|
||||
if (element.roles) {
|
||||
element.roles.forEach(roleEl => {
|
||||
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);
|
||||
|
||||
export const router = new VueRouter({
|
||||
mode: 'history',
|
||||
routes, // short for routes: routes
|
||||
});
|
||||
|
||||
const unProtectedRoutes = ['login', 'auth_signup', 'auth_reset_password'];
|
||||
|
||||
const authIgnoreRoutes = [
|
||||
'auth_confirmation',
|
||||
'pushBack',
|
||||
'auth_password_edit',
|
||||
];
|
||||
|
||||
function routeIsAccessibleFor(route, role) {
|
||||
return window.roleWiseRoutes[role].includes(route);
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
];
|
||||
|
||||
export const validateAuthenticateRoutePermission = (to, from, next) => {
|
||||
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();
|
||||
};
|
||||
|
||||
const validateRouteAccess = (to, from, next) => {
|
||||
if (authIgnoreRoutes.includes(to.name)) {
|
||||
return next();
|
||||
}
|
||||
return validateAuthenticateRoutePermission(to, from, next);
|
||||
};
|
||||
|
||||
// protecting routes
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (!to.name) {
|
||||
return next(frontendURL('dashboard'));
|
||||
}
|
||||
|
||||
return validateRouteAccess(to, from, next);
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user