简体   繁体   English

Ember.js使用Ember Simple Auth从代码对用户进行身份验证

[英]Ember.js Authenticate user from code with Ember Simple Auth

I am trying to have a user authenticated from code. 我试图让用户通过代码进行身份验证。 I have a login form where users can login, and it works like charm. 我有一个登录表单,用户可以在其中登录,它的工作方式就像魅力。

However, when a new user sign up, and saves the form I would like to login them in in background when the form is valid. 但是,当新用户注册并保存表单时,我想在表单有效时在后台登录他们。

How can I do that? 我怎样才能做到这一点?

I found the following in the API at the authenticate() method: 我在API的authenticate()方法中找到了以下内容:

For an application that works without an authentication route (eg because it opens a new window to handle authentication there), this is the method to override, eg: 对于没有身份验证路由的应用程序(例如,因为它在其中打开了一个新窗口来处理身份验证),这是一种覆盖方法,例如:

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

Do anyone know how to implement this. 有谁知道如何实现这一目标。 When I call this.get('session').authenticate() where to put the identication and password data? 当我调用this.get('session').authenticate() ,将this.get('session').authenticate()和密码数据放在哪里?

Any hints or suggestions are highly appreciated! 任何提示或建议都将受到高度赞赏!

Edit: maybe it is possible to use the same authenticator used for logging in instead of app:authenticators:custom as in the example? 编辑:也许可以使用与登录示例相同的身份验证器代替示例中的app:authenticators:custom

To authenticate the session either via a regular login (probably with EmberSimpleAuth's default authenticator) or automatically after successful signup, you'd use a different authenticator for each case. 要通过常规登录(可能使用EmberSimpleAuth的默认身份验证器)或成功注册后自动对会话进行身份验证,您需要为每种情况使用不同的身份验证器。 For the login you'd use the default authenticator with the LoginControllerMixin etc. and for the automatic case you'd use a custom authenticator. 对于登录,您将使用默认的身份验证器和LoginControllerMixin等。对于自动情况,您将使用自定义身份验证器。 For docs on using custom authenticators see the examples in EmberSimpleAuth's github repo an the API docs: http://ember-simple-auth.simplabs.com/api.html . 有关使用自定义身份验证器的文档,请参见EmberSimpleAuth的github存储库中的API文档示例: http ://ember-simple-auth.simplabs.com/api.html。

Basically what you'd do is: 基本上,您要做的是:

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!' })

I did not actually test that - please regard this as pseudo code ;) 我没有实际测试过-请将此视为伪代码;)

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

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