Created
July 21, 2015 22:21
-
-
Save alexbeletsky/fb09289b3b4cf113b2c3 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 auth() { | |
return function (req, res, next) { | |
var token = headers() || query(); | |
if (!token) { | |
return next({message: 'access token missing', status: 401}); | |
} | |
req.mongo.accounts.findOne({accessToken: token}, function (err, account) { | |
if (err) { | |
return next(err); | |
} | |
if (!account) { | |
return next({message: 'not authorized', status: 401}); | |
} | |
req.account = account; | |
next(); | |
}); | |
function headers() { | |
return req.headers['x-token'] || req.headers['x-access-token']; | |
} | |
function query() { | |
return req.query.accessToken || req.query.accesstoken; | |
} | |
}; | |
} | |
module.exports = auth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment