Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created October 21, 2022 08:06
Show Gist options
  • Save Octagon-simon/1225f1a30c992dad5e957b8af53b7cc7 to your computer and use it in GitHub Desktop.
Save Octagon-simon/1225f1a30c992dad5e957b8af53b7cc7 to your computer and use it in GitHub Desktop.

Revisions

  1. Octagon-simon created this gist Oct 21, 2022.
    28 changes: 28 additions & 0 deletions pass-reset-part-5.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    app.post('/reset', async (req, res) => {
    try{
    //find a document with such email address
    const user = await User.findOne({email : req.body.email})
    //check if user object is not empty
    if(user){
    //generate hash
    const hash = new User(user).generatePasswordResetHash()
    //generate a password reset link
    const resetLink = `http://localhost:5000/reset?email=${user.email}?&hash=${hash}`
    //return reset link
    return res.status(200).json({
    resetLink
    })
    //remember to send a mail to the user
    }else{
    //respond with an invalid email
    return res.status(400).json({
    message : "Email Address is invalid"
    })
    }
    }catch(err){
    console.log(err)
    return res.status(500).json({
    message : "Internal server error"
    })
    }
    })