React-admin: ra-data-graphql reports every error aus AUTH_ERROR

Created on 10 Oct 2019  路  4Comments  路  Source: marmelab/react-admin

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:

  • use a data provider that uses ra-data-graphql
  • have your graphql backend throw an error on some query or mutation
  • log the type argument in your authprovier, you will see its an AUTH_ERROR

Related code:

  • sorry, i have none at this point

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

  • React-admin version: 2.9.6
  • Last version that did not exhibit the issue (if applicable): none
  • React version: 16.10.0
  • Browser: chrome
  • Stack trace (in case of a JS 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

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marknelissen picture marknelissen  路  3Comments

Kmaschta picture Kmaschta  路  3Comments

kopax picture kopax  路  3Comments

kdabir picture kdabir  路  3Comments

Dragomir-Ivanov picture Dragomir-Ivanov  路  3Comments