Skip to content

Instantly share code, notes, and snippets.

@gwuah
Created January 15, 2019 20:01
Show Gist options
  • Save gwuah/eab7cfe8f01b9f4b6c497278e2657bb7 to your computer and use it in GitHub Desktop.
Save gwuah/eab7cfe8f01b9f4b6c497278e2657bb7 to your computer and use it in GitHub Desktop.
const User = require('./authConfig.js');
function authorizeRequestTo(resource) {
return (req, res, next) {
/*
This is all on you. You can decide to populate
the user's rolename on a different variable
(other than roleName). This just an example.
*/
const roleName = req.user.role.toLowercase();
const canAccess = User(roleName).canAccess({
resource: resource.toLowerCase(),
request: req // request object, from which we extract method and url
});
if (canAccess) {
next()
} else {
next(new unAuthorizedError())
}
}
}
function authenticateRequest() {
/* your authentication code here */
}
module.exports = {
authenticateRequest,
authorizeRequestTo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment