title: Passport Not Authenticating Correctly
body: <p>This is my first time doing user authentication and I think I'm nearly complete. I'm using Express with Passport.js and MongoDB/Mongoose. When I run my gulp file everything loads correctly but when I attempt to login with my credentials I'm constantly redirected to the failure page (home screen). Here's my code:</p>
  
  <pre><code>// Initialize
  const bodyParser = require('body-parser')
  const path = require('path')
  const bcrypt = require('bcryptjs')
  const express = require('express')
  const session = require('express-session')
  const cookieParser = require('cookie-parser')
  const passport = require('passport'),
        LocalStrategy = require('passport-local').Strategy
  let userData = require('./models/user.js')
  const app = express()
  
  // Configuration
  app.use(cookieParser())
  app.use(session({ secret: 'ThisIsTheSessionSecret', resave: true, saveUninitialized: true }))
  app.use(passport.initialize())
  app.use(passport.session())
  app.use(express.static(path.join(__dirname, '../../docs')))
  app.use(express.static(path.join(__dirname, '../../src')))
  
  app.set('port', process.env.PORT || 8080);
  app.set('view engine', 'pug')
  app.set('views', 'src/pages')
  
  let urlencodedParser = bodyParser.urlencoded({ extended: false })
  
  // Configure authentication and sessions
  passport.use(new LocalStrategy(
    function(username, password, done) {
      user.findOne({ username: username }, function(err, user) {
        if (err) { console.log(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)
      })
    }
  ));
  
  // Session
  passport.serializeUser(function(user, done) {
    done(null, user.id)
  });
  
  passport.deserializeUser(function(id, done) {
    user.findById(id, function(err, user) {
      done(err, user)
    })
  })
  
  app.listen(app.get('port'))
  console.log('Listening on port: ' + app.get('port'))
  
  // Initialize database (local or remote?)
  const MongoClient = require('mongodb').MongoClient,
        mongoose = require('mongoose'),
        assert = require('assert')
  
  const database = 'mongodb://localhost:27017/database'
  mongoose.connect(database)
  
  let db = mongoose.connection
  db.on('error', console.error.bind(console, 'connection error'))
  
  db.once('open', function() {
    // let testUser = new userData.UserModel({ username: 'PodcatchUser', password: 'RIPHarambe' })
    // console.log(testUser.username)
  })
  
  // Routing
  app.get('/', function(req, res) {
      console.log('Welcome!')
      res.render('index')
  })
  
  // Registration
  app.post('/signup', urlencodedParser, function(req, res) {
      let username = req.body.username,
          password = req.body.password,
          salt = bcrypt.genSaltSync(10),
          hash = bcrypt.hashSync(password, salt)
  
      let user = new userData.UserModel({
          username: username,
          password: hash
      })
  
      console.log(user.username)
      console.log(user.password)
  
      user.save(function(err) {
          if (err) {
              return console.log(err, user)
          } else {
              console.log('User has been saved to the database successfully')
          }
      })
      res.end()
  })
  
  // Login
  app.post('/login', passport.authenticate('local', { successRedirect: '/success',
          failureRedirect: '/' }), function(req, res) {
          res.end()
  })
  </code></pre>
  
  <p>and the code in the userData module:</p>
  
  <p>const mongoose = require('mongoose')</p>
  
  <pre><code>let UserSchema = mongoose.Schema({
      username: String,
      password: String
  })
  
  let UserModel = mongoose.model('UserModel', UserSchema)
  
  module.exports.UserSchema = UserSchema
  module.exports.UserModel = UserModel
  </code></pre>
  
  <p>I'm really confused as to what's happening. Mongo saves the credentials and hashed password to the database, I create a session and have a cookie parser but it doesn't authenticate. Help would be greatly appreciated :)</p>