From b41a8c8477ed03325c3610e884f2aa67c582d739 Mon Sep 17 00:00:00 2001 From: Davyd McColl Date: Sat, 5 Oct 2019 11:07:05 +0200 Subject: [PATCH] :recycle: reduce complexity of validateAuthenticateRoutePermission function (#95) --- app/javascript/src/routes/index.js | 62 +++++++++++++++++++----------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/app/javascript/src/routes/index.js b/app/javascript/src/routes/index.js index 547b2e6ad..20ad4ecc5 100644 --- a/app/javascript/src/routes/index.js +++ b/app/javascript/src/routes/index.js @@ -53,31 +53,47 @@ const authIgnoreRoutes = [ '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, + }, +]; + const validateAuthenticateRoutePermission = (to, from, next) => { const isLoggedIn = auth.isLoggedIn(); - const currentUser = auth.getCurrentUser(); - - 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].includes( - to.name - ); - if (!isAccessible) { - return next(frontendURL('dashboard')); - } - } - - return next(); + 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) => {