angularjs - Redirect to an external URL -
i'm developing web application using angularjs authentication page doesn't use angularjs, uses html .
so when user disconnected must redirect him authentication page,so made interceptor executed before request , use service application validate if user connected , if he's not must redirected authentication page .
this interceptor code :
$httpprovider.interceptors .push(function($q, $injector) { return { 'request' : function(request) { var $http = $injector.get('$http'); /* calls service application verify if user connected */ $http .get("sessionvalidator") .success( function(data) { if (data.result == 'sessionnull' || data.role != 'role_user') { window.location.href = '/authenticationpage'; } }); return request; }, }; });
my problem there loop generated when call service(because it's request , interceptor executed again).any solutions problem or there way this.
so description , comments set service intercepts both request , response. created generic one, see code below example plunker. can expand code quite bit achieve want.
var app = angular.module("myapp", []); app.service("interceptorservice", function($http, $q) { // service has post , get, post documented return { get: function(url, parameters){ var deferred = $q.defer(); // promise return $http.get(url, {params: parameters}).then(function(successresponse){ // check null session or role being not user, , redirect if response approved if (successresponse.data.result == 'sessionnull' || successresponse.data.role != 'role_user'){ window.location.href = '/authenticationpage'; // should rejected because don't want process response if person not user, person being redirected anyways deferred.reject(successresponse); } else{ // since successful, can resolve deferred.resolve(successresponse); } }, function(failureresponse){ console.log(failureresponse); deferred.reject(failureresponse); }); return deferred.promise; }, post: function(url, parameters){ var deferred = $q.defer(); $http.post(url, parameters).then(function(successresponse){ console.log(successresponse); deferred.resolve(successresponse); }, function(failureresponse){ console.log(failureresponse); deferred.reject(failureresponse); }); return deferred.promise; } } }) app.controller('myctrl', ['$scope', 'interceptorservice', function($scope, interceptorservice){ var url = 'http://api.sba.gov/geodata/city_county_links_for_state_of/tx.xml'; interceptorservice.get(url).then(function(response){ $scope.result = "http call succeeded"; }, function(reason){ $scope.result = "http call failed"; }); }]);
Comments
Post a Comment