Skip to content

Commit

Permalink
✨ feat: edit password controller added
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 committed Dec 29, 2023
1 parent c914b0f commit 5064a20
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
44 changes: 44 additions & 0 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,50 @@ const authController = {
}
},

// @desc Update a password
// @route PATCH /api/auth/newPassword
// @access Private
updatePassword: async (req, res) => {
try {
// Deconstruct the request body
const { oldPassword, newPassword } = req.body

// Check if the old password is correct
const isCorrect = await bcrypt.compare(
oldPassword,
req.captain.password
)
if (!isCorrect) {
return res.status(400).json({
error: 'Old password is Invalid',
})
}

// Hash the new password
const hashedPassword = await bcrypt.hash(newPassword, 10)

// Update the password
const result = await db.query(
`UPDATE "Captain"
SET "password" = $1
WHERE "captainId" = $2
RETURNING *;`,
[hashedPassword, req.captain.captainId]
)

// Send the response
res.status(200).json({
message: 'Password updated successfully',
body: result.rows[0],
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occurred while updating the password',
})
}
},

// @desc Logout a captain
// @route POST /api/auth/logout
// @access Private
Expand Down
10 changes: 5 additions & 5 deletions server/middlewares/current.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const getCurrentTermMiddleware = async (req, res, next) => {

const getCurrentWeekMiddleware = async (req, res, next) => {
try {
const result = await db.query(
`SELECT * FROM "Week" WHERE "weekNumber" IN
(SELECT COALESCE(MAX("weekNumber"), 0) FROM "Week" WHERE "termNumber" IN
(SELECT COALESCE(MAX("termNumber"), 0) FROM "Term"));`
)

const result = await db.query('CALL get_current_week();')

console.log(result)

if (!result.rows.length) {
req.currentWeek = {
termNumber: 0,
Expand Down
1 change: 1 addition & 0 deletions server/routes/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const authRouter = Router()

authRouter.post('/signUp', authController.signup)
authRouter.post('/logIn', authController.login)
authRouter.post('/newPassword', authMiddleware, authController.updatePassword)
authRouter.post('/logOut', authMiddleware, authController.logout)
authRouter.get('/me', authMiddleware, authController.me)

Expand Down

0 comments on commit 5064a20

Please sign in to comment.