Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
fix(server): fix reset password route
Browse files Browse the repository at this point in the history
  • Loading branch information
elanum committed Dec 17, 2020
1 parent aa326f6 commit 03e6e47
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions server/src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ router

const correctPassword = bcrypt.compare(password, user.password);

if (!correctPassword) return res.status(400).json({ message: 'Password is incorrect' });
if (!correctPassword) { return res.status(400).json({ message: 'Password is incorrect' }); }

const token = jwt.sign({ id: user._id, username: user.password }, secret, {
expiresIn,
Expand All @@ -39,7 +39,7 @@ router
} catch (error) {
return res.status(400).json({ message: error.message });
}

delete user._doc.password;
return res.status(201).json(user);
})
.all((_req, res) => res.status(405).json({ message: 'Method Not Allowed' }));
Expand All @@ -51,15 +51,17 @@ router
async (req, res) => {
const { username, oldPassword, newPassword } = req.body;

if (!username) return res.status(400).json({ message: 'Username is missing' });
if (!oldPassword) return res.status(400).json({ message: 'Old Password is missing' });
if (!newPassword) return res.status(400).json({ message: 'New Password is missing' });
if (!username) { return res.status(400).json({ message: 'Username is missing' }); }
if (!oldPassword) { return res.status(400).json({ message: 'Old Password is missing' }); }
if (!newPassword) { return res.status(400).json({ message: 'New Password is missing' }); }

const user = await User.findOne({ username }).select('+password');

if (!user) return res.status(404).json({ message: 'User not found' });
const passwordMatch = await bcrypt.compare(oldPassword, user.password);

if (!passwordMatch) { return res.status(400).json({ message: 'Old password incorrect' }); }

const passwordMatch = bcrypt.compare(oldPassword, user.password);
if (!passwordMatch) return res.status(400).json({ message: 'Old password incorrect' });
user.password = newPassword;
try {
await user.save();
Expand All @@ -72,4 +74,4 @@ router
)
.all((_req, res) => res.status(405).json({ message: 'Method Not Allowed' }));

export default router;
module.exports = router;

0 comments on commit 03e6e47

Please # to comment.