title: RN - Axios - How to add an axios interceptor in saga - React Native
body: <p>The code snippet in saga looks like this </p>
  
  <pre><code>function* addAmount(action) {
  
  
      try {
          const response = yield call(axios.get, RECHARGE_IP + 'api/createpaymentorder?username=' + action.username + '&amp;amount=' + action.amount,{}, {
              headers: {
                  'authorization': action.token,
              }
          })
  
          yield put({ type: ADD_MONEY_INITIATE_SUCCESS, data: response });
  
      }
      catch (e) {
  
          yield put({ type: ADD_MONEY_INITIATE_FAILURE, error: e });
      }
  }
  </code></pre>
  
  <p>how can i attach a axios response interceptor with this so that i could check whether the token is expired or not and if expired i'll have to send refresh token to get new access token. How can i use axios interceptor in this scenario ?</p>
  
  <hr>
  
  <h2>solved it myself the code is as follows</h2>
  
  <pre><code>async componentDidMount() {
  
    try {
      const value = await AsyncStorage.getItem(REFRESH_TOKEN);
      if (value !== null) {
        // We have data!!
        console.log(value);
        this.setState({
          refreshToken: value
        })
      }
    } catch (error) {
      // Error retrieving data
    }
    //running interceptor
    const self = this;
    axios.interceptors.response.use(function(response) {
      // Do something with response data
      // console.log('response in interceptor', response);
      return response
    }, function(error) {
      const originalRequest = error.config;
      // Do something with response error
      console.log(JSON.stringify(error));
      console.log('error==================================', error.response.data.error);
  
  
      if (error.response.status === 401 &amp;&amp; error.response.data.error === 'invalid_token') { //custom error checking based on the data i get
  
        console.log('hit error');
  
        originalRequest._retry = true
        const retryOrigReq = new Promise((resolve, reject) =&gt; {
          axios.post(REFRESH_TOKEN_URL, null) //give ur refrsh token url 
            .then((resp) =&gt; {
              // console.log(resp)
              self.setState({
                token: resp.data.access_token
              });
              try {
                AsyncStorage.setItem('AUTH_TOKEN', 'bearer ' + resp.data.access_token);
              } catch (error) {
                // Error saving data
              }
              try {
                AsyncStorage.setItem('REFRESH_TOKEN', resp.data.refresh_token);
              } catch (error) {
                // Error saving data
              }
              originalRequest.headers['Authorization'] = 'Bearer ' + resp.data.access_token
              resolve(axios(originalRequest));
            })
            .catch((error) =&gt; {
              console.log(error);
            })
        });
        return retryOrigReq;
      } else {
        return Promise.reject(error);
      }
    });
  }
  </code></pre>