Seth Spivey
Seth Spivey

Reputation: 367

Password Reset Auth using MERN Stack

I'm implementing an auth token within the nodemailer email that is sent whenever a user resets their password. I've got the generated url with the appended token setup and sending properly, I just can't seem to get my routing right to handle the authentication of said token.

My request is returning a 404 saying my GET is not found (GET http://localhost:3000/reset/XXXXXXXXXXXX 404 (Not Found) ). Could anyone help point me in the right direction here?

Here's my api route for the server

router.get('/reset', (req, res, next) => {
   User.findOne({ resetPasswordToken: req.query.resetPasswordToken }).then(user => {
      if (user == null) {
         res.json('Password link is invalid or has expired');
      } else {
         res.status(200).send({
           username: user.email,
           message: 'Password link accepted',
         })
     }
   })
});

And my code to handle the request

componentDidMount() {

  console.log(this.props.match.params.token);

  axios.get('/reset' + this.props.match.params.token)
    .then(response => {
      // Conditional logic here based on response returned
    })
    .catch(function (error) {
      console.log(error);
    })
}

Upvotes: 1

Views: 2539

Answers (1)

chrispytoes
chrispytoes

Reputation: 1897

You need to tell express to expect the parameter in the url like this:

router.get('/reset/:token', (req, res, next) => {
   // token is inside req.params.token
   User.findOne({ resetPasswordToken: req.params.token }).then(user => {
      if (user == null) {
         res.json('Password link is invalid or has expired');
      } else {
         res.status(200).send({
           username: user.email,
           message: 'Password link accepted',
         })
     }
   })
});

Then make sure there is a slash in the url when you make the request:

  axios.get('/reset/' + this.props.match.params.token)
    .then(response => {
      // Conditional logic here based on response returned
    })
    .catch(function (error) {
      console.log(error);
    })

Upvotes: 2

Related Questions