title: passport js local authentication using sequelize
body: <p>I am trying to use passport local auth with sequelize . When I submit login form, the request/respond cycle never end and there is no error message in the terminal . </p>
  
  <p>Here are all of my codes:</p>
  
  <p>app.js</p>
  
  <pre><code>var Sequelize = require('sequelize'),
      express = require('express'),
      bodyParser = require('body-parser'),
      cookieParser = require('cookie-parser'),
      passport = require('passport'),
      LocalStrategy = require('passport-local').Strategy,
      User = require('./models/users');
      ........ and other imports.....
  
      //route import , model  injection
      var auth = require('./routes/auth')(User);
  
      .......................
  app.use(session({
      store: new RedisStore(),
      secret: 'keyboard cat',
      resave: false,
      saveUninitialized: false
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  
  passport.serializeUser(function(user, done) {
      console.log(user);
      done(null, user.id);
  });
  
  passport.deserializeUser(function(id, done) {
      User.findById(id).then(function(user){
          done(null, user);
      }).catch(function(e){
          done(e, false);
      });
  });
  
  passport.use(new LocalStrategy(
      function(username, password, done) {
         User.findOne({where: {username: username}}).then(function(err, user) {
             if (err) { return done(err); }
             if (!user) {
                 console.log('Incorrect username.');
                 return done(null, false, { message: 'Incorrect username.' });
             } else if (password != user.password) {
                 console.log('Incorrect password');
                 return done(null, false, { message: 'Incorrect password.' });
             } else {
                 console.log('ok');
                 done(null, user);
             }
          });
  
      }
  
  ));
  </code></pre>
  
  <p>and routes/auth.js :</p>
  
  <pre><code>var express = require('express'),
      passport = require('passport');
  
  var routes = function(User) {
      var router = express.Router();
  
      // routes for registration
      router.route('/register')
          .get(function(req, res) {
              res.render('register');
          })
          .post(function(req, res) {
             User.count().then(function(number) {
                 if (number &gt;= 1) {
                     res.redirect('/auth/login');
                 } else {
                     User.create({
                         username: req.body.username,
                         password: req.body.password
                     });
  
                     res.redirect('/auth/login');
                 }
             });
          });
      //routes for login
      router.route('/login')
          .get(function(req, res) {
              res.render('login');
          })
          .post(function(req, res) {
              passport.authenticate('local', { successRedirect: '/dashboard',
                  failureRedirect: '/auth/login' });
  
          });
      return router;
  };
  
  module.exports = routes;
  </code></pre>