How to reset password in passport-local strategy in Sails.js

Passport.js provides an authentication framework in Node.js. It applies only to authentication.

Now I would like to enable the password reset. Since the user model does not have a password field, only a passport, how can I reset the password in the passport-local strategy? I assume that the user needs to generate a new password and call something in order to override the existing hash of the old password. What methods are there and where can I find them?

+5
source share
1 answer

When the user selects his reset his password, you can send an email to the user with a link containing a token associated with the user. As soon as the user clicks on the link, you check the user based on the token and email, and then show the reset HTML code. When the user enters a new password, in the base code you set the password in the User object after hashing, and then save it. You can also set the token to zero.

Sample code with base 64 will be as shown below

 user.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64'); user.password = user.hashPassword('newPassword'); user.token = undefined; user.save(...) 

The hashPassword method is used.

 UserSchema.methods.hashPassword = function(password) { if (this.salt && password) { return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64'); } else { return password; } }; 

The above code is auto generated using Yeoman

0
source

All Articles