This is the code:
router.post("/form", async (req, res) => {
try {
let { name, email, password } = req.body;
let user = await User.create({
name,
email,
password,
});
if (!user) return res.status(501).send("Something went wrong");
let token = await user.getSignedToken();
console.log(token); //Server logs this
user.emailToken = await user.verifyEmail();// This property is not being added in the saved mongodb document. It should, but again idk why it's not
await user.save({ validateBeforeSave: false });
console.log(user); // Server doesn't log this
const options = {
expires: new Date(
Date.now() + process.env.JWT_COOKIE_EXPIRE * 24 * 60 * 60 * 1000
),
};
console.log(options); // Server doesn't log this
res.cookie("token", token, options);
res.send("Check email for activation");
} catch (ex) {
res.send(ex);
}
});
Below is the verifyEmail method (in userSchema.. I'm using mongoose):
userSchema.methods.verifyEmail = async function () {
let emailToken = this._id + crypto.randomBytes(10).toString("hex");
let emailTokenHashed = await crypto
.createHash("sha256")
.update(passwordToken)
.digest("hex");
this.emailToken = emailTokenHashed;
return emailToken;
};
Now, it should send "Check email for activation", right? But, it's sending an empty response object. Also, if you see in the code, the server is logging only one console.log (i.e. the first console.log). But, it is sending a cookie (I checked via postman). So, what's the issue here?
cryptowhile creating a hash. nodejs.org/dist/latest-v12.x/docs/api/…this.save()in your Schema method instead of doinguser.emailToken = await user.verifyEmail()and then the saving? Or you could also just doawait user.verifyEmail()without setting it touser.emailTokenbecause it's already set in the Schema method or isn't it? Am I missing something here?