Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Created July 21, 2015 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/fb09289b3b4cf113b2c3 to your computer and use it in GitHub Desktop.
Save alexbeletsky/fb09289b3b4cf113b2c3 to your computer and use it in GitHub Desktop.
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