Saturday 31 August 2019

Very Peculiar Mongo Behaviour !

I'm using Passport, Passport-local-mongoose for User Registration and Management.When i create a token hash. It just get saved with "user.resetPasswordToken". This particular field is not existing in the User Schema Model until now when I have saved it.The peculiar part is if I change the name to anything else for example "emailverificationToken" then the token does not get saved in the database nor is the field created.Infact other than "resetPasswordToken" field I cannot add any other field to the user doc.Not really getting why.P.S. "resetPasswordToken" as already something I use for forget password system. Hence want to change the name to "emailverificationToken". But it's just not happeningBelow is part of my code for User Email Verification: -User.register(newUser, req.body.password, function (err, user) {if (err) {console.log(err);req.flash("error", err.message);return res.redirect("/register");}passport.authenticate("local")(req, res, async function () {try {var user = await User.findOne({ _id: req.user._id });console.log(user);var userEmail = user.email;var host = req.headers.host;var token = await crypto.randomBytes(20).toString('hex');user.resetPasswordToken = token;user.emailverificationToken = token;await user.save();await sendEmailVerificationMail(userEmail, user.username, host, token);req.flash("success", \An e-mail has been sent to verify ${userEmail}.`);res.redirect("/");} catch (err) {console.log(err);req.flash("error", err.message);return res.redirect("/");}});});});`​Result{emailVerified: false,_id: "78839da18de156252410edf6b8",username: "abc",firstName: "abc",lastName: "abc",email: "abc@gmail.com",mobile: "2625351153",accountCreated: "2019-08-31T06:51:10.848Z",__v: 0,resetPasswordToken: "ads5fsdfg5df4gsd5fhsdfhsdhsdd"}Issue :- No filed created for "user.emailverificationToken = token;"Thanks in advance!

Submitted August 31, 2019 at 08:07AM by ujshah

No comments:

Post a Comment