Ralph Mungcal
Ralph Mungcal

Reputation: 117

Passport Jwt Unauthorized

Hello I was trying to implement passport-jwt on my webapp. But It wont work I've seen several same post and tried what they did but on my case it didnt worked.

here are some Authenticating node API with passport-jwt

passport local strategy not getting called

Passport-jwt Unauthorized

I've tried those and it didnt worked. Here is my code on my passport.js

    var config = require('./config');

var JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
module.exports =
    function(passport){
        var opts ={};
        opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
        opts.secretOrKey = config.secret;
            passport.use(new JwtStrategy(opts,function(jwt_payload,done){
                console.log('HERE');
                console.log(jwt_payload);
                User.findOne({id:jwt_payload._id},function(err,user){
                    if(err)
                        return done(err,false);
                    if(user)
                        return done(null,user);
                    else
                        return done(null,false);
                        })

                    }));
            };

It wont even reach the console.log('HERE');

and this is how I generate my token

router.post('/login',function(req,res,next){
    var username = req.body.username;
    var password = req.body.password;
    User.findUser(username,function(err,user){
        if(err) return res.json({success:false,msg:"Error"});
        if(!user) return res.json({success:false,msg:"No Such user"});
        //res.json(user);
        console.log(user);
        if(user){
            User.comparePass(password,user.password,function(err,IsMatched){
                if(err) return res.json({success:false,msg:'error});
                if(!IsMatched) return  res.json({success:false,msg:"wrong password"});
                if(IsMatched){
                    var token = jwt.sign(user.toObject(),config.secret,{expiresIn:604800});
                    res.json({success:true,token:'JWT '+ token,
                            user:{
                                name:user.name,
                                username:user.username,
                                password:user.password,
                                email:user.email,
                                contact:user.contact,
                                address:user.address
                            }
                    });

                }
            });
        }
    });
});

and here is what im trying to access

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

Thanks!

Upvotes: 4

Views: 1286

Answers (1)

Ralph Mungcal
Ralph Mungcal

Reputation: 117

Ok I've figured it out.

ExtractJwt.fromAuthHeaderWithScheme('jwt')

instead of

ExtractJwt.fromAuthHeaderAsBearerToken();

Upvotes: 2

Related Questions