I've a Sails js app for 3 user groups, Site admin, Private User & Public user.
Now url pattern are like below for Admin,
'get /admin/foo': 'fooController.viewAll',
'post /admin/foo': 'fooController.add',
'put /admin/foo/:fooID': 'fooController.edit',
Url pattern for authenticated user,
'get /businesses/foo': 'fooController.viewAll',
'post /businesses/foo': 'fooController.add',
'put /businesses/foo/:fooID': 'fooController.edit',
Url pattern for public user,
'get /public/foo': 'fooController.viewAll',
I want to have a policy to authenticate user based on url pattern, if url is like /admin, it'll check user is admin? else if /business it will check whether user is our existing user or not.
There is a field named originalUrl
in request object. You can access it in your policy and decide depending on the url. Policy would be something like following.[I am assuming you know how to define a policy and add it in the configuration.]
module.exports = function(req, res, next) {
var originalUrl = req.originalUrl;
var tokenizedOriginalUrl = originalUrl.split('/');
if(tokenizedOriginalUrl[0] == 'admin'){
// do something
next(null);
}
else if(tokenizedOriginalUrl[0] == 'business'){
// do something
next(null);
}
else if(tokenizedOriginalUrl[0] == 'public'){
// do something
next(null);
}
else{
next("Access prohibited.");
}
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments