Replies: 4 comments 21 replies
-
Make a function, let's say refreshToken, that takes an argument, inside the Apollo Client file where you have createApolloClient or initializeApolloClient functions. Have a global variable called token in that file which will get replaced with the argument refreshToken takes. So now in your page, inside getStaticProps or getServerSide props where you call initialize Apollo and add Apollo State, before initializing Apollo and after calling getSession function of next auth, call the refreshToken and pass in the session variable. This refresh token function will replace the value of variable token, and then you can use token's session object to extract the jwt token from it and pass it as Authorization header in the createApolloClient function. As far as I know, this works for me as I am using Hasura and I need bearer token in Authorization header to authenticate special queries. Hope it helps. Lacklustre solution but better than having no solution at all. |
Beta Was this translation helpful? Give feedback.
-
in my case, I use session with getSession function
|
Beta Was this translation helpful? Give feedback.
-
I haven't been using nextauth in a long time, but for anyone still looking for ideas on how to get this done, it'd be something like this. If you use another authentication provider, like Firebase, Auth0, etc., you'd basically replace Realm with the provider that makes your user session available, and then pass that session and its JWT down to So instead of
For the
For the client, you'd have something like:
For the createApolloClient function, that looks something like this:
And then your
You'd have to make sure you enabled anonymous and JWT auth in Realm (or any other identity provider if needed). I haven't used this in a long time since my stack changed drastically since back when I needed this, but I hope it helps anyone still trying to figure this out! |
Beta Was this translation helpful? Give feedback.
-
For those still looking for an answer to this question, here's what I came up with. In import jsonwebtoken from 'jsonwebtoken';
// ...
const session = async function session(params: { session: Session; user: User; token: JWT }) {
const encodedToken = jsonwebtoken.sign(params.token, process.env.SECRET, { algorithm: 'HS256' });
params.session.token = encodedToken;
return params.session;
};
export default NextAuth({
providers: [ /* ... */ ],
callbacks: {
session,
},
}); In your Apollo provider: export const AuthorizedApolloProvider: FunctionComponent = (props) => {
const authLink: ApolloLink = setContext(async () => {
const session = await getSession();
return {
headers: {
Authorization: `Bearer ${session?.token}`,
},
};
});
const apolloClient = new ApolloClient({
link: ApolloLink.from([authLink, /* ... other Apollo links ... */]),
});
return <ApolloProvider client={apolloClient}>{props.children}</ApolloProvider>;
}; |
Beta Was this translation helpful? Give feedback.
-
Your question
How can we pass the JWT to an Apollo Provider to authorize MongoDB Realm requests?
Reproduction
Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
Beta Was this translation helpful? Give feedback.
All reactions