简体   繁体   中英

I don't know why bcrypt.compare with hashed password isn't same

I really don't know about this problem about bcrypt.compare function when someone join my website, creating password with hash

const hash = await bcrypt.hash(u_password, 12);
const user = await User.create({
  u_email,
  u_password: hash,
  u_name,
  u_birth,
  u_nation,
  u_phnbr,
  u_sex,
});

and in login I just compare with using bcrypt.compare

  module.exports = (passport) => {
  passport.use(new LocalStrategy({
    usernameField: 'u_email',
    passwordField: 'u_password',
  }, async (u_email, u_password, done) => {
    try {
      const exUser = await User.findOne({ where: { u_email } });
      if (exUser) {
        const result = await bcrypt.compare(u_password, exUser.u_password);

        console.log(u_password);
        console.log(exUser.u_password);

        if (result) {
          done(null, exUser);
        } else {
          done(null, false, { message: 'incorrect password.' });
        }
      } else {
        done(null, false, { message: 'non-exist user'});
      } 
    } catch (error) {
      console.log(error);
      done(error);
    }
  }));
};

when I excute code just return incorrect-password why bcrypt.compare return false? Please help me

I found the solution. because In database password column size is too small for hashing I extend password column in database it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM