H
H
heducose2017-07-21 22:00:06
Node.js
heducose, 2017-07-21 22:00:06

How to make a check before giving the route?

I want to allow only those users who have validated the JWT token along a certain path, but I don’t understand how to combine validation, a request to the Mongo database and routing?
JWT validation is done by the express-jwt plugin jwt({ secret: process.env.SECRET }). After that it gives the contents of the payload to req.user which I can use.
After that, I need to get into the database like this:

User.findById(req.user.sub, function (err, user) {
      if (err) { return done (err, false) }
      if (user) {
        done (null, user)
      } else {
        done (null, false)
      }
    })

subis the payload value. I want to compare it with the value _idin the database. If it matches, I give it a route...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-07-21
@RidgeA

expressjs.com/en/guide/writing-middleware.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question