2013-11-01 31 views
1

在AuthService中可以處理響應並使用SessionService設置會話嗎?Angularjs資源處理響應和自定義方法

我現在在控制器中使用成功回調,但我仍然是Angularjs的新手,並試圖瞭解如何自定義資源。

我使用Angularjs 1.1.5

app.factory('AuthService', ['$resource', 'SessionService', 
function($resource, SessionService) { 

    return $resource(
     '/api/v1/auth/:action', 
     {action:'@action'}, 
     { 
      login: { 
       method:'POST', 
       params: { 
        action: 'login' 
       } 
      }, 
      logout: { 
       method:'GET', 
       params: { 
        action: 'logout' 
       } 
      } 
     } 
    ); 
} 
]); 

app.controller('LoginCtrl', ['$scope', '$location', 'AuthService', 'SessionService' function LoginCtrl($scope, $location, AuthService, SessionService) { 

$scope.credentials = { email: "", password: ""}; 

$scope.login = function() { 
    AuthService.login($scope.credentials).success(function() { 
     SessionService.set('authenticated', true); 
     $location.path('/home'); 
    }); 
} 
}]); 

app.factory("SessionService", function() { 
return { 
    get: function(key) { 
    return sessionStorage.getItem(key); 
    }, 
    set: function(key, val) { 
    return sessionStorage.setItem(key, val); 
    }, 
    unset: function(key) { 
    return sessionStorage.removeItem(key); 
    } 
} 
}); 
+0

不明白你的問題是什麼意思。你能發佈控制器的代碼嗎? – fabrizioM

回答

1

我不知道,$資源是適當的抽象,在這裏使用。我認爲使用純$ http實現AuthService會簡單得多。只需將登錄和註銷作爲普通功能來實現,那麼你可以隨意做任何你想做的事情。您還應該確保您返回承諾,如果登錄後需要執行其他操作,那麼調用login()或logout()的人仍然可以執行.then()。這裏有一個例子:

app.factory('AuthService', ['$http', '$location' 'SessionService', 
function($http, $location, SessionService) { 
    var baseUrl = '/api/v1/auth/'; 

    function onLoginSuccess(data){ 
      SessionService.set('authenticated', true); 
      $location.path('/home'); 
    } 

    function onLoginFailure(error){ 
     SessionService.unset('authenticated'); 
     $location.path('/login'); 
    } 


    return { 
     login: function(credentials){ 
      return $http.post(baseUrl+'login', credential).then(onLoginSuccess, onLoginFailure); 
     } 

     logout: function(){ 
      return $http.get(baseUrl+'logout'); 
     } 
    }; 

app.controller('LoginCtrl', ['$scope', 'AuthService', function LoginCtrl($scope, AuthService) { 
    $scope.credentials = { email: "", password: ""}; 
    $scope.login = function() { 
     AuthService.login($scope.credentials);  
    } 
}]); 

app.factory("SessionService", function() { 
return { 
    get: function(key) { 
    return sessionStorage.getItem(key); 
    }, 
    set: function(key, val) { 
    return sessionStorage.setItem(key, val); 
    }, 
    unset: function(key) { 
    return sessionStorage.removeItem(key); 
    } 
} 
}); 
+0

這就是我現在正在做的事情。我可以擁有與資源相同的邏輯嗎? – Sabourinov

+0

在你的案例中沒有真正的價值或理由使用資源。是否有一個特別的原因,你真的想使用$資源? – dtabuenc

+0

沒有特別的理由。這是爲了學習的目的。 – Sabourinov

0

的路徑/ API/V1/auth /中登錄您的服務器端腳本應返回結果,表明登錄成功地被授予與否。

例如,如果登錄被授予,則/api/v1/auth/login返回成功響應200。 如果登錄被拒絕,則/api/v1/auth/login返回錯誤的請求響應(失敗)400

如果有這個條件,因爲跟着你的代碼應該寫,

app.controller('LoginCtrl', ['$scope', '$location', 'AuthService', 'SessionService' 
function LoginCtrl($scope, $location, AuthService, SessionService) { 
    $scope.credentials = { email: "", password: ""}; 
    $scope.login = function() { 
    AuthService.login($scope.credentials, function(data) { //Success callback 
     SessionService.set('authenticated', true); 
     $location.path('/home'); 
    }, function(error){ //Failure callback 
     SessionService.unset('authenticated'); 
     $location.path('/login'); 
    }); 
} 
}]);