mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.96k stars 213 forks source link

Login and Registration works but Profile still gets 401 Unauthorized #209

Closed andreashp96 closed 4 years ago

andreashp96 commented 4 years ago

I have a difficulty on authentication. User login and registration work perfectly, but not for the authentication.

Here's the passport.js:

const JwtStrategy = require('passport-jwt').Strategy; const ExtractJwt = require('passport-jwt').ExtractJwt; const User = require('../model/User'); const key = require('./keys');

module.exports = (passport) => { let opts = {}; opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt'); opts.secretOrKey = key.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); return done(null, false); }) } ) ); }

and here's the users.js: const express = require('express'); const router = express.Router(); const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const passport = require('passport'); const User = require('../../model/User'); const key = require('../../config/keys').secret;

/**

});

/**

/**

module.exports = router;

Any idea why it's 401 unauthorized? Thank you!

mikenicholson commented 4 years ago

Unfortunately, I'm not able to troubleshoot other peoples code. If you discover a reproducible issue with this module please open an issue with a unit test, succinct code snippet or detailed instructions to reproduce.

Consider asking a question on stack overflow. Best of luck.

alamenai commented 3 years ago

@andreashp96 , did you find the solution?