Skip to content

Instantly share code, notes, and snippets.

@PavelDemyanenko
Created February 17, 2015 17:18
Show Gist options
  • Save PavelDemyanenko/ce7b7a062dbc1fea61b4 to your computer and use it in GitHub Desktop.
Save PavelDemyanenko/ce7b7a062dbc1fea61b4 to your computer and use it in GitHub Desktop.
module.exports = function(req, res, next) {
var token;
if (req.headers && req.headers.authorization) {
var parts = req.headers.authorization.split(' ');
if (parts.length == 2) {
var scheme = parts[0],
credentials = parts[1];
if (/^Bearer$/i.test(scheme)) {
token = credentials;
}
} else {
return res.json(401, {err: 'Format is Authorization: Bearer [token]'});
}
} else if (req.param('token')) {
token = req.param('token');
// We delete the token from param to not mess with blueprints
delete req.query.token;
} else {
return res.json(401, {err: 'No Authorization header was found'});
}
sailsTokenAuth.verifyToken(token, function(err, token) {
if (err) return res.json(401, {err: 'The token is not valid'});
req.token = token;
next();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment