Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Example with passport-jwt requested #26

Open
jovermier opened this issue Jan 31, 2020 · 5 comments
Open

doc: Example with passport-jwt requested #26

jovermier opened this issue Jan 31, 2020 · 5 comments
Labels
accepting help Issues open to PRs or contributions by others documentation An issue or PR primarily about updating documentation

Comments

@jovermier
Copy link

I have been trying to figure out how to use graphql-passport with passport-local and passport-jwt for a couple days now. The GraphQLLocalStrategy built into graphql-passport is working. I have not been able to figure out how to authenticate without sessions and just use jwt. An example would be very helpful.

Thanks in advance for any help you can give :)

@jkettmann
Copy link
Collaborator

Sorry for answering this late. I never used passport-jwt. Did you figure this out already? Otherwise, I'll try to have a look at it in the next days

@JipSterk
Copy link

JipSterk commented Jan 3, 2021

@jovermier @jkettmann did either of you have a solution for this issue?

@danwetherald
Copy link

danwetherald commented Jan 4, 2021

Would love to see an update with this as well.

We are looking to use graphql-passport to build the context so that the logged in user is included in the context, but we would like to use local passport email/password strategies when authenticating, but later on we would like to use passport-jwt to authenticate requests from a JWT Bearer Authorization Header.

@jkettmann
Copy link
Collaborator

In one of my projects I now use passport-jwt but not together with graphql-passport. However, you should be able to simply use the JWTStrategy the usual way you would with passport. My setup looks something like this:

const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt')

passport.use(new JwtStrategy({
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey,
  issuer,
  audience
},
(jwtPayload, done) => {
  User
    .get(jwtPayload.id)
    .then((user) => {
      if (!user) {
        done('User does not exist')
      } else {
        done(null, user)
      }
    })
    .catch((error) => done(error))
}))

Then you could just use the buildContext function of graphql-passport and use it to copy the user and other passport functions into the GraphQL context like it's described in the readme.

context: ({ req, res }) => buildContext({ req, res })

Note that I didn't test this, but I think it should work fine. I'll try to add an example when I find the time again 😓

@jkettmann jkettmann added the question Issues that are just requesting information label Jan 6, 2021
@johannbuscail
Copy link

Hi, could you add the example to the doc please

@ericbf ericbf added accepting help Issues open to PRs or contributions by others documentation An issue or PR primarily about updating documentation and removed question Issues that are just requesting information labels Dec 21, 2022
@ericbf ericbf changed the title Example with passport-jwt requested doc: Example with passport-jwt requested Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting help Issues open to PRs or contributions by others documentation An issue or PR primarily about updating documentation
Projects
None yet
Development

No branches or pull requests

6 participants