When I update the password it is done. But the method used in the schema is not work.
// route file
route.put("/update-password", auth_middleware, update_password);
// userController file
const update_password = asyncHandler(async (request, response) => {
try {
const { id } = request.user;
const { password } = request.body;
validateMongodbId(id);
const findUser = await user_model.findById(id);
createPasswordResetToken()
if (password) {
findUser.password = password;
const updateUser = await findUser.save();
response.json(updateUser);
}
} catch (err) {
throw new Error(`Errorrrrrrrrrrrrrrrrrr: ${err}`)
}
})
// This is all the code in the schema file
const mongoose = require("mongoose");
const { genSaltSync, hashSync, compareSync } = require("bcrypt");
const crypto = require('crypto');
// Schema
const schema_user = new mongoose.Schema(
{
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
mobile: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
block: {
type: Boolean,
default: false,
},
role: {
type: String,
default: "user",
},
cart: {
type: Array,
default: [],
},
address: [{ type: mongoose.Schema.Types.ObjectId, ref: "Address" }],
wishlist: [{ type: mongoose.Schema.Types.ObjectId, ref: "Product" }],
refreshToken: {
type: String,
},
passwordChangedAt: {type: mongoose.SchemaTypes.Date},
passwordResetToken: {type: String},
passwordResetExpires: {type: mongoose.SchemaTypes.Date},
},
{
timestamps: true,
}
);
// Hashing Password
schema_user.pre("save", function (next) {
if (!this.isModified("password")) next();
const salt = genSaltSync();
this.password = hashSync(this.password, salt);
next();
});
// comper password
function comparePassword(raw, hash) {
return compareSync(raw, hash);
}
schema_user.methods.createPasswordResetToken = async function () {
try {
const resteToken = crypto.randomBytes(32).toString("hex");
this.passwordResetToken = crypto
.createHash("sha256")
.update(resteToken)
.digest("hex");
this.passwordResetExpires = Date.now() + 10 * 60 * 1000; // 10 دقائق
console.log(this.passwordResetToken);
console.log(resteToken);
return resteToken;
} catch (err) {
throw new Error(`Errorrrrrrrrrr: ${err}`)
}
}
// Model
const user_model = mongoose.model("User_Authenticat", schema_user);
module.exports = {
user_model,
comparePassword,
createPasswordResetToken,
};
Create a path that enables the user to update the password. And everything was fine, but the problem with this method createPasswordResetToken in the scheme does not work at all.
I did expect these properties to be saved
passwordChangedAt: Date, passwordResetToken: String, passwordResetExpires: Date,
in the database, But this did not happen.
What do I do to make this method createPasswordResetToken work and save these fields passwordChangedAt passwordResetToken passwordResetExpires in the database when the user updates the password
To fix this issue and ensure that the createPasswordResetToken method is executed when you update the password, you need to call the method on the specific user instance instead of calling it as a standalone function.
Here's how you can modify your update_password route handler to make it work:
In the updated code, we call the createPasswordResetToken method on the findUser instance, and then we save the user object to the database with the updated password and the generated reset token.