0

나는 angularjs와 함께 달리는 django-rest-framework를 얻으려고하고있다. 인증을 수행하기 위해 나는 불행하게도 djangoAuth.js이 변수 '요청'내에서 감가 상각 기능의 성공을 사용 django-rest-authangular-django-registration-auth번역 기능 함수로 성공

을 발견하고 나는 다음과 같은 오류 메시지가 얻을 : I가 시도

TypeError: $http(...).success is not a function

을 then 함수를 사용하여 코드를 다시 작성하지만 오류 콜백으로 끝납니다. 무엇을 바꾸어야합니까? 당신의 도움을 주셔서 감사합니다.

djangoAuth.js - orginal 한

auth 
    .service('djangoAuth', function djangoAuth($q, $http, $cookies, $rootScope) { 
    // AngularJS will instantiate a singleton by calling "new" on this function 
    var service = { 
     /* START CUSTOMIZATION HERE */ 
     // Change this to point to your Django REST Auth API 
     // e.g. /api/rest-auth (DO NOT INCLUDE ENDING SLASH) 
     'API_URL': 'localhost:8000/rest-auth', 
     // Set use_session to true to use Django sessions to store security token. 
     // Set use_session to false to store the security token locally and transmit it as a custom header. 
     'use_session': true, 
     /* END OF CUSTOMIZATION */ 
     'authenticated': null, 
     'authPromise': null, 
     'request': function(args) { 
      // Let's retrieve the token from the cookie, if available 
      if($cookies.token){ 
       $http.defaults.headers.common.Authorization = 'Token ' + $cookies.token; 
      } 
      // Continue 
      params = args.params || {} 
      args = args || {}; 
      var deferred = $q.defer(), 
       url = this.API_URL + args.url, 
       method = args.method || "GET", 
       params = params, 
       data = args.data || {}; 
      // Fire the request, as configured. 
      $http({ 
       url: url, 
       withCredentials: this.use_session, 
       method: method.toUpperCase(), 
       headers: {'X-CSRFToken': $cookies['csrftoken']}, 
       params: params, 
       data: data 
      }) 
      .success(angular.bind(this,function(data, status, headers, config) { 
       deferred.resolve(data, status); 
      })) 
      .error(angular.bind(this,function(data, status, headers, config) { 
       console.log("error syncing with: " + url); 
       // Set request status 
       if(data){ 
        data.status = status; 
       } 
       if(status == 0){ 
        if(data == ""){ 
         data = {}; 
         data['status'] = 0; 
         data['non_field_errors'] = ["Could not connect. Please try again."]; 
        } 
        // or if the data is null, then there was a timeout. 
        if(data == null){ 
         // Inject a non field error alerting the user 
         // that there's been a timeout error. 
         data = {}; 
         data['status'] = 0; 
         data['non_field_errors'] = ["Server timed out. Please try again."]; 
        } 
       } 
       deferred.reject(data, status, headers, config); 
      })); 
      return deferred.promise; 
     }, 
     'register': function(username,password1,password2,email,more){ 
      var data = { 
       'username':username, 
       'password1':password1, 
       'password2':password2, 
       'email':email 
      } 
      data = angular.extend(data,more); 
      return this.request({ 
       'method': "POST", 
       'url': "/registration/", 
       'data' :data 
      }); 
     }, 
     'login': function(username,password){ 
      var djangoAuth = this; 
      return this.request({ 
       'method': "POST", 
       'url': "/login/", 
       'data':{ 
        'username':username, 
        'password':password 
       } 
      }).then(function(data){ 
       if(!djangoAuth.use_session){ 
        $http.defaults.headers.common.Authorization = 'Token ' + data.key; 
        $cookies.token = data.key; 
       } 
       djangoAuth.authenticated = true; 
       $rootScope.$broadcast("djangoAuth.logged_in", data); 
      }); 
     }, 
     'logout': function(){ 
      var djangoAuth = this; 
      return this.request({ 
       'method': "POST", 
       'url': "/logout/" 
      }).then(function(data){ 
       delete $http.defaults.headers.common.Authorization; 
       delete $cookies.token; 
       djangoAuth.authenticated = false; 
       $rootScope.$broadcast("djangoAuth.logged_out"); 
      }); 
     }, 
     'changePassword': function(password1,password2){ 
      return this.request({ 
       'method': "POST", 
       'url': "/password/change/", 
       'data':{ 
        'new_password1':password1, 
        'new_password2':password2 
       } 
      }); 
     }, 
     'resetPassword': function(email){ 
      return this.request({ 
       'method': "POST", 
       'url': "/password/reset/", 
       'data':{ 
        'email':email 
       } 
      }); 
     }, 
     'profile': function(){ 
      return this.request({ 
       'method': "GET", 
       'url': "/user/" 
      }); 
     }, 
     'updateProfile': function(data){ 
      return this.request({ 
       'method': "PATCH", 
       'url': "/user/", 
       'data':data 
      }); 
     }, 
     'verify': function(key){ 
      return this.request({ 
       'method': "POST", 
       'url': "/registration/verify-email/", 
       'data': {'key': key} 
      });    
     }, 
     'confirmReset': function(uid,token,password1,password2){ 
      return this.request({ 
       'method': "POST", 
       'url': "/password/reset/confirm/", 
       'data':{ 
        'uid': uid, 
        'token': token, 
        'new_password1':password1, 
        'new_password2':password2 
       } 
      }); 
     }, 
     'authenticationStatus': function(restrict, force){ 
      // Set restrict to true to reject the promise if not logged in 
      // Set to false or omit to resolve when status is known 
      // Set force to true to ignore stored value and query API 
      restrict = restrict || false; 
      force = force || false; 
      if(this.authPromise == null || force){ 
       this.authPromise = this.request({ 
        'method': "GET", 
        'url': "/user/" 
       }) 
      } 
      var da = this; 
      var getAuthStatus = $q.defer(); 
      if(this.authenticated != null && !force){ 
       // We have a stored value which means we can pass it back right away. 
       if(this.authenticated == false && restrict){ 
        getAuthStatus.reject("User is not logged in."); 
       }else{ 
        getAuthStatus.resolve(); 
       } 
      }else{ 
       // There isn't a stored value, or we're forcing a request back to 
       // the API to get the authentication status. 
       this.authPromise.then(function(){ 
        da.authenticated = true; 
        getAuthStatus.resolve(); 
       },function(){ 
        da.authenticated = false; 
        if(restrict){ 
         getAuthStatus.reject("User is not logged in."); 
        }else{ 
         getAuthStatus.resolve(); 
        } 
       }); 
      } 
      return getAuthStatus.promise; 
     }, 
     'initialize': function(url, sessions){ 
      this.API_URL = url; 
      this.use_session = sessions; 
      return this.authenticationStatus(); 
     } 

    } 
    return service; 
    }); 

djangoAuth.js - 당신이 $ HTTP는 (그것은 이미 약속을 반환 사용할 때 내 시험

auth 
.service('djangoAuth', function djangoAuth($q, $http, $cookies, $rootScope) { 
    // AngularJS will instantiate a singleton by calling "new" on this function 
    var service = { 
     /* START CUSTOMIZATION HERE */ 
     // Change this to point to your Django REST Auth API 
     // e.g. /api/rest-auth (DO NOT INCLUDE ENDING SLASH) 
     'API_URL': 'localhost:8000/rest-auth', 
     // Set use_session to true to use Django sessions to store security token. 
     // Set use_session to false to store the security token locally and transmit it as a custom header. 
     'use_session': true, 
     /* END OF CUSTOMIZATION */ 
     'authenticated': null, 
     'authPromise': null, 
     'request': function(args) { 
      // Let's retrieve the token from the cookie, if available 
      if($cookies.token){ 
       $http.defaults.headers.common.Authorization = 'Token ' + $cookies.token; 
      } 
      // Continue 
      params = args.params || {} 
      args = args || {}; 
      var deferred = $q.defer(), 
       url = this.API_URL + args.url, 
       method = args.method || "GET", 
       params = params, 
       data = args.data || {}; 
      // Fire the request, as configured. 
      $http({ 
       url: url, 
       withCredentials: this.use_session, 
       method: method.toUpperCase(), 
       headers: {'X-CSRFToken': $cookies['csrftoken']}, 
       params: params, 
       data: data 
      }).then(
       //Success Callback 
       angular.bind(this,function(data, status, headers, config) { 
        deferred.resolve(data, status); 
       }), 
       //Error Callback 
       angular.bind(this,function(data, status, headers, config) { 
        console.log("error syncing with: " + url); 
        // Set request status 
        if(data){ 
         data.status = status; 
        } 
        if(status == 0){ 
         if(data == ""){ 
          data = {}; 
          data['status'] = 0; 
          data['non_field_errors'] = ["Could not connect. Please try again."]; 
         } 
         // or if the data is null, then there was a timeout. 
         if(data == null){ 
          // Inject a non field error alerting the user 
          // that there's been a timeout error. 
          data = {}; 
          data['status'] = 0; 
          data['non_field_errors'] = ["Server timed out. Please try again."]; 
         } 
        } 
        deferred.reject(data, status, headers, config);     
       }) 
      ); 

      return deferred.promise; 
     }, 
     'register': function(username,password1,password2,email,more){ 
      var data = { 
       'username':username, 
       'password1':password1, 
       'password2':password2, 
       'email':email 
      } 
      data = angular.extend(data,more); 
      return this.request({ 
       'method': "POST", 
       'url': "/registration/", 
       'data' :data 
      }); 
     }, 
     'login': function(username,password){ 
      var djangoAuth = this; 
      return this.request({ 
       'method': "POST", 
       'url': "/login/", 
       'data':{ 
        'username':username, 
        'password':password 
       } 
      }).then(function(data){ 
       if(!djangoAuth.use_session){ 
        $http.defaults.headers.common.Authorization = 'Token ' + data.key; 
        $cookies.token = data.key; 
       } 
       djangoAuth.authenticated = true; 
       $rootScope.$broadcast("djangoAuth.logged_in", data); 
      }); 
     }, 
     'logout': function(){ 
      var djangoAuth = this; 
      return this.request({ 
       'method': "POST", 
       'url': "/logout/" 
      }).then(function(data){ 
       delete $http.defaults.headers.common.Authorization; 
       delete $cookies.token; 
       djangoAuth.authenticated = false; 
       $rootScope.$broadcast("djangoAuth.logged_out"); 
      }); 
     }, 
     'changePassword': function(password1,password2){ 
      return this.request({ 
       'method': "POST", 
       'url': "/password/change/", 
       'data':{ 
        'new_password1':password1, 
        'new_password2':password2 
       } 
      }); 
     }, 
     'resetPassword': function(email){ 
      return this.request({ 
       'method': "POST", 
       'url': "/password/reset/", 
       'data':{ 
        'email':email 
       } 
      }); 
     }, 
     'profile': function(){ 
      return this.request({ 
       'method': "GET", 
       'url': "/user/" 
      }); 
     }, 
     'updateProfile': function(data){ 
      return this.request({ 
       'method': "PATCH", 
       'url': "/user/", 
       'data':data 
      }); 
     }, 
     'verify': function(key){ 
      return this.request({ 
       'method': "POST", 
       'url': "/registration/verify-email/", 
       'data': {'key': key} 
      });    
     }, 
     'confirmReset': function(uid,token,password1,password2){ 
      return this.request({ 
       'method': "POST", 
       'url': "/password/reset/confirm/", 
       'data':{ 
        'uid': uid, 
        'token': token, 
        'new_password1':password1, 
        'new_password2':password2 
       } 
      }); 
     }, 
     'authenticationStatus': function(restrict, force){ 
      // Set restrict to true to reject the promise if not logged in 
      // Set to false or omit to resolve when status is known 
      // Set force to true to ignore stored value and query API 
      restrict = restrict || false; 
      force = force || false; 
      if(this.authPromise == null || force){ 
       this.authPromise = this.request({ 
        'method': "GET", 
        'url': "/user/" 
       }) 
      } 
      var da = this; 
      var getAuthStatus = $q.defer(); 
      if(this.authenticated != null && !force){ 
       // We have a stored value which means we can pass it back right away. 
       if(this.authenticated == false && restrict){ 
        getAuthStatus.reject("User is not logged in."); 
       }else{ 
        getAuthStatus.resolve(); 
       } 
      }else{ 
       // There isn't a stored value, or we're forcing a request back to 
       // the API to get the authentication status. 
       this.authPromise.then(function(){ 
        da.authenticated = true; 
        getAuthStatus.resolve(); 
       },function(){ 
        da.authenticated = false; 
        if(restrict){ 
         getAuthStatus.reject("User is not logged in."); 
        }else{ 
         getAuthStatus.resolve(); 
        } 
       }); 
      } 
      return getAuthStatus.promise; 
     }, 
     'initialize': function(url, sessions){ 
      this.API_URL = url; 
      this.use_session = sessions; 
      return this.authenticationStatus(); 
     } 

    } 
    return service; 
    }); 

답변

2

첫째, 당신은 연기 사용할 필요가 없습니다). 그렇다면 angular.bind()를 사용할 필요가 없습니다.

deferred.resolve()(Second one is a callback)에 두 개의 인수를 전달할 수도 없습니다.

당신이 필요로 $http에서 결과를 반환하는 것입니다 :

'request': function(args) { 
     // Let's retrieve the token from the cookie, if available 
     if($cookies.token){ 
      $http.defaults.headers.common.Authorization = 'Token ' + $cookies.token; 
     } 
     // Continue 
     params = args.params || {} 
     args = args || {}; 
     var url = this.API_URL + args.url, 
      method = args.method || "GET", 
      params = params, 
      data = args.data || {}; 
     // Fire the request, as configured. 
     return $http({ 
      url: url, 
      withCredentials: this.use_session, 
      method: method.toUpperCase(), 
      headers: {'X-CSRFToken': $cookies['csrftoken']}, 
      params: params, 
      data: data 
     }).catch(
      //Use catch for error handling 
      //Error Callback 
      function(data, status, headers, config) { 
       console.log("error syncing with: " + url); 
       // Set request status 
       if(data){ 
        data.status = status; 
       } 
       if(status == 0){ 
        if(data == ""){ 
         data = {}; 
         data['status'] = 0; 
         data['non_field_errors'] = ["Could not connect. Please try again."]; 
        } 
        // or if the data is null, then there was a timeout. 
        if(data == null){ 
         // Inject a non field error alerting the user 
         // that there's been a timeout error. 
         data = {}; 
         data['status'] = 0; 
         data['non_field_errors'] = ["Server timed out. Please try again."]; 
        } 
       }    
      }) 
     }