I am generating reset password token using crypto-js version 4.1.1. But I am getting error when I post my API TypeError: crypto.randomBytes is not a function and everything is correct in my schema . I am using node version v16.16.0. How to overcome of this issue
// This is my code where I am using rest password
import crypto from 'crypto-js';
userSchema.methods.getResetToken = function () {
// Generating token
const resetToken = crypto.randomBytes(20).toString('hex');
//hashing and adding resetPasswordToken to userSchema
this.resetPasswordToken = crypto
.createHash("sha256")
.update(resetToken)
.digest("hex");
this.resetPasswordTime = Date.now() 15 * 60 * 1000;
return resetToken;
};
CodePudding user response:
// This is my code where I am using rest password
import crypto from 'crypto-js';
import { nanoid } from "nanoid";
userSchema.methods.getResetToken = function () {
// Generating token
//use uuid or nanoid
const resetToken = nanoid(64)
//hashing and adding resetPasswordToken to userSchema
this.resetPasswordToken =
crypto.SHA256(resetToken).toString(crypto.enc.Hex);
this.resetPasswordTime = Date.now() 15 * 60 * 1000;
return resetToken;
};
CodePudding user response:
crypto-js has a completely different but well documented interface
compared to node.js crypto library
// docs crypto-js
https://cryptojs.gitbook.io/docs/
// nodejs crypto library docs
https://nodejs.org/api/crypto.html
I found this problem solution from here:
https://peaku.co/questions/54762-el-cartero-genera-hash-criptografico-
da-error:-typeerror:-cryptocreatehash-no-es-una-funcion