Answer the question
In order to leave comments, you need to log in
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)
}
})
sub
is the payload value. I want to compare it with the value _id
in the database. If it matches, I give it a route...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question