shaneMangudi / bcrypt-nodejs

Native implementation of bcrypt for NodeJS
Other
574 stars 69 forks source link

Not a valid BCrypt hash. #79

Open PoojaShah9 opened 7 years ago

PoojaShah9 commented 7 years ago

I am using this with Angular 1.5 + nodeJS + mysql.

Following is code to store password in database:

 var newUserMysql = {
                            firstname: req.body.fname,
                            lastname: req.body.lname,
                            username: username,
                            password: bcrypt.hashSync(password),
                            createdDateTime : new Date()
                        };

                        var insertQuery = "INSERT INTO users ( FirstName,LastName,Email, Password,CreatedDateTime ) values (?,?,?,?,?)";

                        connection.query(insertQuery, [newUserMysql.firstname,newUserMysql.lastname,newUserMysql.username, newUserMysql.password,newUserMysql.createdDateTime], function (err, rows) {
                            newUserMysql.id = rows.insertId;
                            return done(null, newUserMysql);
                        });

it is saving data correctly. Now while login I am trying to compare password with following code, I am getting error of

C:\Users\SONY.ssh\KoruLife\node_modules\mysql\lib\protocol\Parser.js:79 throw err; // Rethrow non-MySQL errors ^ Not a valid BCrypt hash.

Code for Login :

 passport.use(
        'local-login',
        new LocalStrategy({
                // by default, local strategy uses username and password, we will override with email
                usernameField: 'username',
                passwordField: 'password',
                passReqToCallback
passport.use(
        'local-login',
        new LocalStrategy({
                // by default, local strategy uses username and password, we will override with email
                usernameField: 'username',
                passwordField: 'password',
                passReqToCallback: true // allows us to pass back the entire request to the callback
            },
            function (req, username, password, done) { // callback with email and password from our form
                connection.query("SELECT * FROM users WHERE email = ?", [username], function (err, rows) {
                    if (err)
                        return done(err);
                    if (!rows.length) {
                        return done(null,false); // req.flash is the way to set flashdata using connect-flash
                    }
                    //var pwd = bcrypt.hashSync(password, null, null);
                    if(!bcrypt.compareSync(password, rows[0].Password)){
                        return done(null,false);
                    }
                    return done(null, rows[0]);
                });
            })
    );