What you were expecting:
only real auth errors are handled as AUTH_ERROR. the other errors should be reported as a suitable error
What happened instead:
every error is reported as AUTH_ERROR. If your authprovider logs you out on AUTH_ERROR, it gets really annoying.
Steps to reproduce:
type argument in your authprovier, you will see its an AUTH_ERRORRelated code:
Other information:
there is a related PR: https://github.com/marmelab/react-admin/pull/3604
i tried that out, but it does not solve the issue (or maybe there was something other wrong)
Environment
You should parse the error object passed as the 2nd parameter to the auth provider function and check the status code. Then you should only log out your users on 401 and 403.
I have something like this:
import get from 'lodash/get'
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_CHECK, AUTH_GET_PERMISSIONS } from 'react-admin'
const parseError = ({ networkError, graphQLErrors, ...rest }) => {
const message =
get(graphQLErrors, '[0].message') ||
get(networkError, 'result.errors[0].message') ||
get(networkError, 'message') ||
get(rest, 'message')
const status =
get(graphQLErrors, '[0].statusCode') ||
get(networkError, 'statusCode') ||
get(rest, 'statusCode') ||
get(rest, 'status')
return { message, status }
}
export const authProvider = async (type, params) => {
if (type === AUTH_LOGIN) {
const { email, password } = params
return login(email, password)
}
if (type === AUTH_LOGOUT) {
return clearTokens()
}
if (type === AUTH_ERROR) {
const { status } = parseError(params)
if (status === 401 || status === 403) {
clearTokens()
throw new Error('Expired session. Please log in.')
}
return true
}
if (type === AUTH_CHECK) {
const { accessToken } = await getTokensAndRefresh()
return accessToken
}
if (type === AUTH_GET_PERMISSIONS) {
return getRole()
}
throw new Error('Unknown authentication method: ' + type)
}
Thank you for opening this issue. In order to confirm it, we'll have to reproduce it.
As explained in the bug report template, please fork the following CodeSandbox and repeat your issue on it:
https://codesandbox.io/s/github/marmelab/react-admin/tree/master/examples/simple
This is the simplest way to confirm a bug is related to the React Admin codebase.
Closing as @melvynhills actually answered it. Thanks!
thank you guys for the information, this should solve the problem for the moment.
In my opinion, it should be the dataprovider's responsibility to parse the error and decided whether to pass it to the auth-provider, so that an AUTH_ERROR would really be an authentification error
Most helpful comment
thank you guys for the information, this should solve the problem for the moment.
In my opinion, it should be the dataprovider's responsibility to parse the error and decided whether to pass it to the auth-provider, so that an AUTH_ERROR would really be an authentification error