Ruby on rails 在AngularJS中使用JSON用户对象进行身份验证

Ruby on rails 在AngularJS中使用JSON用户对象进行身份验证,ruby-on-rails,json,angularjs,authentication,Ruby On Rails,Json,Angularjs,Authentication,所以我有一个关于身份验证的问题,一直在想其他人会如何处理这种情况。我目前正在运行一个基于Rails API的Angular应用程序 到目前为止,对于身份验证,我有一个表单,它向Rails端发送一个帖子,让用户登录,然后在成功后将其发送回Angular应用程序。设置cookie并登录用户后,我就可以访问user.json文件,其中包含人们可能期望的所有用户信息(Id、用户名、角色、权限等)。由于所有的验证都是在Rails上进行的,因此如果用户注销,则此信息将被删除。所以这两个州看起来是 已登录 {

所以我有一个关于身份验证的问题,一直在想其他人会如何处理这种情况。我目前正在运行一个基于Rails API的Angular应用程序

到目前为止,对于身份验证,我有一个表单,它向Rails端发送一个帖子,让用户登录,然后在成功后将其发送回Angular应用程序。设置cookie并登录用户后,我就可以访问user.json文件,其中包含人们可能期望的所有用户信息(Id、用户名、角色、权限等)。由于所有的验证都是在Rails上进行的,因此如果用户注销,则此信息将被删除。所以这两个州看起来是

已登录

{
id: 99384,
name: "Username",
url: "//www.test.com/profiles/Username",
timezone: null,
rights: [ ],
roles: [
"admin"
],
}
注销

{
error: "You need to login or join before continuing."
}

到目前为止,我已经看到了为Angular进行auth的数百万种不同方法,但似乎没有任何方法适合这种类型的方法。所以我的问题是,既然服务器正在处理所有的验证,那么有没有办法只检查他们的user.json文件是否为空(显示错误消息),以及是否将Angular应用程序发送到Rails登录页面?当我可以基于JSON文件时,是否真的需要处理cookie、令牌等

您已经在使用cookies-服务器正在设置它们。你所做的是一种相当标准的做事方式

要检查json文件,您可以在控制器中执行如下操作:

app.controller('AppControl', function($scope, $http, $location){

    // Get the JSON file.

    $http.get('/path/to/json/file')

    .then(response){

      if(response.data.error){
        // redirect to login
        $location.path('login');

      }

      else{

        $scope.user = response.data;
        // your app code here.

      }

    })

    .catch(function (error){
      // unable to reach the json file - handle this.
    });

});
当然,你真的应该把它转移到一个服务中,这样你就可以重复使用它,还可以缓存数据,而不是每次你改变路线/页面时都得到用户,但这给了你一个模糊的概念

编辑工厂示例:

.factory('User', function( $http ){

  // Create a user object - this is ultimately what the factory will return.
  // it's a singleton, so there will only ever by one instance of it.
  var user = {};

  // NOTE: I am assigning the "then" function of the login promise to 
  // "whenLoggedIn" - your controller code is then very easy to read.
  user.whenLoggedIn = $http.get('user.json')

    .then(function(response){

      // Check to see if there is an error.
      if (response.data.error !== undefined) {
        // You could be more thorough with this check to determine the 
        // correct action (examine the error)
        user.loggedIn = false;

      }

      else {
        // the user is logged in
        user.loggedIn = true;
        user.details = response.data;

        return user;

      }        
    }).then; // <-- make sure you understand why that .then is there.

  return user;

})
因为它在作用域上,所以我们可以在html中执行此操作:

<body ng-controller="ExampleController">
  <h1 ng-show="User.loggedIn == null">Logging in..</h1>
  <h1 ng-show="User.loggedIn == true">Logged in as {{ User.details.name }}</h1>
  <h1 ng-show="User.loggedIn == false">Not logged in</h1>
</body>

登录。。
以{{User.details.name}身份登录
未登录
这是一个工作的地方

注意以下几点:

  • 如果用户已经登录,那么在将来注入服务时,它不会再次检查该文件。您可以在服务上创建其他方法来重新检查文件,还可以注销用户、重新登录等。我将由您决定

  • 还有其他方法可以做到这一点-这只是一个可能的选择

  • 这可能是显而易见的,但总是值得一提的。您需要主要在服务器端处理身份验证和安全性。客户端只是用户体验,确保用户不会看到混乱或冲突的屏幕


非常感谢!所以据我所知,我并没有真正使用ngCookies来验证登录状态,因为我真的不需要?此外,您提到将此作为一项服务使用;这将如何影响这样一个事实:在某些页面上,我不关心用户是否登录,但在其他页面上,我需要检查身份验证?我是否将此服务注入UI路由器?有这样的例子吗?对不起,所有的问题…没问题-如果有帮助,请向上投票/接受:)。您没有使用
ngCookies
,不,您不需要-只需在服务器端这样做即可。对于服务部分,将服务器注入到需要身份验证的页面的控制器中。在执行任何操作之前检查用户,如果用户未填充,请执行重定向。尚无法升级投票:(但我会接受。最后一个问题,然后我就不在你的头发里了。工厂是如何建立的,关于你如何设置示例控制器?到目前为止,我刚刚建立了简单的工厂,用于提取JSON信息并在控制器中显示。另外,非常感谢你帮助新员工…用示例工厂进行了更新。这真的很简单。)简单的最低要求-但希望它能给你一个正确的起点。真是太棒了,Ed。非常感谢!
<body ng-controller="ExampleController">
  <h1 ng-show="User.loggedIn == null">Logging in..</h1>
  <h1 ng-show="User.loggedIn == true">Logged in as {{ User.details.name }}</h1>
  <h1 ng-show="User.loggedIn == false">Not logged in</h1>
</body>