Closed mufidu closed 5 months ago
6f1c3603da
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
utils/passwordUtil.js
✓ https://github.com/mufidu/booku/commit/9f07d8464bf61cf054e905e836ac26d3340a0e6c Edit
Create utils/passwordUtil.js with contents:
• Create a new file `utils/passwordUtil.js` in the root directory.
• Inside `passwordUtil.js`, import bcrypt using `const bcrypt = require('bcrypt');`.
• Define and export an asynchronous function named `hashPassword` that takes a password as its argument, hashes it using bcrypt with a salt round of 10, and returns the hashed password.
• This utility will centralize the password hashing logic, making it reusable across different parts of the application.
utils/passwordUtil.js
✓ Edit
Check utils/passwordUtil.js with contents:
Ran GitHub Actions for 9f07d8464bf61cf054e905e836ac26d3340a0e6c:
routes/auth.routes.js
✓ https://github.com/mufidu/booku/commit/b6aa14ec41dbd84068648242edfe87fcb4855cb8 Edit
Modify routes/auth.routes.js with contents:
• Import the `hashPassword` function from `../utils/passwordUtil.js` at the top of the file.
• Replace the direct bcrypt hashing in the `/register` route with a call to `hashPassword(req.body.password)`. Await the result and use it as the value for `password` in the `User` model instance creation.
• This change ensures that password hashing in user registration uses the centralized utility function, reducing code duplication.
--- +++ @@ -1,5 +1,6 @@ const express = require('express'); const bcrypt = require('bcrypt'); +const { hashPassword } = require('../utils/passwordUtil.js'); const jwt = require('jsonwebtoken'); const User = require('../models/user.model.js'); @@ -7,7 +8,7 @@ router.post('/register', async (req, res) => { try { - const hashedPassword = await bcrypt.hash(req.body.password, 10); + const hashedPassword = await hashPassword(req.body.password); const user = new User({ username: req.body.username, email: req.body.email,
routes/auth.routes.js
✓ Edit
Check routes/auth.routes.js with contents:
Ran GitHub Actions for b6aa14ec41dbd84068648242edfe87fcb4855cb8:
routes/profile.routes.js
✓ https://github.com/mufidu/booku/commit/45e33417fcdf5b2e0eaa3091414571f633ed6bbc Edit
Modify routes/profile.routes.js with contents:
• Import the `hashPassword` function from `../utils/passwordUtil.js` at the top of the file.
• In the `PUT /` route, replace the direct bcrypt hashing with a call to `hashPassword(password)`. Await the result and use it to update the `password` field in `updateData`.
• This modification ensures that password updates through the profile route also utilize the centralized password hashing utility, maintaining consistency and reducing code duplication.
--- +++ @@ -1,6 +1,7 @@ const express = require('express'); const User = require('../models/user.model.js'); const bcrypt = require('bcrypt'); +const { hashPassword } = require('../utils/passwordUtil.js'); const router = express.Router(); @@ -27,7 +28,7 @@ if (username) updateData.username = username; if (email) updateData.email = email; if (password) { - const hashedPassword = await bcrypt.hash(password, 10); + const hashedPassword = await hashPassword(password); updateData.password = hashedPassword; }
routes/profile.routes.js
✓ Edit
Check routes/profile.routes.js with contents:
Ran GitHub Actions for 45e33417fcdf5b2e0eaa3091414571f633ed6bbc:
I have finished reviewing the code for completeness. I did not find errors for sweep/refactor_password_hashing_code
.
💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Details
There is some codes to hash password in
routes/auth.routes.js
androutes/profile.routes.js
. Refactor it so we only need to write it once and just call it when needed.Checklist
- [X] Create `utils/passwordUtil.js` ✓ https://github.com/mufidu/booku/commit/9f07d8464bf61cf054e905e836ac26d3340a0e6c [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_password_hashing_code/utils/passwordUtil.js) - [X] Running GitHub Actions for `utils/passwordUtil.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_password_hashing_code/utils/passwordUtil.js) - [X] Modify `routes/auth.routes.js` ✓ https://github.com/mufidu/booku/commit/b6aa14ec41dbd84068648242edfe87fcb4855cb8 [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_password_hashing_code/routes/auth.routes.js) - [X] Running GitHub Actions for `routes/auth.routes.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_password_hashing_code/routes/auth.routes.js) - [X] Modify `routes/profile.routes.js` ✓ https://github.com/mufidu/booku/commit/45e33417fcdf5b2e0eaa3091414571f633ed6bbc [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_password_hashing_code/routes/profile.routes.js) - [X] Running GitHub Actions for `routes/profile.routes.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_password_hashing_code/routes/profile.routes.js)