DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Edson Junior de Andrade
Edson Junior de Andrade

Posted on

Axios interceptor token on first call

Just add the capture of the token in the local storage in the configuration inside the interceptors request.

In my case I use lib '@react-native-async-storage/async-storage', you should configure capture according to your storage method.

import AsyncStorage from '@react-native-async-storage/async-storage';

apiApp.interceptors.request.use(
  async config => {
    const userData = await AsyncStorage.getItem(STORAGE_USER_AUTH_DATA).then(
      response => JSON.parse(response || '{}'),
    );

    config.headers = {
      Authorization: `Bearer ${userData?.accessData?.token}`,
      ...config.headers,
    };
    return config;
  },
  error => Promise.reject(error),
);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Visualizing Promises and Async/Await 🤯

async await

☝️ Check out this all-time classic DEV post