How to hash password before saving to db to be compatible with passport module (passport local)

前端 未结 3 1308
遇见更好的自我
遇见更好的自我 2020-12-15 10:04

I am using passport-local strategy of passport for authentication. In my express server, I am getting a register post request and I should save password to db for a new user

3条回答
  •  遥遥无期
    2020-12-15 10:50

    Have you tried this?

    https://www.npmjs.com/package/passport-local-authenticate

    var auth = require('passport-local-authenticate');
    
    auth.hash('password', function(err, hashed) {
      console.log(hashed.hash); // Hashed password
      console.log(hashed.salt); // Salt
    });
    
    auth.hash('password', function(err, hashed) {
      auth.verify('password', hashed, function(err, verified) {
        console.log(verified); // True, passwords match
      ));
    });
    
    auth.hash('password', function(err, hashed) {
      auth.verify('password2', hashed, function(err, verified) {
        console.log(verified); // False, passwords don't match
      ));
    });
    

提交回复
热议问题