mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.97k stars 214 forks source link

passport.authenticate doesnt work #116

Closed Quichei closed 7 years ago

Quichei commented 7 years ago

Im using this : app.use(passport.initialize()); app.use(passport.session()); require('./config/passport')(passport);

and /config/passport.js is : `var JwtStrategy = require('passport-jwt').Strategy; var ExtractJwt = require('passport-jwt').ExtractJwt; const UserController = require('../controllers/userController'); const config = require('./database');

module.exports = function(passport){ let opts = {}; opts.jwtFromRequest = ExtractJwt.fromAuthHeader(); opts.secretOrKey = config.secret; passport.use(new JwtStrategy(opts,(jwt_payload, done) => { console.log(jwt_payload); //wont log anything UserController.findById(jwt_payload._doc._id, (err, user) => {

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

}));`

users.js route: router.get('/profile',passport.authenticate('jwt', { session: false }), (req, res, next) => { console.log('req'); res.json({user: req.user}); });

I can log payload. it doesnt even go in strategy to log it...

Quichei commented 7 years ago

when I signed the token I forgott to concatenate "JWT "(space is very important) with the token

munapadhi commented 6 years ago

kindly help me how can solve

const jwtStrategy = require('passport-jwt').Strategy; const ExtractJwt = require('passport-jwt').ExtractJwt;

const User = require('../models/user-model'); const config = require('../config/database');

module.exports = function(passport){

var opts = {}
opts.jwtFromRequest = ExtractJwt.fromHeader();
opts.secretOrKey = config.secret;
console.log(ExtractJwt);
passport.use(new jwtStrategy(opts, (jwt_payload, done) => {
  console.log('comeing',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, false);
        }
    });
}));

}

user.js

User.comparePassword(password, user.password, (err, isMatch) => { if(err) throw err;

        if(isMatch){
            const token = jwt.sign({data: user}, config.secret, {
                expiresIn: 604800 // 1 week
              });

            res.json({
                success: true, 
                token: 'JWT '+token,
                user: {
                id: user._id,
                name: user.name,
                username: user.username,
                email: user.email
            }})
        } else{
            return res.json({success: false, msg: 'wrong password'});
        }
    })

routes.js

router.get('/profile', passport.authenticate('jwt', {session:false}),(req,res,next) => { res.json({user:req.user}); });

last five days i have continues work but still not resolve kindly any body help me.

munapadhi commented 6 years ago

i cant see any console.log('comeing',jwt_payload);