fix: change password

This commit is contained in:
2025-10-09 09:01:08 +07:00
parent ad0f44669b
commit 3fd4a4c1b7
2 changed files with 21 additions and 12 deletions

View File

@@ -2,7 +2,7 @@ const userService = require("../services/user.service");
const { ErrorHandler } = require("../helpers/error"); const { ErrorHandler } = require("../helpers/error");
const { setResponse } = require("../helpers/utils"); const { setResponse } = require("../helpers/utils");
const Joi = require("joi"); const Joi = require("joi");
const { userSchema } = require("../helpers/validation"); const { userSchema, newPasswordSchema } = require("../helpers/validation");
class UserController { class UserController {
// Get all users // Get all users
@@ -112,14 +112,26 @@ class UserController {
// Change user password // Change user password
static async changePassword(req, res) { static async changePassword(req, res) {
try { try {
const { id } = req.params;
const { new_password } = req.body; const { new_password } = req.body;
const { id } = req.params;
if (!id || !new_password) { if (!id || !new_password) {
throw new ErrorHandler(400, "user_id and new_password are required"); throw new ErrorHandler(400, "user_id and new_password are required");
} }
const result = await userService.changeUserPassword(user_id, new_password); const { error } = newPasswordSchema.validate({ new_password });
if (error) {
const errors = error.details.reduce((acc, cur) => {
const field = Array.isArray(cur.path) ? cur.path.join('.') : String(cur.path);
if (!acc[field]) acc[field] = [];
acc[field].push(cur.message);
return acc;
}, {});
return res.status(400).json(setResponse(errors, 'Validation failed', 400));
}
const result = await userService.changeUserPassword(id, new_password);
return res.status(200).json(setResponse(result, "Password changed successfully", 200)); return res.status(200).json(setResponse(result, "Password changed successfully", 200));
} catch (error) { } catch (error) {
return res return res

View File

@@ -115,9 +115,6 @@ class UserService {
...(email && { user_email: email }), ...(email && { user_email: email }),
...(phone && { user_phone: phone }), ...(phone && { user_phone: phone }),
...(role_id !== undefined && { role_id }), ...(role_id !== undefined && { role_id }),
...(is_sa !== undefined && { is_sa }),
...(is_active !== undefined && { is_active }),
...(is_approve !== undefined && { is_approve }),
...(updatedById !== undefined && { updated_by: updatedById }) ...(updatedById !== undefined && { updated_by: updatedById })
}; };
@@ -163,10 +160,10 @@ class UserService {
}; };
// Change password // Change password
changeUserPassword = async (userId, newPassword) => { changeUserPassword = async (user_Id, new_Password) => {
try { try {
const hashedPassword = await hashPassword(newPassword); const hashedPassword = await hashPassword(new_Password);
await changeUserPasswordDb(userId, hashedPassword); await changeUserPasswordDb(user_Id, hashedPassword);
return { message: "Password updated successfully" }; return { message: "Password updated successfully" };
} catch (error) { } catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message); throw new ErrorHandler(error.statusCode || 500, error.message);