graphql-passport | use passport.js with GraphQL server | Authentication library
kandi X-RAY | graphql-passport Summary
kandi X-RAY | graphql-passport Summary
graphql-passport provides simple functionality to authenticate with Passport.js from mutation resolvers. Inside your resolvers you can get access to the following functions and attributes inside the context. authenticate and login are basically passport.authenticate and passport.login wrapped in a promise. user, logout, isAuthenticated and isUnauthenticated are just copies of the corresponding passport functions and attributes.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of graphql-passport
graphql-passport Key Features
graphql-passport Examples and Code Snippets
Community Discussions
Trending Discussions on graphql-passport
QUESTION
I'm trying to type a function to accept both local and general passport authenticator. Unfortunately I'm getting a "this" Typescript error:
...ANSWER
Answered 2021-Sep-06 at 08:42There seems to be an issue with either the typings of Passport
or some underlying issue with Authenticator
. To be honest, I'm not sure what TS is complaining about, when I redefine the type exactly as it was before, just re-declaring use
and unuse
the error goes away. My only guess is that passport
overlays the Authenticator
interface somewhere or there is a typescript bug:
QUESTION
I have a situation where I'm writing a custom mutation resolver, and currently have to pass the current_user's ID from my frontend to be able to then perform a ::find
on the User model. What would be ideal however, is to be able to use an instance of current_user
so that I don't have to rely on passing over an ID to my GraphQL server.
I'm still fairly new to the world of Laravel and GraphQL in general, however I've been reading up on the Lighthouse docs that mention the @auth directive, and other StackOverflow answers that mention using auth('api')->user()
, however that returns NULL for me. I should also mention that I'm using the lighthouse-graphql-passport-auth library for dealing with user authentication, if that makes any difference. Does anybody know how to access current_user?
ANSWER
Answered 2020-Nov-18 at 05:16I found an interesting part in the lighthouse-graphql-passport-auth docs that discuss setting a [global middleware][3] to insert the logged in user into the $context. This was exactly like what I needed, and after adding the line into the middleware section of lighthouse.php
config as mentioned in the docs, I was able to use $context->user()
to return the currently logged in user.
QUESTION
After adding authentication to our backend Graphql server the "Schema" and "Docs" are no longer visible in the Graphql Playground. Executing queries when adding a token to the "HTTP HEADERS" in the Playground does work correctly when authenticated and not when a user isn't authenticated, so that's ok.
We disabled the built-in Playground from Apollo-server and used the middleware graphql-playground-middleware-express
to be able to use a different URL and bypass authentication. We can now browse to the Playground and use it but we can't read the "Schema" or "Docs" there.
Trying to enable introspection
didn't fix this. Would it be better to call passport.authenticate()
in the Context
of apollo-server
? There's also a tool called passport-graphql but it works with local strategy and might not solve the problem. I've also tried setting the token in the header before calling the Playground route, but that didn't work.
We're a bit lost at this. Thank you for any insights you could give us.
The relevant code:
...ANSWER
Answered 2020-Aug-21 at 06:58I figured it out thanks to this SO answer. The key was not to use passport
as middleware on Express but rather use it in the Graphql Context
.
In the example code below you can see the Promise getUser
, which does the passport authentication, being used in the Context
of ApolloServer. This way the Playground can still be reached and the "Schema" end "Docs" are still accessible when run in dev
mode.
This is also the preferred way according to the Apollo docs section "Putting user info on the context".
QUESTION
I'm building a next.js project and while I usually would just use the "Custom Express Server" method to implement my graphql API (using apollo-server-express), I thought that it might be a good idea if I decoupled the next.js project from the graphql API so that each of the servers are hosted on different machines.
But usually I would implement any session-related logic in the graphql API, using something like graphql-passport; I figured that's good practice because if I ever choose to add another frontend (maybe a mobile app or something) they can share the same session logic. But given that I'm server side rendering content with next.js, how do I forward the user's session info to the graphql server? Because the next.js server shouldn't have to redo authentication, right?
Let me know if there are any flaws in the architecture too, I'm kind of new to this.
...ANSWER
Answered 2020-May-24 at 06:01Using the Next server to run the GraphQL service is certainly not a good idea, so yes, do separate the two.
Letting the Next server SSR-render pages with user specific content using the users session is probably not a good idea either, unless you have some specific use case that requires the served HTML pages to have the user specific data in them already. The reasons for this are:
SSR rendering will require lots of server side computations since all pages always will have to be rerendered.
NextJS is moving away (since v9.3) from the
getInitialPros()
way of doing things towards usinggetStaticProps()
to generate a page that is common for all users and which can load its session dependent stuff straight from the GraphQL API once it is displayed on the client device.
This approach will generally have higher performance and scale much better.
Should you really want to go the "SSR with user session data" route you start in the getServerSideProps(context)
method, where context.req
is the actual request which will have all your session data in it (cookies or headers).
This session data you can then extract from the req
and pass on to the GraphQL server requests that require authentication.
QUESTION
I'm using lighthouse-php to make a graphql api and I'm having a trouble changing middleware (it will be deprecated in new versions) directive to guard.
...ANSWER
Answered 2020-May-01 at 13:54In the meantime I've found another solution mentioned in the documentation:
https://lighthouse-php.com/master/security/authentication.html#global
So in short, I needed to add the AttemptAuthentication middleware to the lighthouse config. I use this with @auth(guard: "api") added to all my types.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install graphql-passport
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page