Created
February 17, 2015 17:18
-
-
Save PavelDemyanenko/ce7b7a062dbc1fea61b4 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
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