From a108ae7789f288fbb68f1eb971fc15cccb9ba9e8 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 5 Sep 2019 12:24:26 +0530 Subject: [PATCH] Cleanup routes (#26) --- app/javascript/src/routes/index.js | 43 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/app/javascript/src/routes/index.js b/app/javascript/src/routes/index.js index dee3c48e0..547b2e6ad 100644 --- a/app/javascript/src/routes/index.js +++ b/app/javascript/src/routes/index.js @@ -7,7 +7,6 @@ import dashboard from './dashboard/dashboard.routes'; import authRoute from './auth/auth.routes'; import { frontendURL } from '../helper/URLHelper'; -/* Vue Routes */ const routes = [ ...login.routes, ...dashboard.routes, @@ -54,39 +53,47 @@ const authIgnoreRoutes = [ 'auth_password_edit', ]; -const redirectUser = (to, from, next) => { - // If auth ignore go to page - if (authIgnoreRoutes.indexOf(to.name) > -1) { - return next(); - } - // Check accesibility +const validateAuthenticateRoutePermission = (to, from, next) => { const isLoggedIn = auth.isLoggedIn(); 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 - const isAccessible = - window.roleWiseRoutes[currentUser.role].indexOf(to.name) > -1; + const isAccessible = window.roleWiseRoutes[currentUser.role].includes( + to.name + ); if (!isAccessible) { 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(); }; +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 redirectUser(to, from, next); + return validateRouteAccess(to, from, next); }); export default router;