Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created October 21, 2022 08:09
Show Gist options
  • Select an option

  • Save Octagon-simon/f225d2934cd3111cb21d73d9a32eb6ae to your computer and use it in GitHub Desktop.

Select an option

Save Octagon-simon/f225d2934cd3111cb21d73d9a32eb6ae to your computer and use it in GitHub Desktop.

Revisions

  1. Octagon-simon created this gist Oct 21, 2022.
    36 changes: 36 additions & 0 deletions pass-reset-part-6.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    //reset route
    app.get('/reset', async (req, res) => {
    try {
    //check for email and hash in query parameter
    if (req.query && req.query.email && req.query.hash) {
    //find user with suh email address
    const user = await User.findOne({ email: req.query.email })
    //check if user object is not empty
    if (user) {
    //now check if hash is valid
    if (new User(user).verifyPasswordResetHash(req.query.hash)) {
    //save email to session
    req.session.email = req.query.email;
    //issue a password reset form
    return res.sendFile(__dirname + '/views/new_pass.html')
    } else {
    return res.status(400).json({
    message: "You have provided an invalid reset link"
    })
    }
    } else {
    return res.status(400).json({
    message: "You have provided an invalid reset link"
    })
    }
    } else {
    //if there are no query parameters, serve the normal request form
    return res.sendFile(__dirname + '/views/reset.html')
    }
    } catch (err) {
    console.log(err)
    return res.status(500).json({
    message: "Internal server error"
    })
    }
    })