added ability to change password

This commit is contained in:
2025-01-31 17:53:27 +03:00
parent f5a89a59c7
commit 100b060aa7
5 changed files with 58 additions and 2 deletions

View File

@@ -15,7 +15,7 @@ class UserController {
if (password != passwordConfirm) return res.status(400).send("Passwords do not match");
let hashedPassword = await bcrypt.hash(password, 8);
const hashedPassword = await bcrypt.hash(password, 8);
await UserService.register(username, hashedPassword);
@@ -39,6 +39,24 @@ class UserController {
return res.redirect("/index");
}
async changePassword(req, res) {
const token = req.session.jwt;
const username = jwt.decode(token).username;
const { oldPassword, newPassword } = req.body;
const storedPassword = await UserService.getPassword(username);
if (!bcrypt.compareSync(oldPassword, storedPassword)) {
return res.status(403).send("Password is not correct");
}
const newHashedPassword = await bcrypt.hash(newPassword, 8);
await UserService.changePassword(username, newHashedPassword);
return res.status(200).send("Successful");
}
async logout(req, res) {
req.session.destroy();
return res.redirect("/login");