generate password reset token in node.js

chovy picture chovy · Sep 25, 2012 · Viewed 8k times · Source

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');

Answer

michaelmerg picture michaelmerg · Sep 25, 2012

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