change password

master^2
Varun 9 months ago
parent ed9a155849
commit 982b8947f3

@ -645,6 +645,76 @@ exports.forgotPassword = async (req, reply) => {
}; };
exports.changePassword = async (req, reply) => {
try {
const {phone, oldPassword, newPassword } = req.body;
if (!oldPassword || !newPassword) {
return reply.send({
armintatankdata: {
error: true,
code: 10008,
message: "10008 - Old password and new password are required",
},
});
}
// Find user by phone
const user = await User.findOne({ phone });
if (!user) {
return reply.send({
armintatankdata: {
error: true,
code: 10006,
message: "10006 - User not found. Please check the phone number.",
},
});
}
// Verify old password
const isMatch = await bcrypt.compare(oldPassword, user.services.password.bcrypt);
if (!isMatch) {
return reply.send({
armintatankdata: {
error: true,
code: 10009,
message: "10009 - Incorrect old password",
},
});
}
// Hash new password
const hashedPassword = await bcrypt.hash(newPassword, 10);
// Update password
await User.updateOne(
{ phone },
{
$set: {
"services.password.bcrypt": hashedPassword,
oneTimePasswordSetFlag: false, // Reset OTP flag after password change
},
}
);
reply.send({
armintatankdata: {
error: false,
message: "Password changed successfully",
},
});
} catch (err) {
throw boom.boomify(err);
}
};
exports.forgotPasswordSupplier = async (req, reply) => { exports.forgotPasswordSupplier = async (req, reply) => {
try { try {
// Create a new Supplier object from the request body // Create a new Supplier object from the request body

@ -462,74 +462,74 @@ exports.changePassword = async (req, reply) => {
}; };
exports.verifyOldNewPassword = async (req, reply) => { // exports.verifyOldNewPassword = async (req, reply) => {
try { // try {
const { phone, oldPassword, newPassword } = req.body; // const { phone, oldPassword, newPassword } = req.body;
// Check if the user exists with the provided mobile number // // Check if the user exists with the provided mobile number
const user = await User.findOne({ phone }); // const user = await User.findOne({ phone });
if (!user) { // if (!user) {
return reply.send({ // return reply.send({
armintatankdata: { // armintatankdata: {
error: true, // error: true,
code: 10009, // code: 10009,
message: "User not found.", // message: "User not found.",
}, // },
}); // });
} // }
// Verify the old password
const isOldPasswordCorrect = await bcrypt.compare(oldPassword, user.services.password.bcrypt);
if (!isOldPasswordCorrect) {
return reply.send({
armintatankdata: {
error: true,
code: 10012,
message: "Old password is incorrect.",
},
});
}
// Hash the new password
const hashedNewPassword = await bcrypt.hash(newPassword, 10); // Ensure you use bcrypt.hash here
// Update the password in the database
const updateResult = await User.updateOne(
{ phone },
{
$set: {
"services.password.bcrypt": hashedNewPassword,
oneTimePasswordSetFlag: false,
},
}
);
// Check if the update was successful // // Verify the old password
if (updateResult.nModified > 0) { // const isOldPasswordCorrect = await bcrypt.compare(oldPassword, user.services.password.bcrypt);
// Fetch the updated user details to send back in the response // if (!isOldPasswordCorrect) {
const updatedUser = await User.findOne({ phone }).select('-services.password.bcrypt'); // Exclude the password // return reply.send({
// armintatankdata: {
// error: true,
// code: 10012,
// message: "Old password is incorrect.",
// },
// });
// }
return reply.send({ // // Hash the new password
armintatankdata: { // const hashedNewPassword = await bcrypt.hash(newPassword, 10); // Ensure you use bcrypt.hash here
error: false,
message: "Password changed successfully.", // // Update the password in the database
updatedUser, // Include the updated user details // const updateResult = await User.updateOne(
}, // { phone },
}); // {
} else { // $set: {
return reply.send({ // "services.password.bcrypt": hashedNewPassword,
armintatankdata: { // oneTimePasswordSetFlag: false,
error: true, // },
code: 10011, // }
message: "Failed to update the password. Try again.", // );
},
}); // // Check if the update was successful
} // if (updateResult.nModified > 0) {
} catch (err) { // // Fetch the updated user details to send back in the response
console.error("Error in changePassword:", err); // const updatedUser = await User.findOne({ phone }).select('-services.password.bcrypt'); // Exclude the password
throw boom.boomify(err);
} // return reply.send({
}; // armintatankdata: {
// error: false,
// message: "Password changed successfully.",
// updatedUser, // Include the updated user details
// },
// });
// } else {
// return reply.send({
// armintatankdata: {
// error: true,
// code: 10011,
// message: "Failed to update the password. Try again.",
// },
// });
// }
// } catch (err) {
// console.error("Error in changePassword:", err);
// throw boom.boomify(err);
// }
// };

@ -241,26 +241,26 @@ module.exports = function (fastify, opts, next) {
}); });
fastify.route({ // fastify.route({
method: "POST", // method: "POST",
url: "/api/change-password", // url: "/api/change-password",
schema: { // schema: {
tags: ["User"], // tags: ["User"],
description: "Users to change their password using mobile number, old password, and new password.", // description: "Users to change their password using mobile number, old password, and new password.",
summary: "Users to change their password using mobile number, old password, and new password.", // summary: "Users to change their password using mobile number, old password, and new password.",
body: { // body: {
type: "object", // type: "object",
required: ["phone", "oldPassword", "newPassword"], // required: ["phone", "oldPassword", "newPassword"],
properties: { // properties: {
phone: { type: "string"}, // phone: { type: "string"},
oldPassword: { type: "string"}, // oldPassword: { type: "string"},
newPassword: { type: "string" }, // newPassword: { type: "string" },
//confirmPassword: { type: "string", minLength: 6 }, // //confirmPassword: { type: "string", minLength: 6 },
}, // },
}, // },
}, // },
handler: validationHandler.verifyOldNewPassword, // Adjust the path to your handler // handler: validationHandler.verifyOldNewPassword, // Adjust the path to your handler
}); // });
// fastify.route({ // fastify.route({
@ -342,6 +342,35 @@ module.exports = function (fastify, opts, next) {
}, },
}); });
fastify.route({
method: "POST",
url: "/api/changePassword",
schema: {
tags: ["User"],
description: "This is to change password of user",
summary: "This is to change password of user",
body: {
type: "object",
required: ["phone"],
properties: {
phone: { type: "string" },
oldPassword: { type: "string" },
newPassword: { type: "string" },
},
},
security: [
{
basicAuth: [],
},
],
},
// preHandler: [validationHandler.],
handler: userController.changePassword,
});
fastify.route({ fastify.route({
method: "POST", method: "POST",
url: "/api/resetpassword", url: "/api/resetpassword",

Loading…
Cancel
Save