Skip to content

Commit 6018e6d

Browse files
committed
add route to verify password and change password
1 parent a955ed8 commit 6018e6d

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

peerprep-fe/src/app/profile/page.tsx

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,38 @@ const ProfilePage = () => {
109109
}
110110

111111
try {
112+
const verifyResult = await axiosClient.post(
113+
`/auth/verify-password/${user.id}`,
114+
{
115+
password: passwordData.currentPassword,
116+
},
117+
);
118+
112119
const result = await axiosClient.patch(`/users/${user.id}`, {
113120
password: passwordData.newPassword,
114121
});
115122

116-
if (result.status === 200) {
117-
setPasswordMessage({
118-
type: 'success',
119-
text: 'Password updated successfully',
120-
});
123+
// throw error if result is not 200
124+
if (result.status !== 200) {
125+
throw new Error(result.data.message);
126+
}
121127

122-
// reset the fields
123-
setPasswordData({
124-
currentPassword: '',
125-
newPassword: '',
126-
confirmPassword: '',
127-
});
128+
setPasswordMessage({
129+
type: 'success',
130+
text: 'Password updated successfully',
131+
});
128132

129-
// Clear message after 5 seconds
130-
setTimeout(() => {
131-
setPasswordMessage(null);
132-
}, 5000);
133-
}
133+
// reset the fields
134+
setPasswordData({
135+
currentPassword: '',
136+
newPassword: '',
137+
confirmPassword: '',
138+
});
139+
140+
// Clear message after 5 seconds
141+
setTimeout(() => {
142+
setPasswordMessage(null);
143+
}, 5000);
134144
} catch (error: unknown) {
135145
const message =
136146
(error as { response?: { data?: { message?: string } } })?.response

user-service/controller/auth-controller.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import bcrypt from 'bcrypt';
22
import jwt from 'jsonwebtoken';
33
import { findUserByEmail as _findUserByEmail } from '../model/repository.js';
4+
import { findUserById as _findUserById } from '../model/repository.js';
45
import { formatUserResponse } from './user-controller.js';
56

67
export async function handleLogin(req, res) {
@@ -51,3 +52,44 @@ export async function handleVerifyToken(req, res) {
5152
return res.status(500).json({ message: err.message });
5253
}
5354
}
55+
56+
/**
57+
* Controller function to verify if the password is correct
58+
* @param {} req
59+
* @param {*} res
60+
* @returns 200 if password is correct, 400 if missing id and/or password, 401 if user not found, 500 if error
61+
*/
62+
export async function verifyPassword(req, res) {
63+
const { id } = req.params;
64+
const { password } = req.body;
65+
const verifiedUser = req.user;
66+
67+
console.log(id, password, verifiedUser);
68+
69+
if (!id) {
70+
return res.status(400).json({ message: 'Missing id' });
71+
}
72+
73+
if (!password) {
74+
return res.status(400).json({ message: 'Missing password' });
75+
}
76+
77+
// Only the owner of the account can verify the password
78+
if (id !== verifiedUser.id) {
79+
return res.status(401).json({ message: 'Unauthorized' });
80+
}
81+
82+
// Getting the password from the database
83+
const user = await _findUserById(id);
84+
85+
try {
86+
const match = await bcrypt.compare(password, user.password);
87+
if (!match) {
88+
return res.status(401).json({ message: 'Wrong password' });
89+
}
90+
91+
return res.status(200).json({ message: 'Password verified' });
92+
} catch (err) {
93+
return res.status(500).json({ message: err.message });
94+
}
95+
}

user-service/routes/auth-routes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express';
22
import {
33
handleLogin,
44
handleVerifyToken,
5+
verifyPassword,
56
} from '../controller/auth-controller.js';
67
import { handleGithubCallback } from '../controller/oauth-controller.js';
78
import { verifyAccessToken } from '../middleware/basic-access-control.js';
@@ -14,4 +15,6 @@ router.get('/verify-token', verifyAccessToken, handleVerifyToken);
1415

1516
router.get('/github/callback', handleGithubCallback);
1617

18+
router.post('/verify-password/:id', verifyAccessToken, verifyPassword);
19+
1720
export default router;

0 commit comments

Comments
 (0)