# How to get a new access token by current access token or refresh token in auth0-react?

**URL:** <https://community.auth0.com/t/how-to-get-a-new-access-token-by-current-access-token-or-refresh-token-in-auth0-react/103386>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [March 27, 2023, 10:08am UTC](https://community.auth0.com/t/how-to-get-a-new-access-token-by-current-access-token-or-refresh-token-in-auth0-react/103386 "2023-03-27T10:08:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leslie](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/leslie/32/45792_2.png) [@leslie](https://community.auth0.com/u/leslie)\
**Post date:** [March 27, 2023, 10:08am UTC](https://community.auth0.com/t/how-to-get-a-new-access-token-by-current-access-token-or-refresh-token-in-auth0-react/103386/1 "2023-03-27T10:08:54Z")

</div>

I’m using @auth0/@auth0-react SDK.  
I want to get a new access token when I clicked a button. The current access token is not expired actually.  
Here is my code.

```auto
const providerConfig = {
  domain: auth.domain,
  clientId: auth.clientId,
  audience: auth.audience,
  redirectUri: window.location.origin,
  useRefreshTokens: true,
  scope: auth.scope,
  cacheLocation: 'localstorage' as CacheLocation,
};

ReactDOM.render(
  <Auth0Provider {...providerConfig}>
    <Auth0Context.Consumer>
      {({ getAccessTokenSilently, loginWithRedirect }) => {
        // toLogin = loginWithRedirect;
        // deferred.resolve(getAccessTokenSilently);
        return (
          <Provider store={store}>
            <App />
          </Provider>
        );
      }}
    </Auth0Context.Consumer>
  </Auth0Provider>,
  document.getElementById('root') as HTMLElement
);

```

I can get the access token and refresh token since I set the ` cacheLocation: 'localstorage'`.  
I want something like this

```auto
const getNewToken = () => {
// generate a new access token and use it replace current access token
}
...
<Button onClick={getNewToken}>new token</Button>

```

I have tried to use the code below, but it return an error.

```auto
const options = {
  method: 'POST',
  url: `https://${auth.domain}/oauth/token`,
  headers: {
    'content-type': 'application/x-www-form-urlencoded',
    authorization: `Basic ${code}`,
  },
  data: new URLSearchParams({
    grant_type: 'refresh_token',
    client_id: auth.clientId,
    refresh_token: refresh_token,
  }),
};

export const getNewToken = () => {
  axios
    .request(options)
    .then(function (response) {
      console.log(response.data);
    })
    .catch(function (error) {
      console.error(error);
    });
};

```

The variable `code` is this field, I don’t know where to get the field `code` actually.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/2/b/2bb9174faed4e3d5dd3edc2e159260a392f72900.png)

The error info.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/3/7/379c632b4f68202a3ec941f85b4aec3db1eea096.png)

---

<div class="post-metadata">

**Author:** ![leslie](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/leslie/32/45792_2.png) [@leslie](https://community.auth0.com/u/leslie)\
**Post date:** [March 28, 2023, 9:35am UTC](https://community.auth0.com/t/how-to-get-a-new-access-token-by-current-access-token-or-refresh-token-in-auth0-react/103386/2 "2023-03-28T09:35:09Z")

</div>

I found there is an easy way to implement this.

```auto
 const getNewToken = async () => {
    const newAccessToken = await getAccessTokenSilently({ ignoreCache: true });
 }

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 29, 2023, 12:46am UTC](https://community.auth0.com/t/how-to-get-a-new-access-token-by-current-access-token-or-refresh-token-in-auth0-react/103386/4 "2023-03-29T00:46:41Z")

</div>

Hey there @leslie thanks for following up with your findings, and welcome to the community!

That’s correct, [silent authentication](https://auth0.com/docs/authenticate/login/configure-silent-authentication) ([getAccessTokenSilently](https://auth0.github.io/auth0-react/interfaces/Auth0ContextInterface.html#getAccessTokenSilently) in this case) is exactly the manner in which you should go about obtaining new access tokens. Setting `ignoreCache: true` forces the refresh regardless of if what’s in the cache.

Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 12, 2023, 12:47am UTC](https://community.auth0.com/t/how-to-get-a-new-access-token-by-current-access-token-or-refresh-token-in-auth0-react/103386/5 "2023-04-12T00:47:22Z")

</div>

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