简体   繁体   English

Ember.js基于会话cookie的Rails和设计认证

[英]Ember.js session cookie based authentication with Rails and devise

I'm looking to satisfy 3 goals with my Ember.js app authentication using rails, devise and a cookie based session. 我希望使用rails,devise和基于cookie的会话,使用我的Ember.js app身份验证来满足3个目标。

  1. Redirected to #/sessions/new if they're not logged in. 如果他们没有登录,则重定向到#/sessions/new
  2. Always show the current user's information in the application template. 始终在应用程序模板中显示当前用户的信息。
  3. If the user is logged in and they go to #/some/route directly. 如果用户已登录并且他们直接转到#/some/route The current user should be loaded on load. 应该在加载时加载当前用户。

I've watched these embercast videos: Client-side Authentication Part 1 & Client-side Authentication Part 2 . 我已经看过这些embercast视频: 客户端身份验证第1部分客户端身份验证第2部分 They're a little out of date but helpful. 他们有点过时但很有帮助。

But still can't full solution. 但还是不能完全解决。 Anyone have full Rails 4, Devise, Emberjs 1.0.0 example? 任何人都有完整的Rails 4,Devise,Emberjs 1.0.0的例子?

Biggest problem is having a strategy to load the current user on page load and setting the current user when the sign in form is submitted. 最大的问题是有一个策略在页面加载时加载当前用户并在提交登录表单时设置当前用户。

Right now this is my strategy: 现在这是我的策略:


App.User = Em.Object.extend();

App.User.reopenClass({
  current: function() {
    return Ember.$.getJSON("/users/current").then(function(data) {
      return data
    })
  }
});

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return App.User.current();
  }
});

App.SessionsNewController = Ember.ObjectController.extend({

  actions: {
    save: function(data) {
      var self = this, data = this.getProperties('email', 'password');

      $.post("/sessions", { session: data }).always(function(response, status, data) {
        if (status == "success") {
          self.transitionToRoute('index');
        } else {
          self.set('errorMessage', data);
        }
      })

    },
  }

});

I would not say this is not doable. 我不会说这不可行。 But you will do lots of extra and unnecessary works to get the authentication working, which can all be done with a simple page redirect. 但是你会做很多额外的和不必要的工作来使身份验证工作,这可以通过一个简单的页面重定向来完成。

I've collected some opinions from Derick, the author of Backbone.Marionette. 我收集了Backbone.Marionette的作者Derick的一些意见。 Though these are for Backbone but not Ember.js, the situation of client side authentication is same. 虽然这些是针对Backbone而不是Ember.js,但客户端身份验证的情况是相同的。

I find it painful and unnecessary to try and make Backbone/Marionette handle the authentication and re-loading of the authorized site stuff. 我发现使用Backbone / Marionette处理认证和重新加载授权网站的东西是痛苦的,没有必要。 Once they log in, redirect them to a different URL that the server handles, and have the server send down all the stuff that they need, as an authenticated user. 登录后,将它们重定向到服务器处理的不同URL,并让服务器作为经过身份验证的用户发送所需的所有内容。 https://stackoverflow.com/a/18151935 https://stackoverflow.com/a/18151935

Another quote from Derick as well: 德里克的另一个引用:

Right. 对。 And there's a lot of cases where I just flat out say, “Do not do single-page applications,” as well. 还有很多案例,我只是说,“不要做单页应用程序”。 And a login screen is the biggest example of that. 登录屏幕就是最好的例子。 In all of the clients that I've had in the last couple of years, they've all asked me, “Hey, I'm having this problem. 在过去几年我所有的客户中,他们都问过我,“嘿,我遇到了这个问题。 I'm trying to get my login screen to give me the current user information back from the server and redo all of this stuff on the screen without refreshing everything.” My answer every single time is, “Don't do that." http://javascriptjabber.com/056-jsj-marionette-js-with-derick-bailey/ 我正试图让我的登录界面从服务器返回当前的用户信息并在屏幕上重做所有这些内容而不刷新所有内容。“我的答案每次都是,”不要这样做。“ http ://javascriptjabber.com/056-jsj-marionette-js-with-derick-bailey/

Also think about other cases, say Gmail. 还要考虑其他情况,比如Gmail。 You won't get a smooth transition after click "Sign in" button on Gmail's sign in page. 点击Gmail登录页面上的“登录”按钮后,您将无法顺利转换。 There will be redirect with rather big data loading as well :) 将有重定向与相当大的数据加载:)

From users' perspective, they won't say Gmail is not great just because there is a redirect after signing in. After all signing/sign up is much much less frequent than daily mail operations. 从用户的角度来看,他们不会说Gmail只是因为登录后有重定向而不是很好。毕竟签名/注册比日常邮件操作少得多。

So my suggestion is, reload all resources after user session changed. 所以我的建议是,在用户会话更改后重新加载所有资源。 Let Rails and Devise do these dirty jobs in traditional fashion. 让Rails和Devise以传统方式做这些肮脏的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM