-
Notifications
You must be signed in to change notification settings - Fork 350
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
React: Apollo hooks #69
Comments
More to the point, consider the following code: import { ApolloClient } from "apollo-client";
import { HttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";
import { InMemoryCache } from "apollo-cache-inmemory";
const setAuthorizationLink = setContext((request, previousContext) => {
// get hook??
// get accessToken somehow???
return {
headers: { authorization: "1234" }
};
});
export function createApolloClient() {
const httpLink = new HttpLink({
uri: "/api/graphql"
});
const link = setAuthorizationLink.concat(httpLink);
return new ApolloClient({
link,
cache: new InMemoryCache()
});
} How do I get my bearer token inside the setContext callback? |
That's a good question. I don't know how all the apollo setup works, so I'm not sure I'll be able to help you that much. What I can say is that if you have to setup everything outside a react component (like you're doing right now), it's unlikely the react wrapper will help you that much. The react wrapper is using React context, which needs a component to hold the state, so you'll have to ignore the wrapper and use the sdk itself. |
Feel free to open an issue in the samples repo if you have more questions, since the react wrapper is not part of this sdk. |
A quick way to get the token in apollo link is through localStorage. After login, write token to localStorage. Is storing the access token in localStorage safe? What if the token expires? Does it need to call |
@johannwattzon Storing tokens in local storage is not recommended for security reasons. As @luisrudge mentioned, if you're having to use the Auth0 client outside of a React component, then you will most likely have to use the SDK directly rather than the React wrapper. |
is it in annyway possible to not overcomplicate things and make the following code or something similar work.
|
@NicTanghe The token might need to be refreshed when it expires. Also |
@franciscolourenco Have you got a sample repo you can share that you're trying to get working? |
@stevehobbsdev I setup an example repo here. The apollo client and ecosystem assumes the token is stored in localStorage, and that it also doesn't expire.
We could call the login method after authentication is complete, and store it in memory, and restart apollo. |
i think this may help you. the following code will check the token each time the apollo client makes a request to the server and seamlessly get a new token if not valid, although it is not for auth0 specifically, i'm sure it could be easily modified. this is from ben awad's import React from "react"; const cache = new InMemoryCache({}); const requestLink = new ApolloLink(
); const client = new ApolloClient({
]), ReactDOM.render( |
@micahgoodreau thanks. ApolloLink looks "sophisticated" |
You can wrap ApolloProvider in the Auth0 Context Consumer and pass the Auth0 client context into a function that returns the ApolloClient as a singleton. It would look something like this:
In this example, you will have access to the auth0Client.getAccessTokenSilently function inside your apollo client function where you can setContext for headers or whatever you need to do. |
Hi, how would the React wrapper work in combination with @apollo/hooks?
At some point in my application, I would like to add an additional header with my access token if the user decided to log in. Some graphql queries should be available when the user isn't logged in.
So I have a scenario where I want to add the header only at a later point.
The text was updated successfully, but these errors were encountered: