Node JS bcrypt compare returns false

724 views Asked by At

So, I've seen a few posts about bcrypt compare returning false, but none seem to be a solution for me.

A quick explanation of what's what:

login handled with passport. credentials stored in MySQL DB, using 'mysql' package.

These both seem to be working fine with plain text passwords.

The code of hashing and saving to the database:

    var newPlayer = (email,username,password,callback) =>{
        if(email && username &&password){
            bcrypt.hash(password, 16, function(err,hash){
                var sqlstr = sql.format("INSERT INTO players (email,username,password) VALUES (?,?,?);",[email,username, hash])
                console.log(sqlstr);
                connection.query(sqlstr,function(err, rows) {
                    if(err)console.log("[MYSQL] Error:",err);
                    if(callback)callback(err);
                })

            })

        } else {
            throw Error("[MYSQL]  New Player requires email, username and password");
        }

    }

and the code for comparing (snippet from passport config):

        DAO.getPlayer(email,function(player){
            if(player)
            bcrypt.compare(password,player.password,(err,match)=>{
                if(err){
                    console.log("[PASSPORT] BCRYPT ERR:",err)
                    done(null,false)
                }
                else if(match)
                return done(null,player)
                else
                return done(null, false)
            })
            else
            return done(null, false)
        })

Thanks in advance for any help, and if I need to specify something, let me know! :)

1

There are 1 answers

0
Sridhar On BEST ANSWER

The hash that bcrypt generates will be of 60 character. Try increasing the size of the column.

Hash Info

The characters that comprise the resultant hash are ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$.

Resultant hashes will be 60 characters long.

https://github.com/kelektiv/node.bcrypt.js#hash-info