jeudi 8 octobre 2020

Interceptors vs Polling for checking authorization for features

I have an webapp where the admin user can enable/disable some features within the admin private module.

The problem I was facing is how to propagate changes coming from the admin module to the other sessions user ?

I was thinking using polling but I realized that interceptors could make the job.

So in my front-end source code I use this as filter and reject request if the rights are not synchronized with these in database :

    $httpProvider.interceptors.push(function($q, $injector) {
      return {
       'request': function(config) {
            
            if (config.url === "url/feature") {
                
                const $http = $injector.get('$http');
                $http({
                    method: 'GET',
                    url: appServer + '/getFeatureActivations',
                    respondType: 'json',
                    headers: {
                       'Accept': 'application/json',
                       'Content-Type': 'application/json'
                    }
                })
                .success(function(data, status, headers, config) {
                    // Check if feature activation boolean in data object correspond to the current boolean state in $rootScope.feature1 / feature2 / ...
                    // If yes: do nothing and allow request
                    // If no: update and reject request + alert() to the user
                }).
                error(function(data, status, headers, config) {
                    console.log("error");
                });
            }
            else {
                return config;
            }
        },

        'response': function(response) {
            return response;
        }
      };
    });

What do you think about this procedure ? Is it a good practice or there is another cleaner way to do that ? What errors could occur ? Impact on performance ?

Aucun commentaire:

Enregistrer un commentaire