简体   繁体   中英

Error in hashing password with bcrypt-nodejs

Have been trying to create a password hash in my nodejs code But it not working and not showing any error message for me to debug. And my user is not creating also.

I dont know if there is a better way to do it.

This is my file which is responsible for the code...Model/User.js

const Promise = require('bluebird')
const bcrypt = Promise.promisifyAll(require('bcrypt-nodejs'))

function hashPassword (user) {
const SALT_FACTOR = 8

if (!user.changed('password')) {
return;
}

return bcrypt
.genSaltSyncAsync(SALT_FACTOR)
.then(salt => bcrypt.hashAsync(user.password, salt, null))
.then(hash => {
user.setDataValue('password', hash)
})}   

module.exports = (sequelize, DataTypes) => {

const User = sequelize.define('User', {
 email: {
     type: DataTypes.STRING,
     unique: true
 },
 password: DataTypes.STRING
 }, {
 hooks: {
     beforeCreate: hashPassword,
     beforeUpdate: hashPassword,
     beforeSave: hashPassword
 }
 })
 User.prototype.comparePassword = function (password) {
return bcrypt.compareAsync(password, this.password)
}
return User }

Does the following snippet help in any way?

const bcrypt = require('bcryptjs');

const userAbc = {
    email: 'user@user.com',
    password: '1234'
}


async function hashPassword(user) {
    try {
        const hashedPassword = await bcrypt.hash(user.password, 12);
        user.password = hashedPassword; 

        console.log(user);
    } catch (err) {
        console.log(err);
    }
}

hashPassword(userAbc);

I did change the bcrypt-nodejs to bcryptjs then replace genSaltSyncAsync to genSaltSync and everyother Async to Sync and it worked.

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