You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've request and response interceptors implemented on my React Native app. I use the request interceptor to log the request body to the console. and I use the responseinterceptor for setting Authentication Token from my response header.
When I checked the interceptor is getting fired twice for all my requests and responses. I've no idea why it is doing so.
can someone help out here?
importaxiosfrom'axios';import{clearAsyncStorage,saveToken}from'./JwtService';import{store}from'../redux/store';import{removeAccessToken}from'../redux/globalSlice';constbaseURL='http://localhost:5000/delivery';exportdefaultclassApiService{statictoken=null;staticreferenceId=null;staticsetHeader(){if(!ApiService.token)return;axios.defaults.headers.common['Authorization']=`Bearer ${ApiService.token}`;axios.defaults.headers.common['Accept']='application/json';axios.defaults.headers.common['Content-Type']='application/json';axios.defaults.headers.common['reference-id']=ApiService.referenceId;// Axios interceptor to get new Auth token from response header and set to next requestsaxios.interceptors.response.use(x=>{const_newAuthToken=x.headers['authorization']?.slice(7);if(_newAuthToken){ApiService.token=_newAuthToken;ApiService.setHeader();saveToken(_newAuthToken)}returnx;});axios.interceptors.request.use(x=>{console.log(x);constheaders={
...x.headers.common,
...x.headers[x.method],
...x.headers,};['common','get','post','head','put','patch','delete'].forEach(header=>{deleteheaders[header];});constprintable=`${newDate()} | Request: ${x.method.toUpperCase()} | ${x.url} | ${JSON.stringify(x.data,)} | ${JSON.stringify(headers)}`;console.log(printable);returnx;});}staticget(resource){returnaxios.get(`${baseURL}/${resource}`);}staticpost(resource,params){returnaxios.post(`${baseURL}/${resource}`,params);}staticput(resource,params){returnaxios.put(`${baseURL}/${resource}`,params);}staticasynchandleError(error,is_login=false){if(error.response){switch(error.response.status){case400:
returnString(error.response.data.message);case401:
if(!is_login){awaitclearAsyncStorage();store.dispatch(removeAccessToken());}return`Unauthorized`;case404:
return`Not Found.`;case413:
return`Payload too large.`;default:
return`An Error has occurred.`;}}elseif(error.request){returnerror.request._response;}else{returnerror.message;}}staticclearHeaders(){deleteaxios.defaults.headers.common['Authorization'];deleteaxios.defaults.headers.common['reference-id'];ApiService.token=null;ApiService.referenceId=null;}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I've
request
andresponse
interceptors implemented on myReact Native
app. I use therequest
interceptor to log the request body to the console. and I use theresponse
interceptor
for settingAuthentication Token
from my response header.When I checked the
interceptor
is getting fired twice for all myrequests
andresponses
. I've no idea why it is doing so.can someone help out here?
Beta Was this translation helpful? Give feedback.
All reactions