bradtraversy / meanauthapp

Complete MEAN stack app with authentication
242 stars 152 forks source link

getting unauthorized in passport.js #34

Closed Vinay-Shankar closed 6 years ago

Vinay-Shankar commented 6 years ago

hi.... i am trying to learn MEAN stack web app development getting unauthorized in passport.js here code is: module.exports= function(passport){ let opts = {}; opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); opts.secretOrKey = config.secret; passport.use(new JwtStrategy(opts, function(jwt_payload, done) { console.log('payload received', jwt_payload); User.getUserById(jwt_payload.sub, (err, user) => { if(err){ return done(err, false); } if(user){ return done(null, user); } else { return done(null, false); } }); })); }

in above code,....i have changed code as per the version ,even i can't able see payload and its not accessing from below this code.

passport.use(new JwtStrategy(opts, function(jwt_payload, done) { console.log('payload received', jwt_payload); User.getUserById(jwt_payload.sub, (err, user) => { if(err){ return done(err, false); } if(user){ return done(null, user); } else { return done(null, false); } }); }));

if anyone gets answer pls.....help me

tomcatbuzz commented 6 years ago

@Vinay-Shankar your code is wrong. In the Config folder you need to compare your code if you are doing the video to the Updated Passport.js file.


const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config/database');

module.exports = function(passport) {
  let opts = {};
  opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
  opts.secretOrKey = config.secret;
  passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
    User.getUserById(jwt_payload.data._id, (err, user) => {
      if(err) {
        return done(err, false);
      }

      if(user) {
        return done(null, user);
      } else {
        return done(null, false);
      }
    });
  }));
}```
Vinay-Shankar commented 6 years ago

@tomcatbuzz ...hi sir i have compared the code...but no changes as per thought....if i give console.log('check') before passport.use() function.....it works upto console.log('check'); and it nnot mores to the next step that is passport.use() function. even the jwt-payload details are not shown code : `const JwtStrategy = require('passport-jwt').Strategy; const ExtractJwt = require('passport-jwt').ExtractJwt; const User = require('../models/user'); const config = require('../config/database');

module.exports= function(passport){ let opts = {}; opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); opts.secretOrKey = config.secret; console.log('check'); passport.use(new JwtStrategy(opts, function( jwt_payload, done) { console.log('payload received', jwt_payload); User.getUserById(jwt_payload._data._id, (err, user) => { if(err){ return done(err, false); } if(user){ return done(null, user); } else { return done(null, false); } }); })); }`

please help me...sir i am interested in MEAN stack

tomcatbuzz commented 6 years ago

I would suggest you clone the Repo and follow the directions for Both the Front End Angular and the Backend Nodejs-express. Then try to re-run your application. You are not showing any Error information. All you are doing is saying you don't get any console.log return. I have no clue what you are doing. Are you following Brad's Video series? If so what video are you are on? Give more info and I will try to help.

Vinay-Shankar commented 6 years ago

@tomcatbuzz.... it's getting unauthorized in authenicate... if i give console.log(jwt_payload); inside passport.use(new JwtStrategy(opts, function( jwt_payload, done) ...it should display,what payload is giving. yes...i am following brad traversy the video is mean authenicate app tutorial in youtube

tomcatbuzz commented 6 years ago

@Vinay-Shankar is it the video series from Feb 13, 2017. If so what video are you on so I can review the video and try to see where you are getting stuck. Sometimes it is good to go back over a previous video to make sure you didn't miss any steps in the process.

Vinay-Shankar commented 6 years ago

@tomcatbuzz .... yes.....video series published in 13 feb 2017... i am stuck in API authenication that's in video 4.... getting unauthorized in users/profile.

tomcatbuzz commented 6 years ago

Are you Copying the JWT key and using it in POSTMAN like shown at minute 19 of the video? You will get unauthorized until you paste the key in and Select Authorize in POSTMAN On Sunday, March 25, 2018, 12:52:40 PM EDT, Vinay Shankar notifications@github.com wrote:

@tomcatbuzz .... yes.....video series published in 13 feb 2017... i am stuck in API authenication that's in video 4.... getting unauthorized

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub, or mute the thread.

Vinay-Shankar commented 6 years ago

@tomcatbuzz ...yes i have given JWT key and Authorization in postman

tomcatbuzz commented 6 years ago

You should join this discord server so others can help you with your code also. It is an open invitation server now and I am inviting you to join. You will be able to post your code for review to see if there any errors. Brad originally created this discord server. Discord - Free voice and text chat for gamers

|

Discord - Free voice and text chat for gamers

Step up your game with a modern voice & text chat app. Crystal clear voice, multiple server and channel support,... |

|

|

On Tuesday, March 27, 2018, 12:47:24 PM EDT, Vinay Shankar <notifications@github.com> wrote:  

@tomcatbuzz ...yes i have given JWT key and Authorization in postman

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub, or mute the thread.

Vinay-Shankar commented 6 years ago

@tomcatbuzz unauthorized in authenicate issue is solved jwt_payload is giving in the form of data i.e...code: payload { data: { _id: '5ac6222d0d6b1f41c8ed386b', name: 'abc', email: 'abcd@gmail.com', username: 'abcd18', password: '$2a$10$Us1XSSg9o9OfGrYhrotE8.3wwreUnDSjPLm4zH4Li0cYiRTpzXM7O', __v: 0 }, iat: 1522937438, exp: 1522937738 } so.....please change code in passport.js const JwtStrategy = require('passport-jwt').Strategy; const ExtractJwt = require('passport-jwt').ExtractJwt; const User = require('../models/user'); const config = require('../config/database');

module.exports = function(passport) { let opts = {}; opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt'); opts.secretOrKey = config.secret; passport.use(new JwtStrategy(opts, (jwt_payload, done) => { console.log('payload received', jwt_payload); User.getUserById(jwt_payload.data._id, (err, user) => { if(err) { return done(err, false); }

  if(user) {
    return done(null, user);
  } else {
    return done(null, false);
  }
});

})); }

thanks for helping and guiding me...

Vinay-Shankar commented 6 years ago

i will close the issue of unauthorized in authenicate....