How to get refresh token like getAccessTokenSilently?

  • SDK name: auth0-react
  • SDK Version: v1.10.2
  • Code Snippets/Error Messages/Supporting Details/Screenshots:

After the user signs in, we can use getAccessTokenSilently and get access token, but how to get refresh token? I can see in the networks tab, there is a page with service worker that gives the response in json that contains refresh_token along with other things.

Screenshot:

How can I access this response ? As I am unable to get a hold of this service worker file named token.

So, in order to get the refresh token, you need to first pass useRefreshToken={true}. And then, pass cacheLocation="localstorage" to the Auth0Provider. This will store the tokens and other response in the localStorage.

And then, you need to use LocalStorageCache class to access the keys and its value.

    const refresh_token = new LocalStorageCache;
    const key = refresh_token.allKeys().find(key => key.includes('auth0spa'));
    const refresh_token_value = refresh_token.get(key);
    const finalRefreshToken = refresh_token_value?.body?.refresh_token
    localStorage.setItem('refresh_token', finalRefreshToken);

Hope it helps!

3 Likes

Hey there @praveenprk welcome to the community!

Great to know you got this sorted and thanks a bunch for sharing with the community :smile:

1 Like

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