Capture HTTP 401 with Angular.js interceptor

前端 未结 4 1730
暗喜
暗喜 2020-11-27 17:00

I\'d like to implement authentication on a single page web app with Angular.js. The official Angular documentation recommends the using of interceptors:

$pro         


        
4条回答
  •  失恋的感觉
    2020-11-27 17:20

    in app config block:

    var interceptor = ['$rootScope', '$q', "Base64", function(scope, $q, Base64) {
      function success(response) {
        return response;
      }
    
      function error(response) {
        var status = response.status;
        if (status == 401) {
          //AuthFactory.clearUser();
          window.location = "/account/login?redirectUrl=" + Base64.encode(document.URL);
          return;
        }
        // otherwise
        return $q.reject(response);
      }
      return function(promise) {
        return promise.then(success, error);
      }
    }];
    

提交回复
热议问题