Open Harshal96 opened 3 years ago
same problem!
Yeah this one took me a while too... the callback (in OP defined as (jwt_payload, done)
must not be asynchronous in any way.
Change that to (jwt_payload, done) => done(null, jwt_payload)
.
Then, the authenticate
function needs to be wrapped:
const authenticateWithJwt = (req, res, next) => {
passport.authenticate('jwt', {session: false}, (error, jwt_payload) => {
if (error) {
return next(error);
}
User.findOne({id: jwt_payload.sub}, (err, user) => {
if (err || !user) {
return next(err || new Error('Could not find user'));
}
next(user);
});
})(req, res);
};
app.get('/protected', authenticateWithJwt, (req, res) => {
res.status(200).json({message: 'it works!'});
});
Strategy:
JWT signing:
Authentication:
When running with Postman, I selected Authorization as "bearer token" and simply pasted the token in the box. I also tried sending it as a query parameter and in the request body.
Nothing works. There is no error, it just doesn't give any response.