mikenicholson / passport-jwt

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

Wrapping authenticate method #141

Closed kadosh1000 closed 6 years ago

kadosh1000 commented 6 years ago

Hi, I am trying to wrap the authenticate method so I can always check for user. I have tried the following

image

But it never gets to the console.log and instead directly responses with 401. Is there a way to prevent it and simply return the callback so I can define the desired action?

EddyTheDove commented 6 years ago

Hello, try like this

app.use((req, res, next) => {
    passport.authenticate('jwt', {session: false}, (err, user, info) => {
        if (!user) {`
            console.log('no user found in JWT', info)
        }
        next()
    })(req, res, next) // You must put this;
})
kadosh1000 commented 6 years ago

@EddyTheDove Thanks I have used it that way pretty much after posting this issue and forgot to comment and close it.

Thanks anyway!