Skip to content

Conversation

fzaninotto
Copy link
Member

Problem

When a developer forgets to return a Promise in authProvider.checkAuth() or authProvider.checkError(), they see a cryptic error message:

Cannot read properties of undefined (reading 'then')

Solution

Although the doc (and typescript) specifies that these methods must be async, react-admin can be more permissive and not throw any error in this case, by using async methods instead of promises.

How To Test

Update the authProvider in the simple example to use sync methods:

    checkError: ({ status }) => {
        if (status === 401 || status === 403) {
            throw new Error();
        }
    },
    checkAuth: () => {
        if (localStorage.getItem('not_authenticated')) {
            throw new Error();
        }
    },

The simple example should still work.

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • [ ] The PR includes unit tests (if not possible, describe why)
  • [ ] The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

No tests or stories because this is a corner case.

@fzaninotto fzaninotto changed the title Fix error when authProvider check methods are nnot async Fix error when authProvider check methods are not async Aug 20, 2025
@fzaninotto fzaninotto added the RFR Ready For Review label Aug 20, 2025
@slax57 slax57 added this to the 5.10.2 milestone Aug 21, 2025
@slax57 slax57 merged commit 377f29f into master Aug 21, 2025
15 checks passed
@slax57 slax57 deleted the fix-auth-hook branch August 21, 2025 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Development

Successfully merging this pull request may close these issues.

2 participants