2013-11-01 32 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'); 
    }); 
} 
}]);