I’ve hit a problem having followed through the Ultimate React Native course.
If an API call returns a 401 (unauthorised) I want the user to handle this by clearing the stored user but cannot fathom how this should be done.
I am assuming I should catch the 401 in the client.js file but what is the right way to handle this? All my attempts are failing because I am breaking the rules for hooks, which I clearly am not understanding properly .
Can anyone help.?
apiClient.get = async (url, params, axiosConfig) => {
const response = await get(url, params, axiosConfig);
if (response.ok) {
cache.store(url, response.data);
return response;
} else {
if (response.status === 401) {
console.log('Logout required but how do I do it?!');
} else {
console.log(response);
}
}
const data = await cache.get(url);
return data ? { ok: true, data } : response;
};