3

我正在尝试编写一个自定义身份验证器,类似于文档中此示例中的身份验证器。目标是能够通过 检索当前登录的用户session.user

我正在使用 Ember CLI,所以initializers/authentication.js我有

import Ember from 'ember';

var customAuthenticator = Ember.SimpleAuth.Authenticators.Devise.extend({
  authenticate: function(credentials) {
    debugger;
  }
});

export default {
  name: 'authentication',

  initialize: function(container, application) {

    Ember.SimpleAuth.Session.reopen({
      user: function() {
        var userId = this.get('user_id');
        if (!Ember.isEmpty(userId)) {
          return container.lookup('store:main').find('user', userId);
        }
      }.property('userId')
    });

    // register the custom authenticator so the session can find it
    container.register('authenticator:custom', customAuthenticator);

    Ember.SimpleAuth.setup(container, application, {
      routeAfterAuthentication: 'landing-pages',
      authorizerFactory: 'ember-simple-auth-authorizer:devise'
    });
  }
};

当我尝试进行身份验证时,出现以下错误:

TypeError: Cannot read property 'authenticate' of undefined
at __exports__.default.Ember.ObjectProxy.extend.authenticate

知道为什么吗?

4

3 回答 3

5

从 Simple Auth 0.6.4 开始,您现在可以执行以下操作:

索引.html:

window.ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:devise',
  session: 'session:withCurrentUser'
};

初始化程序/customize-session.js:

import Ember from 'ember';
import Session from 'simple-auth/session';

var SessionWithCurrentUser = Session.extend({
  currentUser: function() {
    var userId = this.get('user_id');
    if (!Ember.isEmpty(userId)) {
      return this.container.lookup('store:main').find('user', userId);
    }
  }.property('user_id')
});

export default {
  name: 'customize-session',
  initialize: function(container) {
    container.register('session:withCurrentUser', SessionWithCurrentUser);
  }
};
于 2014-08-14T22:45:17.840 回答
1

你需要做这样的事情:

  Em.SimpleAuth.Authenticators.OAuth2.reopen
    serverTokenEndpoint: "http://myapp.com/token"
    authenticate: (credentials) ->
      new Em.RSVP.Promise (resolve, reject) =>
        data =
          grant_type: "password"
          username: credentials.identification
          password: credentials.password

        @makeRequest(data).then (response) =>
          # success call
        , (xhr, status, error) ->
          # fail call

我认为可能发生的是您正在向应用程序注册身份验证器而不是身份验证器本身?

于 2014-06-20T21:53:15.220 回答
0

问题是 AMD 构建当前不会自动注册扩展库的组件(请参阅https://github.com/simplabs/ember-simple-auth/issues/198)。我将在下一个版本中更改它,并且可能还会采用文档以更专注于 AMD 构建而不是浏览器化版本。目前,您必须在初始化程序中运行它

container.register(
  'ember-simple-auth-authorizer:devise',
  Ember.SimpleAuth.Authorizers.Devise
);
container.register(
  'ember-simple-auth-authenticator:devise',
  Ember.SimpleAuth.Authenticators.Devise
);
于 2014-06-20T07:29:24.517 回答