question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Token call and new Token Generation on every route change

See original GitHub issue

Every time I try to add the SDK to a new react project, I always get this issue. Previously, I would just use the react sample, which does not have this problem. But, now, I cannot use the sample anymore and really need to get this working.

I have a couple of pages with basic routing. brand new react project. Auth0 SDK added right after new project creation. Login, Logout works fine.

  • Use Logs In.
  • Page Reload - Network Token Call
  • Visit another route/view which uses Token - Network Token Call
  • Visit another route/view which does not use Token - Again, Network Token Call
  • Return to Home Page - Network Token Call, again.

The SDK does not prompt a login. It knows that the user is already authenticated. But, it cannot stop itself from asking for a new token for every route change or page reload.

Now, with the exact same browser, same computer, the auth0 react sample, does not have this problem. It’s clearly something I am not including somewhere.

It’s similar to the issue reported here - https://github.com/auth0/auth0-react/issues/44 , I think.

Here is a photo of the network calls.

TokenCallIssue1

I have followed the discussion on the https://github.com/auth0/auth0-react/issues/44 and also at https://github.com/auth0/auth0-react#getting-started

Here is my index file, App being wrapped up as per instructions.

ReactDOM.render(
  <Auth0Provider
    domain={config.domain}
    clientId={config.clientId}
    audience={config.audience}
    redirectUri={window.location.origin}
    onRedirectCallback={onRedirectCallback}
    scope={config.scope}
  >
    <React.StrictMode>
      <App />
    </React.StrictMode>,
  </Auth0Provider>,
  document.getElementById('root')
);

Here is my App file.

function App() {

  const { isLoading, error } = useAuth0();

  if (error) {
    return <div>Oops... {error.message}</div>;
  }

  if (isLoading) {
    return <Loading />;
  }  
  return (
    <Router history={history}>
      <Container>
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/about" component={About} />
        <Route path="/userprofile" component={Profile} />
      </Switch>
      </Container>

    </Router>
  );
}

export default App;

Even, with a regular component which does not even need authentication , like this…

const About = () => (
    <div>
        <h4>About</h4>
        <p>React JS App that works in tandem with the Random Stuff Generator API Server</p>
    </div>
);

export default About;

The token endpoint gets called and I am provided with a new token.

What was the expected behavior?

The behavior should be similar to what I see in the sample app. route changes should not be triggering token calls.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
zhilcommented, May 13, 2021

By default tokens are cached in memory, and getTokenSilently still send request to …/oauth/token each time when you reload page.

its correct behavior because of security (it provide better protection against XSS), but… its annoying and it slow down my App page loading speed. So, I am also using localstorage cache

<Auth0Provider
....
      cacheLocation="localstorage"
    >

I hope it will save time for somebody 😃

1reaction
Jay-study-nildanacommented, Jan 6, 2021

@adamjmcgrath

aha! I knew i was making a mistake of my own making. I am an old .NET guy trying to pick up some front end skills.

thank you very much man. I will close this as resolved now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

on change route need to run a function to check the token
You can use CanActivate to guard routes or call a function during transition of routes , example is mentioned below
Read more >
Token Based Authentication Made Easy - Auth0
Navigate to the localhost:3000/token route and you will see a new token generated. Copy this token, then navigate to localhost:3000/api?
Read more >
Should login/refresh authentication routes return new tokens if ...
Hitting /token with a valid refresh_token , returns a new access_token and refresh_token and this should remain consistent, regardless of wether ...
Read more >
Requesting access tokens and authorization codes
By default, these parameters must be x-www-form-urlencoded and specified in the request body (as shown in the sample above); however, it is possible...
Read more >
Angular(v14) JWT Access Token Authentication & Refresh ...
Now register the 'AuthGuard' at each route and register the route data like 'requiredAuth' at required routes. src/app/app-routing.module.ts:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found