Skip to content

Instantly share code, notes, and snippets.

@vgheri
Created November 25, 2013 18:10
Show Gist options
  • Save vgheri/7645778 to your computer and use it in GitHub Desktop.
Save vgheri/7645778 to your computer and use it in GitHub Desktop.
function authorise(req, res, next) {
var apiAccessToken = req.body.apiAccessToken || null;
var userId = req.params.userId || req.body.userId || null;
if (apiAccessToken && userId) {
SecurityToken.authorise(apiAccessToken, userId)
.then(function(authorised) {
if (authorised) {
next();
}
else {
logger.log('info', 'User ' + userId + ' is not authorised. Request from address ' + req.connection.remoteAddress + '.');
res.json(401, {
error: "User is not authorised"
});
}
}, function(err) {
logger.log('error', 'An error has occurred while processing a request ' +
' from ' +
req.connection.remoteAddress + '. Stack trace: ' + err.stack);
res.json(500, {
error: err.message
});
});
}
else {
logger.log('info', 'Bad request from ' +
req.connection.remoteAddress + '. Api access token and user id are mandatory.');
res.json(400, {
error: 'Api access token and user id are mandatory.'
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment