# How to get token from outside React components?

**URL:** <https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756>\
**Category:** Get Help\
**Tags:** react, auth0\
**Created:** [August 29, 2021, 8:12am UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756 "2021-08-29T08:12:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaohui](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/xiaohui/32/31422_2.png) [@xiaohui](https://community.auth0.com/u/xiaohui)\
**Post date:** [August 29, 2021, 8:12am UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756/1 "2021-08-29T08:12:06Z")

</div>

I am using axios to make requests to my APIs. Currently I use an axios interceptor to set the token to every request that gets sent (so I don’t have to repeat getting the token from within the component). Is there any way to do this? My first attempt looks something like this:

```auto
fetchClient.interceptors.request.use(
  async (config) => {
    const { user, getAccessTokenSilently } = useAuth0();

    if (!user || !user.sub) return Promise.reject("No user");

    const token = await getAccessTokenSilently({
      audience: process.env.REACT_APP_AUTH0_API_AUDIENCE,
    });

    const userId = user.sub.split("|")[1];
    config.headers["Authorization"] = `Bearer ${token}`;
    config.headers["userId"] = userId;
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

```

But of course you can’t call Hooks and therefore can’t call getAccessTokenSilently from non functional components. Any way to get around this? Thanks all

---

<div class="post-metadata">

**Author:** ![shylae](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/shylae/32/28560_2.png) [@shylae](https://community.auth0.com/u/shylae)\
**Post date:** [January 25, 2022, 10:42am UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756/2 "2022-01-25T10:42:56Z")

</div>

Hi, I’m facing the same issue, I want to get the access token outside a React Component.  
Did you find a solution? 🙂

---

<div class="post-metadata">

**Author:** ![jiajun](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jiajun/32/33256_2.png) [@jiajun](https://community.auth0.com/u/jiajun)\
**Post date:** [November 10, 2022, 9:31pm UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756/3 "2022-11-10T21:31:30Z")

</div>

Create a Interceptor class, init and export a instance

```auto
class AuthInterceptor {
  setAuthGetter(getToken) {
    this.getToken = getToken;
  }

  async intercept(config) {
    if (!this.getToken) {
      return config;
    }

    try {
      const token = await this.getToken();
      config.headers['Authorization'] = `Bearer ${token}`;
    } catch (e) {
      console.log(e);
    }
    return config;
  }
}

export const authInterceptor = new AuthInterceptor()
fetchClient.interceptors.request.use(authInterceptor.intercept)

```

Create a function component to set the getToken method

```auto
function AuthInject() {
  const { user, getAccessTokenSilently } = useAuth0();

  useEffect(() => {
    authInterceptor.setAuthGetter(getAccessTokenSilently);
    return () => authInterceptor.setAuthGetter(undefined);
  }, [getAccessTokenSilently]);

  return null;
}

//set AuthInject as children
<Auth0Provider {...auth0Config}>
  <AuthInject />
  <App />
</Auth0Provider>;

```

---

<div class="post-metadata">

**Author:** ![didier7](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@didier7](https://community.auth0.com/u/didier7)\
**Post date:** [March 12, 2024, 11:25am UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756/4 "2024-03-12T11:25:12Z")

</div>

This did not work for me: when `intercept` is called, `this` is not defined.

Anyone managed to make it work?

---

<div class="post-metadata">

**Author:** ![dbi75](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dbi75/32/59207_2.png) [@dbi75](https://community.auth0.com/u/dbi75)\
**Post date:** [August 14, 2024, 1:12pm UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756/7 "2024-08-14T13:12:32Z")

</div>

Had the same issue, you need to bind the functions:

```auto
class AuthInterceptor {
  private getToken: any = undefined;

  constructor() {
    this.intercept = this.intercept.bind(this);
    this.setAuthGetter = this.setAuthGetter.bind(this);
  }
...

```

---

<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:** [May 6, 2025, 4:32pm UTC](https://community.auth0.com/t/how-to-get-token-from-outside-react-components/68756/8 "2025-05-06T16:32:12Z")

</div>

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