Since I use Oauth2 to protect my Api, I need to get a new access token before any HTTP requests if the previous access token has expired.
I still haven't used an event listener.
Here is what I did now (Please let me know if this is correct):
ApplicationController.js :
app.controller('ApplicationController', function($rootScope, $scope, $localStorage, AuthService){
$scope.$on('event:apiRequested', function(e) {
AuthService.token();
access_token = $localStorage.getObject('access_token');
});
})
UserController.js:
$rootScope.$broadcast('event:apiRequested');
return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
return response;
});
The first thing I'm not sure about ... does $ http handle if the event is already fully completed?
So since I'm not sure, I'm thinking of adding a callback.
Here is an idea:
$rootScope.$broadcast('event:apiRequested', function(response){
if(response){
return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
return response;
});
}
});
Please let me know if this is possible, or should I use something else besides an event listener for this case.
source
share