How to persist auth0-spa-js tokens?

Hi,

Auth0 seems to not be saving users sessions. I login successfully in my SPA application using “@auth0/auth0-spa-js”, but after refreshing the page the session is not kept. When trying to get the token silently it returns an error saying that a login is required.

I set the audience URL as suggested in “How does auth0-spa-js store tokens - #7 by madipradhana” without success.

Thanks

Hi @fabioDMFerreira,

Welcome to the Community!

How did you setup your app? Did you use a quickstart? Could you show us the code for the silent auth?

Hi @dan.woda,

Thanks.

I used the react quickstart available while configuring the app.

Here it is the code I am using. I am trying to get the token silently. If the request fails it redirects user to auth0 login form.

  const {
    isAuthenticated, loginWithRedirect, user, getTokenSilently
  } = useAuth0();

  if (!isAuthenticated) {
    getTokenSilently({
      audience: 'https://mmanagement.auth0.com/api/v2/'
    })
      .then(token => {
        console.log({ token });
      })
      .catch(err => {
        loginWithRedirect({
          audience: 'https://mmanagement.auth0.com/api/v2/'
        });
      })

    return <span>Loading...</span>
  }

Thank you,
Fábio

What browser are you using? This is likely an issue with the cookies.

I tested with Chrome 80.0.3987.116 and Safari 13.0.5.

Do you have some code sample using auth0-spa-js that is working in your machine? If yes I can test it in my browser.

Another update. When I access the auth0 login form I get these errors in console:

Already added the domain to the “Allowed Web Origins” in Auth0 dashboard, but it didn’t solve the problem.

@fabioDMFerreira,

I just tested this quickstart and the session persists between page refreshes for me on the latest version of chrome:

Can you DM the name of your application and your tenant name?

1 Like

@dan.woda,

It’s working! The session persists between page refreshes for created accounts.

I was using google identity provider in trial mode. I guess after configuring it in Connections > Social it will persist session between page refreshes as well.

This guide link you sent was useful to find the issue.

Thanks

2 Likes

Glad to hear it! Let us know if you run into anything else.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.