-
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
[Finishes #187354251] Update user password #46
Conversation
src/controllers/authController.ts
Outdated
const token = req.headers.authorization?.split(' ')[1]; | ||
if (!token) { | ||
res.status(401).json({ | ||
ok: false, | ||
message: 'Unauthorized', | ||
}); | ||
return; | ||
} | ||
const decode = jwt.verify(token, process.env.JWT_SECRET as string) as { id: string }; | ||
if (!decode) { | ||
res.status(400).json({ | ||
ok: false, | ||
message: 'Invalid token', | ||
}); | ||
} |
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 guess you aren't supposed to write this particular code cuz there's a reusable function in middleware folder for that.
src/routes/authRoute.ts
Outdated
@@ -18,4 +17,6 @@ router.get('/google/callback', authenticateViaGoogle); | |||
// Route to login a user | |||
router.post('/login', login); | |||
|
|||
router.put('/update-password', updatePassword, isAuthenticated); |
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.
Hey Patrick,
I believe the authenticated middleware should be placed at the beginning of the updatePassword controller.
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.
Exactly, please the middleware should be between the route string and the controller function
@@ -93,5 +95,61 @@ const login = async (req: Request, res: Response): Promise<void> => { | |||
sendInternalErrorResponse(res, err); | |||
} | |||
}; | |||
const updatePassword = async (req: Request, res: Response): Promise<void> => { |
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.
Let's shift this controller to the userController
instead of keeping it in the authController
.
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.
No Yves anything related to auth should stay in auth. Normal these are the controllers that goes in auth
- Login
- Logout
- Forgot Password
- Change Password
- Reset password
@@ -93,5 +95,61 @@ const login = async (req: Request, res: Response): Promise<void> => { | |||
sendInternalErrorResponse(res, err); | |||
} | |||
}; | |||
const updatePassword = async (req: Request, res: Response): Promise<void> => { |
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.
No Yves anything related to auth should stay in auth. Normal these are the controllers that goes in auth
- Login
- Logout
- Forgot Password
- Change Password
- Reset password
src/routes/authRoute.ts
Outdated
@@ -18,4 +17,6 @@ router.get('/google/callback', authenticateViaGoogle); | |||
// Route to login a user | |||
router.post('/login', login); | |||
|
|||
router.put('/update-password', updatePassword, isAuthenticated); |
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.
Exactly, please the middleware should be between the route string and the controller function
3c6f7b5
to
8d4ac51
Compare
eaba3b6
to
ff623dc
Compare
src/controllers/authController.ts
Outdated
const updatePassword = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const { oldPassword, newPassword } = req.body; | ||
const token = req.headers.authorization?.split(' ')[1]; |
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.
Good morning! Since there's middleware handling user authorization, there's no need for you to redundantly check the user's authorization status in your code
} | ||
|
||
// Generate salt and hash new password | ||
const hashedNewPassword = await passwordEncrypt(newPassword); |
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.
Dont we have to validate newPassword?
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 are right. @A-gent64 validate passwords
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
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.
Don't we need to validate newPassword if iam not mistaken?
} | ||
|
||
// Generate salt and hash new password | ||
const hashedNewPassword = await passwordEncrypt(newPassword); |
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 are right. @A-gent64 validate passwords
ff623dc
to
be643e2
Compare
…r-the-Project-Repository [finishes 187511688]-implement-badges-status(CI/CD)-show-whether-pass…
be643e2
to
ae26381
Compare
…p-rwanda/e-commerce-mavericcks-bn into 187354251-ft-update-password
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 which is also with a middleware that checks if the user is authenticated.
Testing Instructions
Check on localhost:{PORT}/users/update-password (On Postman use POST method)
Related Issues
Reference any related issues or pull requests
Checklist
Please review the following checklist and make sure all tasks are complete before submitting: