使用Rails和设计的Ember.js基于会话cookie的身份验证 [英] Ember.js session cookie based authentication with Rails and devise

查看:169
本文介绍了使用Rails和设计的Ember.js基于会话cookie的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的Ember.js应用程序身份验证,使用rails,devise和基于cookie的会话来满足3个目标。

I'm looking to satisfy 3 goals with my Ember.js app authentication using rails, devise and a cookie based session.


  1. 如果未登录,则重定向到#/ sessions / new

  2. 始终在应用程序模板中显示当前用户的信息。

  3. 如果用户登录,直接进入#/ some / route 。当前用户应该加载加载。

  1. Redirected to #/sessions/new if they're not logged in.
  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. The current user should be loaded on load.

我看过这些embercast视频:客户端认证第1部分& 客户端验证第2部分。他们有点过时,但有帮助。

I've watched these embercast videos: Client-side Authentication Part 1 & Client-side Authentication Part 2. They're a little out of date but helpful.

但仍然无法完全解决。任何人都有完整的Rails 4,Devise,Emberjs 1.0.0示例?

But still can't full solution. Anyone have full Rails 4, Devise, Emberjs 1.0.0 example?

最大的问题是有一个策略来加载当前用户的页面加载并设置当前用户

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.

现在这是我的策略:


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.

我收集了Derick的一些意见,Backbone.Marionette的作者。虽然这些是用于Backbone而不是Ember.js,但客户端身份验证的情况是相同的。

I've collected some opinions from Derick, the author of Backbone.Marionette. Though these are for Backbone but not Ember.js, the situation of client side authentication is same.


我觉得尝试不必要并使Backbone / Marionette处理授权网站的身份验证和重新加载。一旦他们登录,将它们重定向到服务器处理的其他URL,并让服务器将所有需要的东西发送给身份验证用户。 https://stackoverflow.com/a/18151935

另外来自Derick的报价:

Another quote from Derick as well:


对。还有很多情况下,我只是说出不要单页面应用程序,以及。登录屏幕是最大的例子。在过去几年的所有客户中,他们都问我:嘿,我有这个问题。我正在尝试让我的登录屏幕从服务器提供给我当前的用户信息,并重做所有这些东西在屏幕上,而不刷新一切。我的答案每一次都是,不要这样做。< a href =http://javascriptjabber.com/056-jsj-marionette-js-with-derick-bailey/ =nofollow noreferrer> http://javascriptjabber.com/056-jsj-marionette-js- with-derick-bailey /

还要考虑其他情况,说Gmail,你不会顺利过渡在Gmail登录页面上点击登录按钮后,将重定向到相当大的数据加载:)

Also think about other cases, say Gmail. You won't get a smooth transition after click "Sign in" button on Gmail's sign in page. There will be redirect with rather big data loading as well :)

从用户的角度来看,他们不会说Gmail不是很好,只是因为登录后有一个重定向。所有的签名/注册比日常邮件操作要少得多。

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.

所以我的建议是在用户会话更改后重新加载所有资源,让Rails和Devise在传统的fashi中执行这些脏工作on。

So my suggestion is, reload all resources after user session changed. Let Rails and Devise do these dirty jobs in traditional fashion.

这篇关于使用Rails和设计的Ember.js基于会话cookie的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆