marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
25.02k stars 5.26k forks source link

Reject promise in checkError with logoutUser: false and without redirectTo causes an error #10172

Open thibault-barrat opened 2 months ago

thibault-barrat commented 2 months ago

What you were expecting: I would like to notify user when getPermissions request is failing. To do it, I catch the error in checkError and I reject promise with logoutUser: false and without redirectTo as I just want to display a notification without logout or redirect the user

What happened instead: The notification is correctly displayed but I have a JS error:

TypeError: can't access property "startsWith", redirectTo is undefined

This comes from useLogoutIfAccessDenied: https://github.com/marmelab/react-admin/blob/75a6683c9a64d6190ddde88d043b8e15b48c39e6/packages/ra-core/src/auth/useLogoutIfAccessDenied.ts#L103-L113

Steps to reproduce: Update getPermissions and checkError with following code:

getPermissions: () => {
    return Promise.reject('getPermissions error');
},
checkError: (error) => {
    if (error.status === 401 || error.status === 403) {
        return Promise.reject({ message: 'Unauthorized' });
    }
    if (error === 'getPermissions error') {
        return Promise.reject({
            message: error,
            logoutUser: false,
        });
    }
    return Promise.resolve();
},

Environment

slax57 commented 2 months ago

Thanks for this report!

Providing no redirectTo is currently not supported, but it would be nice if it was!

I'll label this issue as enhancement (and good first issue). Thanks!

rktamil commented 2 months ago

I had fixed it in https://github.com/marmelab/react-admin/pull/10177. Could you please assign it to me ?

slax57 commented 1 month ago

@rktamil No need to assign the issue. Referencing the issue number in your PR description is enough. Thank you for contributing!