I am currently learning to make API, so the last part I need to learn is JWT token verification to authorise routes. My authentication is working fine, but I need to pass the response user's data with it.
The code which I have tried has some bug that I cannot understand, but I think I messed up with async await part.
When I am calling User.findById(jwt_payload.user._id) I must get the user's data, currently I am getting null. I am stuck on that part with no clue to solve it.
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const { User } = require('../models/user');
const config = require('../config/database');
module.exports = async function(passport){
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts, async function(jwt_payload, done){
//console.log(jwt_payload.user._id);
const user = await User.findById(jwt_payload.user._id);
//console.log(user);
if(user){
return done(null,user);
}
else{
return done(null,false);
}
}));
}
I would appreciate if someone helps me understand the part of error I have made