Spaces:
Runtime error
Runtime error
Commit
·
6a978cd
1
Parent(s):
ed11216
add password update functionality in user update API
Browse files
src/controllers/user.controller.ts
CHANGED
|
@@ -164,7 +164,7 @@ const getAllUsers = async (req: Request, res: Response) => {
|
|
| 164 |
const updateUserById = async (req: Request, res: Response) => {
|
| 165 |
try {
|
| 166 |
const userId = parseInt(req.params.id, 10);
|
| 167 |
-
const { name, email, role_id, status } = req.body;
|
| 168 |
|
| 169 |
const user = await User.findByPk(userId);
|
| 170 |
|
|
@@ -184,6 +184,10 @@ const updateUserById = async (req: Request, res: Response) => {
|
|
| 184 |
if (status) {
|
| 185 |
user.status = status;
|
| 186 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
await user.save();
|
| 189 |
|
|
|
|
| 164 |
const updateUserById = async (req: Request, res: Response) => {
|
| 165 |
try {
|
| 166 |
const userId = parseInt(req.params.id, 10);
|
| 167 |
+
const { name, email, role_id, status, password } = req.body;
|
| 168 |
|
| 169 |
const user = await User.findByPk(userId);
|
| 170 |
|
|
|
|
| 184 |
if (status) {
|
| 185 |
user.status = status;
|
| 186 |
}
|
| 187 |
+
if (password) {
|
| 188 |
+
const hashedPassword = await hashPassword(password);
|
| 189 |
+
user.password = hashedPassword;
|
| 190 |
+
}
|
| 191 |
|
| 192 |
await user.save();
|
| 193 |
|
src/validators/user.validator.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import { checkSchema} from 'express-validator';
|
| 2 |
import User from '../models/users';
|
| 3 |
import Role from '../models/roles';
|
| 4 |
import { Op } from 'sequelize';
|
|
@@ -86,6 +86,13 @@ const validateUserUpdate = checkSchema({
|
|
| 86 |
},
|
| 87 |
},
|
| 88 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
});
|
| 90 |
|
| 91 |
export { validateUser, validateUserUpdate };
|
|
|
|
| 1 |
+
import { checkSchema } from 'express-validator';
|
| 2 |
import User from '../models/users';
|
| 3 |
import Role from '../models/roles';
|
| 4 |
import { Op } from 'sequelize';
|
|
|
|
| 86 |
},
|
| 87 |
},
|
| 88 |
},
|
| 89 |
+
password: {
|
| 90 |
+
optional: true,
|
| 91 |
+
isLength: {
|
| 92 |
+
options: { min: 8 },
|
| 93 |
+
errorMessage: 'Password must be at least 8 characters long',
|
| 94 |
+
},
|
| 95 |
+
},
|
| 96 |
});
|
| 97 |
|
| 98 |
export { validateUser, validateUserUpdate };
|