Ahmad ul hoq Nadim
Ahmad ul hoq Nadim

Reputation: 378

Node js passport local authentication is keep on loading

I am creating an app with node js (express and mysql), trying to do authentication with passport local. when i click on log in, everything running fine but after comparing credentials success redirect not working (keep loading). but if credentials not matched then failure redirect is working fine without any error.

what is wrong? where? can anyone help to find out? please...

my login route:

router.post('/login', (req, res, next) => { 
       console.log('post login.');          //never logged, why?
    passport.authenticate('local', {
      successRedirect: '/admin/dashboard',
      failureRedirect: '/login',
      failureFlash: true
    })(req, res, next);
  });

passport config:

const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');

// Load User model
const User = require('../config/db').User;

module.exports = function(passport) {
  passport.use(
    new LocalStrategy({usernameField: 'email',  passwordField: 'password' }, (username, password, done) => {
      // Match user
      User.findOne({where: {email: username}}).then(user => {

        console.log(user.name);        //logged user name.

        if (!user) {

          return done(null, false, { message: 'That email is not registered' });
        }

        // Match password
        const isMatched = bcrypt.compareSync(password, user.password);

          if (isMatched) {
              console.log('password matched.');         //logged when password matched.
            return done(null, user);
          } else { console.log('password not matched.');    //logged when password not matched.
            return done(null, false, { error_msg: 'Password incorrect' });
          } 
      });
    })
  );

  passport.serializeUser(function(user, done) {
      console.log('serialized: ', user.id);       //logged when credentials matched.
    return done(null, user.id);
  });

  passport.deserializeUser(function(id, done) {
    User.findByPk(id, function(err, user) {
        console.log('deserialized');

      return done(null, user.id);
    });
  });

};

Upvotes: 0

Views: 1019

Answers (2)

kaleb teshale
kaleb teshale

Reputation: 89

When deserializing input the Id and you will return the user object. Do like this

 passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        console.log('deserialized');

      return done(null, user);
    });
  });

Upvotes: 0

Ahmad ul hoq Nadim
Ahmad ul hoq Nadim

Reputation: 378

After a few hours of googling, i found a solution for this, the problem here is in the deserializeUser method. I just change a little bit of code in the deserializeUser because: passport can't deserialize the user out of the session.

It worked well for me, and I hope it will work for someone with a problem like mine.

passport.deserializeUser(function(user, done) {
      return done(null, user);
    });

Upvotes: 3

Related Questions