Created
January 15, 2019 20:01
-
-
Save gwuah/eab7cfe8f01b9f4b6c497278e2657bb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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