mikenicholson / passport-jwt

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

not response #123

Closed heinhoang closed 7 years ago

heinhoang commented 7 years ago

Passport jwt config

const jwtOpts = {
    jwtFromRequest: ExtractJwt.fromAuthHeader('authorization'),
    secretOrKey: process.env.JWT_SECRET,
};

const jwtLogin = new JWTStrategy(jwtOpts, (payload, done) => {
    User.findOne({ id: payload.sub }, (err, user) => {
        if (err) {
            return done(err, false, { msg: 'meet error', error: err });
        }
        if (!user) {
            return done(null, false, { msg: 'there\'s no user associated with this token' });
        }
        return done(null, user);
    });
});

Using

const jwtAuthenticate = () => passport.authenticate('jwt', { session: false });
const ctr = (req, res, next) => {
    res.status(HTTPStatus.OK).json(req.user);

    return next();
};
router.get('/secret', jwtAuthenticate, ctr);

Problem Testing using Postman: I register a user, get token from that user and fill in Authorization in Headers (using postman). It's loading too long and then inform 'can't get any response'. Note: If I remove jwtAuthenticate middleware, it works fine..

heinhoang commented 7 years ago

if I place jwtAuthenticate() in ctr function, it works fine. But why use jwtAuthenticate() as middleware, it's not working???

heinhoang commented 7 years ago

I don't know why but it's working now. I will reopen if meet the same problem.

mikenicholson commented 7 years ago

Odd. There hasn't been a new release in this time period and I wasn't able to reproduce the behavior.