Issue
I have ordinary custom data provider with only urls changed, and I see no sense to bring it's code here. The problem is that on 400 HTTP status response, my data provide redirects to /login.
As for me it's quite inadequate behaviour, since 400 bad request is not a 401 unauthorized. The problem of 400 originates from API backend, and I see no reason to re-login the user. Is it possible to prevent that redirection?
Solution
The authProvider
inspects API errors in its checkError
method, so your normally have the ability to decide when an error requires a logout (in that case, return a rejected Promise), and when the error should just trigger a notification (and in that case, return a resolved Promise).
For instance:
// in src/authProvider.js
export default {
login: ({ username, password }) => { /* ... */ },
checkError: (error) => {
const status = error.status;
if (status === 401 || status === 403) {
localStorage.removeItem('auth');
return Promise.reject();
}
// other error code (404, 500, etc): no need to log out
return Promise.resolve();
},
// ...
};
More info at https://marmelab.com/react-admin/AuthProviderWriting.html#checkerror
Answered By - François Zaninotto
Answer Checked By - - Robin (ReactFix Admin)