Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate password reset token in node.js

How do I generate a password reset token in node.js that can be used in a url?

I just need the method for generating the token:

user.reset_password_token = ???;
user.reset_password_expire = expire_date;

Edit -- here's the solution:

user.reset_password_token = require('crypto').randomBytes(32).toString('hex');
like image 548
chovy Avatar asked Sep 25 '12 07:09

chovy


People also ask

How do I reset my node JS password?

It will be processed by the code below. const resetPassword = async (userId, token, password) => { let passwordResetToken = await Token. findOne({ userId }); if (! passwordResetToken) { throw new Error("Invalid or expired password reset token"); } const isValid = await bcrypt.

How do I reset my passport password in Javascript?

Resetting or changing passwords : You can reset or change passwords using 2 simple functions in passport-local-mongoose. They are setPassword function and changePassword functions. Normally setPassword is used when the user forgot the password and changePassword is used when the user wants to change the password.


1 Answers

I'm using this to generate my auth-token:

require('crypto').randomBytes(32, function(ex, buf) {
    var token = buf.toString('hex');
});

Crypto Node.js v0.8.9 Manual & Documentation

like image 135
michaelmerg Avatar answered Oct 21 '22 22:10

michaelmerg