fix: change password
This commit is contained in:
@@ -2,7 +2,7 @@ const userService = require("../services/user.service");
|
||||
const { ErrorHandler } = require("../helpers/error");
|
||||
const { setResponse } = require("../helpers/utils");
|
||||
const Joi = require("joi");
|
||||
const { userSchema } = require("../helpers/validation");
|
||||
const { userSchema, newPasswordSchema } = require("../helpers/validation");
|
||||
|
||||
class UserController {
|
||||
// Get all users
|
||||
@@ -112,14 +112,26 @@ class UserController {
|
||||
// Change user password
|
||||
static async changePassword(req, res) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { new_password } = req.body;
|
||||
const { new_password } = req.body;
|
||||
const { id } = req.params;
|
||||
|
||||
if (!id || !new_password) {
|
||||
throw new ErrorHandler(400, "user_id and new_password are required");
|
||||
if (!id || !new_password) {
|
||||
throw new ErrorHandler(400, "user_id and new_password are required");
|
||||
}
|
||||
|
||||
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(user_id, new_password);
|
||||
const result = await userService.changeUserPassword(id, new_password);
|
||||
return res.status(200).json(setResponse(result, "Password changed successfully", 200));
|
||||
} catch (error) {
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user