Replies: 1 comment 3 replies
-
So if you call // _app.jsx
import { SessionProvider } from "next-auth/client"
export default function App({ Component, pageProps: {session, ...pageProps} }) {
return (
<SessionProvider session={session}>
<Component {...pageProps}/>
</SessionProvider/>
)
} And use the following hook when you need to make requests to GraphQL // hooks/useClient.js
import * as React from "React"
import { useSession } from "next-auth/client"
export default function useClient() {
const accessToken = useSession()[0]?.accessToken
const graphqlClient = useGraphQLClient() // Not sure how you get your graphql client
React.useEffect(() => {
if (accessToken) {
graphqlClient.setHeader('Authorization', `Bearer ${accessToken}`);
}
}, [accessToken])
return graphqlClient
}
Remember, since getting session is an async operation, in the beginning, the returned graphql client won't have the right header. You have to make sure to postpone requests that must have this header until the user is logged in and How to do this depends on how you use the graphql client in your code. |
Beta Was this translation helpful? Give feedback.
-
Hello I need to pass the jwt token into the headers for every request that needs authentication.
I have a graphql client and I need to set the headers so that can happen. I ended with that solution .I put the code below in the _app js file . What do you think ?
Beta Was this translation helpful? Give feedback.
All reactions