1

I am trying to get the token value from the following URL http://localhost:3000/users/reset/e3b40d3e3550b35bc916a361d8487aefa30147c8. I have a get request that checks if the token is valid and redirects the user to a reset password screen. I also have a post request but when I console req.params.token, it outputs :token instead of e3b40d3e3550b35bc916a361d8487aefa30147c8. I am wondering if the form action is correct but don't know how to get the token value from it.

Reset Password Get Request

router.get('/reset/:token', (req, res) => {
  console.log(req.params.token) // e3b40d3e3550b35bc916a361d8487aefa30147c8
  User.findOne({
    resetPasswordToken: req.params.token,
    resetPasswordExpires: {
      $gt: Date.now() 
    }
  }, (err, user) => {
    if (!user) {
      req.flash('error_msg', 'The password reset token is invalid or has expired.')
      return res.redirect('/users/forgot')
    }
    res.render('reset')
  })
})

reset.ejs

<% include ./partials/messages %>
<form action="/users/reset/:token" method="POST">
    <div class="form-group">
    <label for="password">Password</label>
    <input type="password" id="password" name="password" class="form-control" placeholder="Please enter a password."
        value="<%= typeof password != 'undefined' ? password : '' %>" />
    </div>
    <button type="submit" class="btn btn-primary btn-block">Register</button>
</form>

Reset Password Post Request

router.post('/reset/:token', (req, res) => {
  console.log(req.params.token) // :token
  User.findOne({
    resetPasswordToken: req.params.token,
    resetPasswordExpires: {
      $gt: Date.now() 
    }
  }, (err, user) => {
    if (!user) {
      req.flash('error_msg', 'The password reset token is invalid or has expired.')
      return res.redirect('/users/forgot')
    }

    user.password = req.body.password;
    user.resetPasswordToken = undefined;
    user.resetPasswordExpires = undefined;

    user.save(function (err) {
      req.flash('success_msg', 'Working.')
      return res.redirect('/users/login')
    })
  })
})
8
  • What are you expecting action="/users/reset/:token" to do in your form post url? It's going to literally just send that as the URL with :token in the URL (well the : will be encoded, but you get the idea). Commented Feb 3, 2019 at 3:31
  • @jfriend00 From the http://localhost:3000/users/reset/e3b40d3e3550b35bc916a361d8487aefa30147c8, I am trying to get e3b40d3e3550b35bc916a361d8487aefa30147c8 from the string Commented Feb 3, 2019 at 3:33
  • But, that's NOT the URL for the form post which is where you said the problem was. It already works for the GET URL because the URL is correct. That's the point of my comment about the form post URL. Your form POST has :token in the URL, not the e3b40d3e3550b35bc916a361d8487aefa30147c8 you want. Commented Feb 3, 2019 at 3:34
  • @jfriend00 Yes, correct, but this token changes and expires after 60 minutes so I can't hard code the value in. I have to get it some way else. Using req.params.token works with the get request but not the post request Commented Feb 3, 2019 at 3:40
  • If you want it to be in the form post URL, you HAVE to put the token in the form post URL in your page. There is no other way for it to magically appear in the URL. You're putting it IN the URL for the GET, but not the POST> Depending upon what you're really doing here, there are other places to put the token such as in a form field, in the server-side session object for that user, etc... Commented Feb 3, 2019 at 3:42

2 Answers 2

2

In your form in your HTML, you have this:

<form action="/users/reset/:token" method="POST">

That's going to make the actual URL that gets requested when the form is posted be:

/users/reset/:token

There's no code doing any substitution for the :token here. That's just getting sent directly to the server as the URL.

So, when you then have:

router.post('/reset/:token', (req, res) => {
    console.log(req.url);            // "/user/reset/:token"
    console.log(req.params.token);   // ":token"
});

What req.params.token is showing you is whatever is in the URL that's after /users/reset. In your case, that is the literal string ":token". For req.params.token to actually have to token in it, you would have to insert the actual token into the URL so your form tag looks like this:

<form action="/users/reset/e3b40d3e3550b35bc916a361d8487aefa30147c8" method="POST">

Or, you will have to get access to the token some other way such as from the express session, from a cookie, from a field in the form, etc...

Sign up to request clarification or add additional context in comments.

Comments

1

To get a URL parameter's value

app.get('/reset/:token', function(req, res) {
    res.send("token is " + req.params.token);
});

To get a query parameter ?token=Adhgd5645

app.get('/reset/?token=Adhgd5645', function(req, res) {
    res.send("token is " + req.query.token);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.