Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.changePassword = (req, res) => {
- const { token, password, confirmPassword } = req.body
- User.findOne({resetPasswordToken: token}, (err, user)=>{
- if(!user){
- return res.status(400).send({
- msg: 'Invalid token or token has been used!'
- })
- }
- const hash_password = bcrypt.hashSync(password, 10)
- User.findOneAndUpdate({_id: user._id},
- {hash_password},
- (err, result)=>{
- if(err){
- return res.status(400).send({
- msg: err
- })
- }
- User.findOneAndUpdate({_id: user._id},
- {resetPasswordToken: ''},
- (err, result)=>{
- if(err){
- return res.status(400).send({
- msg: err
- })
- }
- res.status(200).json({
- status: 1,
- data: 'Your password has been changed.'
- })
- }
- )
- })
- })
- }
- exports.changePassword = (req, res) => {
- const { token, password, confirmPassword } = req.body
- User.findOne({resetPasswordToken: token}).then((user)=>{
- // do stuff with user here
- const hash_password = bcrypt.hashSync(password, 10)
- // Now chain the next promise by returning it
- return User.findOneAndUpdate({_id: user._id}, {hash_password});
- }).then((result)=>{
- // Now you have the result from the next promise, carry on...
- res.status(200).json({
- status: 1,
- data: 'Your password has been changed.'
- })
- }).catch(err => {
- // Handle any errors caught along the way
- });
- }
- // Note this now has the async keyword to make it an async function
- exports.changePassword = async (req, res) => {
- const { token, password, confirmPassword } = req.body
- try {
- // Here is the await keyword
- const user = await User.findOne({resetPasswordToken: token});
- // do stuff with user here
- const hash_password = bcrypt.hashSync(password, 10)
- // Now the next promise can also be awaited
- const result = await User.findOneAndUpdate({_id: user._id}, {hash_password});
- // Finally send the status
- res.status(200).json({
- status: 1,
- data: 'Your password has been changed.'
- });
- } catch (err) {
- // Any promise rejections along the way will be caught here
- });
- }
Add Comment
Please, Sign In to add comment