DEV Community

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)