Auth0 Home Blog Docs

Using getTokenSilently() makes app crash. with error like auth0Client is undefined

Errors only occuring when using getTokenSilently annywhere in my code

When i log in and pass my token to my graph ql client i get the following error.

ApolloProvider was not passed a client instance. Make sure you pass in your client via the “client” prop.

loginWithPopup

C:/Users/nicta/Documents/dev/react/tests/ben/src/components/utils/Auth/react-auth0-spa.js:56

  53 |    } 
  54 |    const user = await auth0Client.getUser();  
  55 |    setUser(user);>  
>  56 |    setIsAuthenticated(true);     
  57 |  };  
  58 |   
  59 |  const handleRedirectCallback = async () => {

my code.

App.js

function App() {
  const {
    user,
    isAuthenticated,
    loginWithRedirect,
    getTokenSilently,
    loginWithPopup,
    logout
  } = useAuth0();

  const client =
  !isAuthenticated &&
    new ApolloClient({
      uri: "wss://learn.hasura.io/graphql"
    });
  isAuthenticated &&
    new ApolloClient({
      uri: "wss://learn.hasura.io/graphql",
      options: {
        reconnect: true,
        connectionParams: {
          headers: {
            Authorization: `Bearer ${getTokenSilently()}`
          }
        }
      }
    });

      return (
        <ApolloProvider client={client}>
          <div>
            <MiniDrawer>
              <Route path="/home" component={Home}></Route>
              <Route path="/upload" component={Upload}></Route>
              <Redirect from="/" exact to="/home" />
            </MiniDrawer>
          </div>
        </ApolloProvider>
      );
    }

    export default App;

index.js

ReactDOM.render(
  <BrowserRouter>
    <Auth0Provider
      domain={config.domain}
      client_id={config.clientId}
      redirect_uri={window.location.origin}
      onRedirectCallback={onRedirectCallback}
    >
      <ThemeProvider theme={theme}>
        <CssBaseline />
        <App />
      </ThemeProvider>
    </Auth0Provider>
  </BrowserRouter>,
  document.querySelector("#root")
);

When i try to login using loginWithRedirect.
I get the error.

ApolloProvider was not passed a client instance. Make sure you pass in your client via the “client” prop.

initAuth0

C:/Users/nicta/Documents/dev/react/tests/ben/src/components/utils/Auth/react-auth0-spa.js:32

29 |  
30 |      const isAuthenticated = await auth0FromHook.isAuthenticated();  
31 |    
> 32 |      setIsAuthenticated(isAuthenticated);    | ^  
33 |  
34 |      if (isAuthenticated) {  
35 |        const user = await auth0FromHook.getUser();

In my logs i find.

Type Description Date↓ Connection Application
Success Exchange Authorization Code for Access Token 5 minutes ago N/A Default App
Success Login Successful login 5 minutes ago google-oauth2 Default App

Please help me i need this.