Search code examples
reactjsoauth-2.0reduxreact-reduxadmin-on-rest

Authentication and OAuth2 implementation in admin-on-rest


First of all I would like to thank you for the awesome work in admin-or-rest that you have done for this template.

I would like to ask if there is an easy workaround in order to implement oauth2 authentication.

There is an example on how we can implement a basic JWT Authentication but I cannot figure out how I’m going to handle the 401 errors in order to handle to refresh the token as described in oAuth2.

Is there any way to apply a middleware? I’ve tried in the past redux-api-middleware in order to have full control of my action and http handling, but this cannot been applied to this system once all the calls except the login are being handled automagically.


Solution

  • This has changed in admin-on-rest 1.0, and the documentation tries to be clearer about how to achieve what you want to do:

    // in src/authClient.js
    import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR } from 'admin-on-rest';
    
    export default (type, params) => {
        if (type === AUTH_LOGIN) {
            // ...
        }
        if (type === AUTH_LOGOUT) {
            // ...
        }
        if (type === AUTH_ERROR) {
            const { status } = params;
            if (status === 401 || status === 403) {
                localStorage.removeItem('token');
                return Promise.reject();
            }
            return Promise.resolve();
        }
        return Promise.resolve();
    };
    

    More info at https://marmelab.com/admin-on-rest/Authentication.html#catching-authentication-errors-on-the-api