abhishek-akbari01 commited on
Commit
a08dcde
·
1 Parent(s): 0469ebe

change password expose api

Browse files
src/controllers/auth.controller.ts CHANGED
@@ -6,6 +6,7 @@ import { logger } from '../utils/logger';
6
  import { sendMail } from '../utils/mailer';
7
  import { generateResetToken } from '../utils/tokenUtils';
8
  import { Op } from 'sequelize';
 
9
 
10
  export const login = async (req: Request, res: Response) => {
11
 
@@ -118,4 +119,44 @@ export const resetPassword = async (req: Request, res: Response) => {
118
  logger.error("Error in reset password: ", error);
119
  return res.status(500).json({ error: 'Internal server error' });
120
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  }
 
6
  import { sendMail } from '../utils/mailer';
7
  import { generateResetToken } from '../utils/tokenUtils';
8
  import { Op } from 'sequelize';
9
+ import { AuthenticatedRequest } from 'shared/interfaces/user.interface';
10
 
11
  export const login = async (req: Request, res: Response) => {
12
 
 
119
  logger.error("Error in reset password: ", error);
120
  return res.status(500).json({ error: 'Internal server error' });
121
  }
122
+ }
123
+
124
+ export const changePassword = async (req: AuthenticatedRequest, res: Response) => {
125
+ try {
126
+ const { currentPassword, newPassword, confirmPassword } = req.body;
127
+
128
+ const userId = req.user?.id;
129
+ if (!userId) {
130
+ return res.status(401).json({ error: 'Unauthorized' });
131
+ }
132
+
133
+ const user = await User.findByPk(userId);
134
+ if (!user) {
135
+ return res.status(404).json({ error: 'User not found' });
136
+ }
137
+
138
+ const isPasswordCorrect = await comparePassword(currentPassword, user.password);
139
+ if (!isPasswordCorrect) {
140
+ return res.status(400).json({ error: 'Invalid current password' });
141
+ }
142
+
143
+ if (newPassword !== confirmPassword) {
144
+ return res.status(400).json({ error: 'New passwords do not match' });
145
+ }
146
+
147
+ if (newPassword.length < 8) {
148
+ return res.status(400).json({ error: 'Password must be at least 8 characters long.' });
149
+ }
150
+
151
+ const hashedPassword = await hashPassword(newPassword);
152
+ user.password = hashedPassword;
153
+ await user.save();
154
+
155
+
156
+ return res.status(200).json({ message: 'Password changed successfully' });
157
+
158
+ } catch (error) {
159
+ logger.error("Error changing password: ", error);
160
+ return res.status(500).json({ error: 'Internal server error' });
161
+ }
162
  }
src/routes/auth.routes.ts CHANGED
@@ -1,9 +1,10 @@
1
  import express from "express";
2
  import { validateLogin } from "../validators/login.validator";
3
- import { forgotPassword, login, resetPassword } from "../controllers/auth.controller";
4
  import { handleValidationErrors } from "../middlewares/handleValidatorError";
5
  import { validateForgotPassword } from "../validators/forgotPassword.validator";
6
  import { validateResetPassword } from "../validators/resetPassword.validator";
 
7
 
8
  const authRouter = express.Router();
9
 
@@ -112,4 +113,44 @@ authRouter.post("/auth/forgot-password", validateForgotPassword, handleValidatio
112
  authRouter.post("/auth/reset-password", validateResetPassword, handleValidationErrors, resetPassword);
113
 
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  export default authRouter;
 
1
  import express from "express";
2
  import { validateLogin } from "../validators/login.validator";
3
+ import { forgotPassword, login, resetPassword, changePassword } from "../controllers/auth.controller";
4
  import { handleValidationErrors } from "../middlewares/handleValidatorError";
5
  import { validateForgotPassword } from "../validators/forgotPassword.validator";
6
  import { validateResetPassword } from "../validators/resetPassword.validator";
7
+ import { jwtMiddleware } from "../middlewares/authMiddleware";
8
 
9
  const authRouter = express.Router();
10
 
 
113
  authRouter.post("/auth/reset-password", validateResetPassword, handleValidationErrors, resetPassword);
114
 
115
 
116
+ /**
117
+ * @swagger
118
+ * /api/auth/change-password:
119
+ * post:
120
+ * summary: Change the current user's password
121
+ * tags: [Auth]
122
+ * requestBody:
123
+ * required: true
124
+ * content:
125
+ * application/json:
126
+ * schema:
127
+ * type: object
128
+ * required:
129
+ * - currentPassword
130
+ * - newPassword
131
+ * - confirmPassword
132
+ * properties:
133
+ * currentPassword:
134
+ * type: string
135
+ * description: The current password of the user
136
+ * newPassword:
137
+ * type: string
138
+ * description: The new password to be set
139
+ * confirmPassword:
140
+ * type: string
141
+ * description: Confirmation of the new password
142
+ * responses:
143
+ * 200:
144
+ * description: Password changed successfully
145
+ * 400:
146
+ * description: Validation errors or incorrect current password
147
+ * 401:
148
+ * description: Unauthorized, user not authenticated
149
+ * 500:
150
+ * description: Internal server error
151
+ */
152
+ authRouter.post('/auth/change-password', jwtMiddleware, changePassword);
153
+
154
+
155
+
156
  export default authRouter;