-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#187354251 Update / changing user password #26
Conversation
21dc179
to
7ba2e95
Compare
src/controllers/userController.ts
Outdated
@@ -33,5 +34,38 @@ const createUser = async (req: Request, res: Response): Promise<void> => { | |||
res.status(500).send('Internal Server Error'); | |||
} | |||
}; | |||
const updatePassword = async (req: Request, res: Response): Promise<void> => { | |||
try { | |||
const { oldPassword, newPassword, email } = req.body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot request an email from a user who is already signed in. Instead, explore methods for changing or updating the password using various resources. Retrieve the email from the token or from the request object appended by an authentication middleware.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to extracting id from the token
src/controllers/userController.ts
Outdated
const user = await User.findOne({ | ||
where: { | ||
email: email, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
res.status(400).send('User not found'); | ||
return; | ||
} | ||
const match = await bcrypt.compare(oldPassword, user.password); | ||
if (!match) { | ||
res.status(400).send('The old password is incorrect!'); | ||
} | ||
const hashedNewPassword = await bcrypt.hash(newPassword, saltRound); | ||
await User.update( | ||
{ password: hashedNewPassword }, | ||
{ | ||
where: { | ||
email: email, | ||
}, | ||
} | ||
); | ||
res.status(200).json({ message: 'Successfully updated user password!' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done, but please ensure you include ok: true or ok: false in the response. For example
res.status(400).json({
ok: false,
message: 'User not found'
);
Please avoid using send()
and use json()
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/controllers/userController.ts
Outdated
logger.error('Error updating user:', error); | ||
res.status(500).send('Internal Server Error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the reusable function sendInternalErrorResponse from src/validations. Refer to how other controller functions have utilized it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed to using the reusable fucntions
src/docs/users.yml
Outdated
api/users/update-password: | ||
put: | ||
summary: Update user password | ||
consumes: | ||
- application/json | ||
produces: | ||
- application/json | ||
parameters: | ||
- in: body | ||
name: passwordUpdate | ||
description: Password update data | ||
required: true | ||
schema: | ||
type: object | ||
properties: | ||
oldPassword: | ||
type: string | ||
newPassword: | ||
type: string | ||
email: | ||
type: string | ||
responses: | ||
'200': | ||
description: Successfully updated user password | ||
schema: | ||
type: object | ||
properties: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done in the auth.yml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/routes/userRoute.ts
Outdated
import { Router } from 'express'; | ||
import { createUser } from '../controllers/userController'; | ||
import { createUser, updatePassword } from '../controllers/userController'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/', createUser); | ||
|
||
router.put('/update-password', updatePassword); | ||
export default router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be in th authRoute.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
05cc1a2
to
feb4cbc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes Patrick
Great work on the PR! Just a couple of suggestions:
- It might be beneficial to move the updatePassword controller into the user controller file for better code organization.
- Consider capturing the email from the logged-in user using the auth middleware from @JeanIrad instead of from the request body. This can enhance security and align with best practices for handling user data.
b9024c9
to
1d7a5d4
Compare
bf572db
to
e95789e
Compare
86204b3
to
9f670eb
Compare
187454249 ft middleware fns
9f670eb
to
39f3855
Compare
ft-resend-verification-link
39f3855
to
13e35e1
Compare
Purpose
The purpose of the following PR is to help the user to change his/her password but not when forgotten the first password
Changes Made
-Added a controller in userController.ts for updating/changing user password
-Added API endpoint for updating/changing password in userRouter.js
-Added tests
Testing Instructions
Check on localhost:{PORT}/users/update-password (On Postman use POST method)
Related Issues
No issues at the moment
Checklist
Please review the following checklist and make sure all tasks are complete before submitting: