{"action":"created","issue":{"url":"https://api.github.com/repos/saintedlama/passport-local-mongoose/issues/91","labels_url":"https://api.github.com/repos/saintedlama/passport-local-mongoose/issues/91/labels{/name}","comments_url":"https://api.github.com/repos/saintedlama/passport-local-mongoose/issues/91/comments","events_url":"https://api.github.com/repos/saintedlama/passport-local-mongoose/issues/91/events","html_url":"https://github.com/saintedlama/passport-local-mongoose/issues/91","id":95927473,"number":91,"title":"how would i authenticate an admin user?","user":{"login":"tsm91","id":4459551,"avatar_url":"https://avatars.githubusercontent.com/u/4459551?v=3","gravatar_id":"","url":"https://api.github.com/users/tsm91","html_url":"https://github.com/tsm91","followers_url":"https://api.github.com/users/tsm91/followers","following_url":"https://api.github.com/users/tsm91/following{/other_user}","gists_url":"https://api.github.com/users/tsm91/gists{/gist_id}","starred_url":"https://api.github.com/users/tsm91/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tsm91/subscriptions","organizations_url":"https://api.github.com/users/tsm91/orgs","repos_url":"https://api.github.com/users/tsm91/repos","events_url":"https://api.github.com/users/tsm91/events{/privacy}","received_events_url":"https://api.github.com/users/tsm91/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-19T16:08:41Z","updated_at":"2015-07-19T17:26:07Z","closed_at":null,"body":"@saintedlama How can i check if `userSchema.admin` is true before successful authentication on admin login?"},"comment":{"url":"https://api.github.com/repos/saintedlama/passport-local-mongoose/issues/comments/122687198","html_url":"https://github.com/saintedlama/passport-local-mongoose/issues/91#issuecomment-122687198","issue_url":"https://api.github.com/repos/saintedlama/passport-local-mongoose/issues/91","id":122687198,"user":{"login":"saintedlama","id":123820,"avatar_url":"https://avatars.githubusercontent.com/u/123820?v=3","gravatar_id":"","url":"https://api.github.com/users/saintedlama","html_url":"https://github.com/saintedlama","followers_url":"https://api.github.com/users/saintedlama/followers","following_url":"https://api.github.com/users/saintedlama/following{/other_user}","gists_url":"https://api.github.com/users/saintedlama/gists{/gist_id}","starred_url":"https://api.github.com/users/saintedlama/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saintedlama/subscriptions","organizations_url":"https://api.github.com/users/saintedlama/orgs","repos_url":"https://api.github.com/users/saintedlama/repos","events_url":"https://api.github.com/users/saintedlama/events{/privacy}","received_events_url":"https://api.github.com/users/saintedlama/received_events","type":"User","site_admin":false},"created_at":"2015-07-19T17:26:07Z","updated_at":"2015-07-19T17:26:07Z","body":"I would add an express middleware before handing requests over to passport.\r\n\r\nI a typical passport-local-mongoose setup you'll have some login route configured as\r\n\r\n```javascript\r\nrouter.post('/login', passport.authenticate('local'), function(req, res) {\r\n  res.redirect('/');\r\n});\r\n```\r\n\r\nYou could add a requireAdmin custom middleware function before `passport.authenticate('local')`\r\n\r\n```javascript\r\nrouter.post('/login', requireAdmin(), passport.authenticate('local'), function(req, res) {\r\n  res.redirect('/');\r\n});\r\n```\r\n\r\nThe `requireAdmin` middlware has the responsibility to check if the model has an admin field set to true\r\n\r\n```javascript\r\nfunction requireAdmin() {\r\n  return function(req, res, next) {\r\n    User.findOne({ req.body.username }, function(err, user) {\r\n      if (err) { return next(err); }\r\n\r\n      if (!user) { \r\n        // Do something - the user does not exist\r\n      }\r\n\r\n      if (!user.admin) { \r\n        // Do something - the user exists but is no admin user\r\n      }\r\n\r\n      // Hand over control to passport\r\n      next();\r\n    });\r\n  }\r\n}\r\n```"}}