♻️ reduce complexity of validateAuthenticateRoutePermission function (#95)
This commit is contained in:
committed by
Pranav Raj S
parent
e91868f823
commit
b41a8c8477
@@ -53,31 +53,47 @@ const authIgnoreRoutes = [
|
|||||||
'auth_password_edit',
|
'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 validateAuthenticateRoutePermission = (to, from, next) => {
|
||||||
const isLoggedIn = auth.isLoggedIn();
|
const isLoggedIn = auth.isLoggedIn();
|
||||||
const currentUser = auth.getCurrentUser();
|
const isProtectedRoute = !unProtectedRoutes.includes(to.name);
|
||||||
|
const strategy = routeValidators.find(
|
||||||
const isAnUnprotectedRoute = unProtectedRoutes.includes(to.name);
|
validator =>
|
||||||
if (isAnUnprotectedRoute && isLoggedIn) {
|
validator.protected === isProtectedRoute &&
|
||||||
return next(frontendURL('dashboard'));
|
validator.loggedIn === isLoggedIn
|
||||||
}
|
);
|
||||||
|
const nextRoute = strategy.handler(to.name);
|
||||||
const isAProtectedRoute = !unProtectedRoutes.includes(to.name);
|
return nextRoute ? next(frontendURL(nextRoute)) : next();
|
||||||
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 validateRouteAccess = (to, from, next) => {
|
const validateRouteAccess = (to, from, next) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user