node.js - Cannot verify Hashed password in Express using Node js -


this how hash , store password in database.

newuser function

 var salt = bcrypt.gensaltsync(10); var hash = bcrypt.hashsync(password, salt); var query="insert user(email,firstname,lastname,logintime,gender,password) values('"+email+"','"+firstname+"','"+lastname+"','"+logintime+"','"+gender+"','"+hash+"')"; 

this how retrieve , check authenticate

validate function

var query = "select password user email='" + email +  "'"; connection.query(query,function(err,result){     if(err)         {         console.log("error:"+err.message);         }     else         {         if(result.length!==0)          {  var hash=json.stringify(result[0].password); console.log(hash);     console.log(bcrypt.comparesync(password,hash ));   if(bcrypt.comparesync(password, hash)) { callback(err, result); } 

this shows false if way shows expected result

var hash = bcrypt.hashsync("sacjap", 8);             //var hash=json.stringify(result[0].password);             console.log(hash);             console.log(bcrypt.comparesync(password,hash ));             if(bcrypt.comparesync(password, hash))                 {             callback(err, result);                 } 

so problem whenever password database not working. plz

first of all, answer based on documentation found here: https://github.com/davidwood/node-password-hash

it seems password-hash module tries call 'split' function on second argument provided 'verify' function, assuming string (javascript string split function on mdn). think should check type of 'result' variable, looks me more complex query result object returned database. provided code doesn't give me more information type of connection using here, can't give more specific answer. approach find out how plain string 'result' variable, represent hashed password can hand on 'verify'. wild guess, hope little hint helps solve problem.

side note: module using password hashing appears deprecated, maybe should out alternative.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -