-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Not documented: How can I setContext (using 'apollo-link-context, etc.) AFTER ApolloClient is initiliazed? #4990
Comments
you need to use apollo-link-context. here's an example from my project:
I just set the |
also, there's an example in the docs |
Please can you tell me did find the decision? Because i have the same situation. Can you sent the code example please |
I stumbled across the same issue. I am retrieving token from localStorage. But the context is set BEFORE the user logs in and hence no token is set in context. I had to reload the page for apollo client to reinitialize after the user logs in. |
Got the same issue. Need to change the headers whenever the URL changes. |
I have a similar problem (outlined here). Has anyone determined where you can set these headers early enough before (a) the first query or (b) mutation and (c) submissions are established? Right now I my Vue component is created along with the Apollo Client before my Single Sign On is completed and there is no way that I can find to set headers other than using the |
Useful note: Remember that default |
I ended up hard-redirecting the browser after sign in and sign out ( |
I used middleware so that on every request I'll pass the token, taking it from local storage, please see these docs https://www.apollographql.com/docs/react/networking/network-layer/#middleware |
The sample code
|
@youbek, thanks for the insight and code snippet. Our Vue webapp, uses Vue-Apollo for the Apollo Client interface and we use Okta's library as our OAuth/OIDC provider to generate an access token. Because the accessToken can expire, and be refreshed at any time, we use the callback to ensure each invocation by the ApolloClient has the latest and a valid Access Token furnished by Okta's property accessors rather than going to localStorage directly; however, because it is asynchronous, it doesn't quite work. What I was seeking in this Reported Issue is for a way to make Vue Apollo's |
Proposed localStorage "workaround" with redirect-after-login is nonsense, because in most browsers if user has disabled cookies, we don't have access to localStorage. So how do we properly change headers after user logs in? How to do it without localStorage? |
the token middleware run on every request so the problem is not with middleware. this is wrong const authLink = setContext((_, {headers, ...context}) => {
const token = localStorage.getItem('auth:token');
return {
headers: {
...headers,
authorization: `Bearer ${token}`,
},
...context,
};
}); this is right const authLink = setContext((_, {headers, ...context}) => {
const token = localStorage.getItem('auth:token');
return {
headers: {
...headers,
...(token ? {authorization: `Bearer ${token}`} : {}),
},
...context,
};
}); |
Hi @EhsanSarshar, yes, confirming that you're absolutely correct in that the setContext middleware runs on each request as you note. |
It depends on a requirement, but if you want to delay the link itself, my lib may help: https://github.com/dai-shi/apollo-link-lazy |
Getting a forward is not a function error when using any of the solutions here, any insight on what I might be missing ? |
Has anyone got this to work when the token is returned from a promise and without using localstorage? (For example Okta, AuthO, Microsoft Authentication Lib (msal.js)) |
I got this to work with https://github.com/AxaGuilDEv/react-oidc, which is a React context wrapper for oidc-client. Not sure if it's applicable for everyone's use-case here,but copying this template worked great for me: https://github.com/osstotalsoft/generator-webapp-rocket/blob/master/generators/app/templates/infrastructure/src/apollo/AuthApolloProvider.js What it does is use |
It doesn't sound like there is an outstanding issue here, so closing. Thanks! |
After user logs in need to set auth headers (token), but ApolloClient is already initialized at the top level of my app. Is there any example on the best way to do this? Thanks.
The text was updated successfully, but these errors were encountered: