angular 차단기 요청마다 사용자 token 전달

1785 단어
angular.module('houseApp')
	.config(["$stateProvider", "$ionicConfigProvider","$httpProvider",function ($stateProvider, $ionicConfigProvider,$httpProvider) {
		// 
		$ionicConfigProvider.views.maxCache(0);
		// 
		$httpProvider.interceptors.push('sessionInteceptor');

})]);

  
/**
 *  
 * */
angular.module('houseApp')
	.factory('sessionInteceptor', ["WAP_CONFIG","$q","userInfoService",function(WAP_CONFIG,$q,userInfoService) {

		var myInterceptor = {};

		// (request configuration object) ,  promise 。
		myInterceptor.request = function(requestConfig){
			console.log("myInterceptor.request userInfoService.getUserKey(): " + userInfoService.getUserKey());
			// token, 
			if(requestConfig["data"] != "" && requestConfig["data"] != null && requestConfig["data"] != undefined ){
				requestConfig["data"]["token"] = userInfoService.getUserKey();
			}
			return requestConfig;
		};

		// (response object) ,  promise。
		myInterceptor.response = function(responseObject){
			// 999, 
			if(responseObject.data.status == 999){
				//window.location.href = "/wap/tmpl/member/login.html";
				console.log("responseObject.data.status == 999");
			}
			return responseObject;
		};

		myInterceptor.requestError  = function(rejectReason){
			var deferred = $q.defer();

			console.log("myInterceptor.requestError : " + responseObject);
			return deferred.promise;
		};

		myInterceptor.responseError = function(responseError){
			console.log("myInterceptor.responseError : " + responseObject);
			return {};
		};

		return myInterceptor;

	}]);

좋은 웹페이지 즐겨찾기