title: PassportJS: User Is Not Defined
body: <p>Yes, I see that this question has been asked <a href="https://stackoverflow.com/questions/23289925/referenceerror-user-is-not-defined">here</a> and <a href="https://stackoverflow.com/questions/28143587/user-is-not-defined-nodejs-express-passport">here</a>, but the answers point to either making changes to your <code>deserializeUser</code> callback, or changing something in your Mongoose model.  </p>
  
  <p>I've tried the first to no avail and am using the regular ol' NodeJS driver, so I'm not quite sure where to pinpoint the root cause of my issue.  </p>
  
  <p>Here's my script:</p>
  
  <p><strong>AUTHENTICATE.JS:</strong></p>
  
  <pre><code>var app = require('express');
  var router = app.Router();
  var assert = require('assert');
  var bcrypt = require('bcrypt');
  var passport = require('passport');
  var LocalStrategy = require('passport-local').Strategy;
  
  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });
  
  passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
      done(err, user);
    });
  });
  
  passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  },
    function(username, password, done) {
  User.findOne({ username: username }, function(err, user) {
    if (err) { return done(err); }
    if (!user) {
      return done(null, false, { message: 'Incorrect username.' });
    }
    if (!user.validPassword(password)) {
      return done(null, false, { message: 'Incorrect password.' });
    }
    return done(null, user);
      });
    }
  ));
  
  router.post('/login',
     passport.authenticate('local', {successRedirect:'/',   
       failureRedirect:'/login',failureFlash: false}),
        function(req, res) {
        res.redirect('/');
  });
  </code></pre>
  
  <p>For my <code>app.js</code> file I've tried a number of the fixes suggested in similar questions but nothing has made a difference.  This is what it looks like:</p>
  
  <p><strong>APP.JS:</strong></p>
  
  <pre><code>var express = require('express');
  var app = express();
  var bodyParser = require('body-parser');
  var cookieParser = require('cookie-parser');
  var expressValidator = require('express-validator');
  var passport = require('passport');
  var localStrategy = require('passport-local').Strategy;
  var mongo = require('mongodb').MongoClient;
  var session = require('express-session');
  var bcrypt = require('bcrypt');
  
  // Express Session
  app.use(session({
    secret: 'keyboard cat',
    saveUninitialized: true,
    resave: true,
    cookie: {secure: true}
  }));
  
  
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  
  //initialize passport for app
  app.use(passport.initialize());
  app.use(passport.session());
  
  // make mongodb available to the application
  app.use((req, res, next) =&gt; {
    mongo.connect('mongodb://localhost:27017/formulas', (e, db) =&gt; {
    if (e) return next(e);
    req.db = db;
    next();
    });
  });
  
  app.set('view engine', 'ejs');
  app.set('views', __dirname + '/views');
  app.use(express.static(__dirname + '/public'));
  
  // Express Validator
  app.use(expressValidator({
    errorFormatter: function(param, msg, value) {
        var namespace = param.split('.')
        , root    = namespace.shift()
        , formParam = root;
  
    while(namespace.length) {
        formParam += '[' + namespace.shift() + ']';
    }
    return {
        param : formParam,
        msg   : msg,
        value : value
      };
     }
    }));
  
  //define routes here
  
  
  app.set('port', (process.env.PORT || 3000));
  
  app.listen(app.get('port'), function(){
    console.log("The server is now listening on port "+app.get('port'));
  });
  
  module.exports = app;
  </code></pre>
  
  <p>Any help would be greatly appreciated, thank you.</p>