Created
November 25, 2013 18:10
-
-
Save vgheri/7645778 to your computer and use it in GitHub Desktop.
This file contains 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
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