Ember.js使用Ember Simple Auth从代码中验证用户身份

Del*_*ynx 1 ember.js

我试图让用户通过代码进行身份验证.我有一个登录表单,用户可以登录,它就像魅力一样.

但是,当新用户注册并保存表单时,我希望在表单有效时在后台登录.

我怎样才能做到这一点?

我在API中找到了以下authenticate()方法:

对于没有身份验证路由的应用程序(例如,因为它打开了一个新窗口来处理身份验证),这是要覆盖的方法,例如:

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  actions: {
    authenticateSession: function() {
      this.get('session').authenticate('app:authenticators:custom', {});
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

有谁知道如何实现这一点.当我打电话给this.get('session').authenticate()哪里放置识别和密码数据?

任何提示或建议都非常感谢!

编辑:也许可以使用与登录相同的身份验证器而不是app:authenticators:custom示例中的身份验证器?

mar*_*oow 5

要通过常规登录(可能使用EmberSimpleAuth的默认验证器)或在成功注册后自动验证会话,您将为每种情况使用不同的验证器.对于登录,您将使用带有LoginControllerMixin等的默认验证器.对于自动情况,您将使用自定义验证器.有关使用自定义身份验证器的文档,请参阅EmberSimpleAuth的github repo中的示例和API文档:http://ember-simple-auth.simplabs.com/api.html .

基本上你要做的是:

App.AutoAuthenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend({
  authenticate: function(credentials) {
    if (!Ember.isEmpty(credentials.access_token)) {
      return Ember.RSVP.resolve(credentials);
    } else {
      return this._super(credentials);
    }
  }
});
Ember.Application.initializer({
  name: 'authentication',
  initialize: function(container, application) {
    container.register('app:authenticators:custom', App.AutoAuthenticator);
    Ember.SimpleAuth.setup(container, application);
  }
});
this.get('session').authenticate('app:auto-authenticator', { access_token: 'secret token!' })
Run Code Online (Sandbox Code Playgroud)

我实际上没有测试过 - 请将此视为伪代码;)