generate password reset token in node.js

99封情书 提交于 2019-12-07 03:18:10

问题


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

回答1:


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




回答2:


function customToken() {
    var buffreValue = new Buffer(64);
    for (var i = 0; i < buffreValue.length; i++) {
        buffreValue[i] = Math.floor(Math.random() * 256);
    }
    var token = buffreValue.toString('base64');
    return token;
}
var getToken = customToken()


来源:https://stackoverflow.com/questions/12578248/generate-password-reset-token-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!