Hi, in my app, I wrote a method which checks whether the user have permission to view a particular page. In every page visit, this method will be called and if the user does not have permission, he will be sent back to the home page.
The problem is, this method slow things down. It takes time to do this check and moving between pages in the app is very slow. Here’s the method :
'users.havePermission'(currentRoute) {
check(currentRoute, String);
const loggedInUser = Meteor.user();
const permissions = {
home: [ 'superAdmin', 'adviser', 'admin' ],
'page.1': [ 'superAdmin', 'adviser' ],
'page.2': [ 'superAdmin', 'adviser', 'admin' ],
'page.3': [ 'superAdmin', 'adviser', 'admin' ],
'page.4': [ 'superAdmin', 'adviser' ],
'page.5': [ 'superAdmin' ],
'page.6': [ 'superAdmin', 'adviser' ],
'page.7': [ 'superAdmin', 'admin' ],
'page.8': [ 'superAdmin', 'admin' ],
'page.9': [ 'superAdmin' ],
'page.10': [ 'superAdmin', 'adviser', 'admin' ],
'page.11': [ 'superAdmin' ],
'page.12': [ 'superAdmin' ],
'page.13': [ 'superAdmin', 'admin' ],
'page.14': [ 'superAdmin' ],
'page.15': [ 'superAdmin', 'admin' ],
'page.16': [ 'superAdmin' ],
'page.17': [ 'superAdmin', 'admin' ],
'page.18': [ 'superAdmin' ],
'page.19': [ 'superAdmin' ],
'page.20': [ 'superAdmin', 'adviser', 'admin' ]
};
const permissionForCurrentRoute = permissions[currentRoute];
return Roles.userIsInRole(loggedInUser, permissionForCurrentRoute);
}
Here’s how I call the method :
Meteor.call('users.havePermission', currentRoute, (error, userHavePermission) => {
if (!userHavePermission) {
// If not, send directly to the home page without any warning.. Boom!
FlowRouter.redirect('/');
} else {
// If everything is in order, let him pass!
const userName = loggedInUser.profile.name;
const loggedInUserEmail = loggedInUser.emails[0].address;
LocalState.set('LOGGED_IN_USER_EMAIL', loggedInUserEmail);
const isAdmin = loggedInUser.roles.includes('admin');
const isSuperAdmin = loggedInUser.roles.includes('superAdmin');
onData(null, {notifications, userName, currentRoute, isAdmin, isSuperAdmin,
uuid, RouteTransition});
return clearAllNotifications;
}
});
Strangely, this method sometimes (Not all the time) returns an error like this TypeError: Cannot read property 'includes' of undefined
which might be contributing to the delay. No idea.
My main question is, is it possible to speed things up by caching this on the server? How can I do it?