I have this function that I was trying to debug because i was getting an error. I have since fixed the issue because I found someone with a similar problem, but being able to console log one of the parameters in the JwtStrategy would have really helped and given me a better understanding what was being passed through. I still can't seem to console it even though others have been able to using Postman and their source code.
So I have mongod and nodemon running in the back. and I this is the function.
module.exports = function(passport){
let opts = {}; //options is an object literal containing options to control how token is extracted.
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("jwt")
opts.secretOrKey = config.secret; //string containing the secret or pem encoded public key verifying the tokens signiture
passport.use(new JwtStrategy(opts, (jwt_payload,done)=>{
console.log(jwt_payload);
User.getUserById(jwt_payload._doc._id, (err, user)=>{
if(err){
return done(err,false);
}
if(user){
return done(null, user);
}else{
return done(null,done);
}
});
}));
}
As you see, I have a console.log(jwt_payload); This is what I put into Postman Post method, http://localhost:3000/users/authenticate, body:
{
"username":"bobdylan",
"password":"123456"
}
As I said above it is working and returning the correct JSON that I want. The only problem is that this jwt_payload isn't console logging in the terminal so I can see it passing through. I have this in the terminal.
Server started on port 3000
yay i am connected to databasemongodb://localhost:27017/authapp
So the "yay I am connected" is a console.log in the app.js. Why is this printing but not my console.log above? I would like to have this ability for future debugging issues.
This is additional code showing that it is actually working but the console log isnt working
{
"sucess": true,
"token": "JWTeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2M2VmMGM3MjM4NjA0MGUwYTgwZjEiLCJuYW1lIjoiYm9iIiwiZW1haWwiOiJib2JAZHlsYW4uY29tIiwidXNlcm5hbWUiOiJib2JkeWxhbiIsInBhc3N3b3JkIjoiJDJhJDEwJHprbFBVWjI5dlJMLjRJWnlOZ1N6amVaMk1mVGVzcWdRSHIuTEkzVjhmZTIwT1NxT2p0STdLIiwiX192IjowLCJpYXQiOjE1MTk3OTk0NTcsImV4cCI6MTUyMDM5OTQ1N30.DlwzJuQayQ1zc4p8Gy5PGaE22N8ekyrBrmOhVPMkJ6Y",
"user": {
"id": "5a963ef0c72386040e0a80f1",
"name": "bob",
"username": "bobdylan",
"email": "[email protected]"
}
}
So that is the response after the request.
I have this function that I was trying to debug because i was getting an error. I have since fixed the issue because I found someone with a similar problem, but being able to console log one of the parameters in the JwtStrategy would have really helped and given me a better understanding what was being passed through. I still can't seem to console it even though others have been able to using Postman and their source code.
So I have mongod and nodemon running in the back. and I this is the function.
module.exports = function(passport){
let opts = {}; //options is an object literal containing options to control how token is extracted.
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("jwt")
opts.secretOrKey = config.secret; //string containing the secret or pem encoded public key verifying the tokens signiture
passport.use(new JwtStrategy(opts, (jwt_payload,done)=>{
console.log(jwt_payload);
User.getUserById(jwt_payload._doc._id, (err, user)=>{
if(err){
return done(err,false);
}
if(user){
return done(null, user);
}else{
return done(null,done);
}
});
}));
}
As you see, I have a console.log(jwt_payload); This is what I put into Postman Post method, http://localhost:3000/users/authenticate, body:
{
"username":"bobdylan",
"password":"123456"
}
As I said above it is working and returning the correct JSON that I want. The only problem is that this jwt_payload isn't console logging in the terminal so I can see it passing through. I have this in the terminal.
Server started on port 3000
yay i am connected to databasemongodb://localhost:27017/authapp
So the "yay I am connected" is a console.log in the app.js. Why is this printing but not my console.log above? I would like to have this ability for future debugging issues.
This is additional code showing that it is actually working but the console log isnt working
{
"sucess": true,
"token": "JWTeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2M2VmMGM3MjM4NjA0MGUwYTgwZjEiLCJuYW1lIjoiYm9iIiwiZW1haWwiOiJib2JAZHlsYW4uY29tIiwidXNlcm5hbWUiOiJib2JkeWxhbiIsInBhc3N3b3JkIjoiJDJhJDEwJHprbFBVWjI5dlJMLjRJWnlOZ1N6amVaMk1mVGVzcWdRSHIuTEkzVjhmZTIwT1NxT2p0STdLIiwiX192IjowLCJpYXQiOjE1MTk3OTk0NTcsImV4cCI6MTUyMDM5OTQ1N30.DlwzJuQayQ1zc4p8Gy5PGaE22N8ekyrBrmOhVPMkJ6Y",
"user": {
"id": "5a963ef0c72386040e0a80f1",
"name": "bob",
"username": "bobdylan",
"email": "[email protected]"
}
}
So that is the response after the request.
Share Improve this question edited Feb 28, 2018 at 13:32 Jonathan Schroeder asked Feb 28, 2018 at 6:13 Jonathan SchroederJonathan Schroeder 851 gold badge2 silver badges10 bronze badges 6 | Show 1 more comment3 Answers
Reset to default 15For anyone who is struggling with output of console.log
, console.info
, etc in Postman
You can view postman console on menu View >> Show Postman Console
not on menu View >> Developer >> Show DevTools
changed user.toJSON() to {data:user}. Finally it started printing to the console and found out the payload was coming in through an object called data. Because of this I put jwt_payload.data._id instead of what I had above. I think the npm documentation has changed with updates. I have another Mean app using passport and I used jwt_payload._doc._id which worked before. Also I don't think I had to put {data:user}. I think before I just had user there. Im surprised the registration and authentication worked with user.toJSON() but didn't work with going to the profile page. I really was about to give up but thankfully I tried again and again.
Change opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("jwt")
to
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('JWT')
JWT in capitals
console.log('this is a test')
at the same location? – Randy Casburn Commented Feb 28, 2018 at 6:21User.getUserById(...)
works correctly? If so, that is very perplexing. – Randy Casburn Commented Feb 28, 2018 at 6:41