generated from hatchways/express-starter
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from hatchways/FS-Settings-Page-79
Fs settings page 79
- Loading branch information
Showing
5 changed files
with
233 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { GeneralError, NotFound, Unauthorized } = require('../utils/errors'); | ||
const bcrypt = require('bcrypt'); | ||
const { validationResult } = require('express-validator'); | ||
const User = require('../models/user'); | ||
|
||
exports.passwordChange = async (req, res, next) => { | ||
const errors = validationResult(req); | ||
|
||
if (!errors.isEmpty()) | ||
return res.status(400).json({ | ||
error: errors.array(), | ||
}); | ||
|
||
const { oldPassword, newPassword, userId } = req.body; | ||
|
||
try { | ||
const user = await User.findById(userId); | ||
if (!user) throw new NotFound('No user found'); | ||
|
||
const comparePassword = await bcrypt.compare( | ||
oldPassword, | ||
user.password | ||
); | ||
if (!comparePassword) | ||
throw new Unauthorized('Unauthorized to change password'); | ||
|
||
const newHashedPw = await bcrypt.hash(newPassword, 10); | ||
if (!newHashedPw) throw new GeneralError('Failed to hash password'); | ||
|
||
user.password = newHashedPw; | ||
|
||
const response = await user.save(); | ||
if (!response) throw new GeneralError('Failed saving user'); | ||
|
||
res.status(200).json({ message: 'Password updated successfully' }); | ||
} catch (err) { | ||
console.log(err.message); | ||
next(err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters