4

Since i'm using Oauth2 to protect my Api, i need to get a new access token before any http requets if the previous access token has expired.

I didn't used event listener much until now.

Here what i did for now (Please let me know if it is correct) :

ApplicationController.js :

app.controller('ApplicationController', function($rootScope, $scope, $localStorage, AuthService){

    // Listening event apiRequested
    $scope.$on('event:apiRequested', function(e) {

        AuthService.token();
        // Restore the access_token in case it has changed
        access_token = $localStorage.getObject('access_token');

    });

})

UserController.js :

$rootScope.$broadcast('event:apiRequested');

// Get Users around
return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
    return response;
});

First thing i'm not sure about ... Does $http is processed if the event already executed entirely?

So since i'm not sure, i'm thinking about adding a callback.

Here the idea :

$rootScope.$broadcast('event:apiRequested', function(response){

    if(response){

        // Get Users around
        return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
            return response;
        });

    }

});

Please let me know if it is possible to do that or should i use something else than event listener for that case.

1 Answer 1

4

Why don't you use interceptors that is done to intercept HTTP request ? In your case, you shall add this very specific behaviour into the "request" part.

See an interceptor exemple bellow:

var $myService; // Add a constant that store the service
    $httpProvider.interceptors.push(['$location', '$injector', '$q', function($location, $injector, $q) {
                             return {

                               'request' : function(config){
                                 console.log("intercept request", config.url,config)
                                 // Your token shall be retreive in this part
                                 return config
                               },
                               'response' : function(config){
                                 $myService= $myService|| $injector.get('$myService'); // inject the service manually if constant is undefined
                                 console.log("intercept response", config)
                                 // Your token shall be retreive in this part
                                 return config
                               },
                                 'responseError': function(rejection) {

                                     console.log("responseError intercepted" , rejection);
                                      if (rejection.status === 403) {

                                         return $q.reject(rejection);

                                     } else if (rejection.status === 423) {



                                         return $q.reject(rejection);
                                     }else
                                          return $q.reject(rejection);
                                 }
                             };
                         }]);

Interceptors shall be defined into .config(["$httpProvider", function($httpProvider)

Sign up to request clarification or add additional context in comments.

7 Comments

I didn't know about interceptors. thank you, i'll dig into that and give my feed back.
sure, I added a complement to define where you shall define interceptors
That is working perfectly, i read about promises/deferred first since i did't know the concept. docs.angularjs.org/api/ng/service/$q . May i ask how would you use a service in it? I tried to register the service in the .config but it can't find my AuthService.
I update my code for explantion on how add a service
can't tell you ;) don't know... You have to try héhé
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.