Axios interceptor token on first call

Edson Junior de Andrade - Sep 29 '22 - - Dev Community

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
. . . . . . . . . . . . . . . . . . . . .
Terabox Video Player