Skip to content

Commit 0427242

Browse files
committed
[backend] Added rapi endpoint for change user password
1 parent 3844520 commit 0427242

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

Postman Collections/auth.postman_collection.json

+168
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,174 @@
12181218
}
12191219
]
12201220
},
1221+
{
1222+
"name": "change password",
1223+
"request": {
1224+
"auth": {
1225+
"type": "bearer",
1226+
"bearer": [
1227+
{
1228+
"key": "token",
1229+
"value": "{{currentAccessToken}}",
1230+
"type": "string"
1231+
}
1232+
]
1233+
},
1234+
"method": "POST",
1235+
"header": [],
1236+
"body": {
1237+
"mode": "raw",
1238+
"raw": "{\r\n \"newPassword\" : \"abcd\",\r\n \"confirmPassword\" : \"abcd\"\r\n}",
1239+
"options": {
1240+
"raw": {
1241+
"language": "json"
1242+
}
1243+
}
1244+
},
1245+
"url": {
1246+
"raw": "{{backendUrl}}user/change-password/",
1247+
"host": [
1248+
"{{backendUrl}}user"
1249+
],
1250+
"path": [
1251+
"change-password",
1252+
""
1253+
]
1254+
}
1255+
},
1256+
"response": [
1257+
{
1258+
"name": "success",
1259+
"originalRequest": {
1260+
"method": "POST",
1261+
"header": [],
1262+
"body": {
1263+
"mode": "raw",
1264+
"raw": "{\r\n \"newPassword\" : \"abcd\",\r\n \"confirmPassword\" : \"abcd\"\r\n}",
1265+
"options": {
1266+
"raw": {
1267+
"language": "json"
1268+
}
1269+
}
1270+
},
1271+
"url": {
1272+
"raw": "{{backendUrl}}user/change-password/",
1273+
"host": [
1274+
"{{backendUrl}}user"
1275+
],
1276+
"path": [
1277+
"change-password",
1278+
""
1279+
]
1280+
}
1281+
},
1282+
"status": "OK",
1283+
"code": 200,
1284+
"_postman_previewlanguage": "json",
1285+
"header": [
1286+
{
1287+
"key": "X-Powered-By",
1288+
"value": "Express"
1289+
},
1290+
{
1291+
"key": "Access-Control-Allow-Origin",
1292+
"value": "*"
1293+
},
1294+
{
1295+
"key": "Content-Type",
1296+
"value": "application/json; charset=utf-8"
1297+
},
1298+
{
1299+
"key": "Content-Length",
1300+
"value": "43"
1301+
},
1302+
{
1303+
"key": "ETag",
1304+
"value": "W/\"2b-BcqZXnJsIxPWaT5KqbeKFU83Xws\""
1305+
},
1306+
{
1307+
"key": "Date",
1308+
"value": "Wed, 06 Sep 2023 18:47:20 GMT"
1309+
},
1310+
{
1311+
"key": "Connection",
1312+
"value": "keep-alive"
1313+
},
1314+
{
1315+
"key": "Keep-Alive",
1316+
"value": "timeout=5"
1317+
}
1318+
],
1319+
"cookie": [],
1320+
"body": "{\n \"message\": \"Password changed successfully\"\n}"
1321+
},
1322+
{
1323+
"name": "failed",
1324+
"originalRequest": {
1325+
"method": "POST",
1326+
"header": [],
1327+
"body": {
1328+
"mode": "raw",
1329+
"raw": "{\r\n \"newPassword\" : \"abcd\",\r\n \"confirmPassword\" : \"abcda\"\r\n}",
1330+
"options": {
1331+
"raw": {
1332+
"language": "json"
1333+
}
1334+
}
1335+
},
1336+
"url": {
1337+
"raw": "{{backendUrl}}user/change-password/",
1338+
"host": [
1339+
"{{backendUrl}}user"
1340+
],
1341+
"path": [
1342+
"change-password",
1343+
""
1344+
]
1345+
}
1346+
},
1347+
"status": "Bad Request",
1348+
"code": 400,
1349+
"_postman_previewlanguage": "json",
1350+
"header": [
1351+
{
1352+
"key": "X-Powered-By",
1353+
"value": "Express"
1354+
},
1355+
{
1356+
"key": "Access-Control-Allow-Origin",
1357+
"value": "*"
1358+
},
1359+
{
1360+
"key": "Content-Type",
1361+
"value": "application/json; charset=utf-8"
1362+
},
1363+
{
1364+
"key": "Content-Length",
1365+
"value": "38"
1366+
},
1367+
{
1368+
"key": "ETag",
1369+
"value": "W/\"26-60NXatM0MTV9zlehOW2xis03Z3k\""
1370+
},
1371+
{
1372+
"key": "Date",
1373+
"value": "Wed, 06 Sep 2023 18:47:46 GMT"
1374+
},
1375+
{
1376+
"key": "Connection",
1377+
"value": "keep-alive"
1378+
},
1379+
{
1380+
"key": "Keep-Alive",
1381+
"value": "timeout=5"
1382+
}
1383+
],
1384+
"cookie": [],
1385+
"body": "{\n \"nonFieldError\": \"Password not match\"\n}"
1386+
}
1387+
]
1388+
},
12211389
{
12221390
"name": "logout",
12231391
"request": {

backend/controllers/user.controller.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import userModel from "@app/models/user";
2+
import bcrypt from "bcrypt";
23
import { sendMail } from "@app/utils/sendMail";
34
import { Request, Response } from "express";
45
import {
@@ -68,4 +69,23 @@ const resendEmailVerify = async (req: Request, res: Response) => {
6869
.json({ message: "Verification mail sent successfully" });
6970
};
7071

71-
export default { getUser, deleteUser, resendEmailVerify };
72+
/**
73+
* This controller is used to change password of user
74+
*/
75+
const changePassword = async (req: Request, res: Response) => {
76+
const { newPassword, confirmPassword } = req.body;
77+
// check if password match with confirm password
78+
if (!newPassword || !confirmPassword || newPassword !== confirmPassword)
79+
return res.sendCustomErrorMessage("Password not match", 400);
80+
// update password
81+
const hashedPassword = await bcrypt.hash(newPassword, 10);
82+
const user = await userModel.findOne({ _id: req.user._id });
83+
if (!user) return res.sendCustomErrorMessage("User not found", 400);
84+
user.password = hashedPassword;
85+
user.save((error) => {
86+
if (error) return res.sendMongooseErrorResponse(error);
87+
return res.status(200).json({ message: "Password changed successfully" });
88+
});
89+
};
90+
91+
export default { getUser, deleteUser, resendEmailVerify, changePassword };

backend/routes/user.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import userController from "@controllers/user.controller";
55
router.get("/:userId", userController.getUser);
66
router.delete("/me", userController.deleteUser);
77
router.post("/resendEmailVerification", userController.resendEmailVerify);
8+
router.post("/change-password", userController.changePassword);
89

910
module.exports = router;

0 commit comments

Comments
 (0)