Custom authenticator with Ember simple auth + Ember CLI

荒凉一梦 提交于 2019-12-13 14:23:28

问题


I'm trying to write a custom authenticator, similar to the one from this example in the docs. The goal is to be able to retrieve the currently logged in user via session.user.

I'm using Ember CLI, so in initializers/authentication.js I have

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

When I try to authenticate, I get the following error:

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

Any idea why?


回答1:


As of Simple Auth 0.6.4, you can now do something like:

index.html:

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

initializers/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);
  }
};



回答2:


You would need to do something like this:

  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

What I think might be happening is that you are registering the authenticator with the application and not the authenticator itself?




回答3:


The problem is that the AMD build does not currently automatically register the extension libraries' components (see https://github.com/simplabs/ember-simple-auth/issues/198). I'll change that in the next release and will probably also adopt the documentation to be more focussed on the AMD build instead of the browserified version. For the moment you'd have to run this in your initializer

container.register(
  'ember-simple-auth-authorizer:devise',
  Ember.SimpleAuth.Authorizers.Devise
);
container.register(
  'ember-simple-auth-authenticator:devise',
  Ember.SimpleAuth.Authenticators.Devise
);


来源:https://stackoverflow.com/questions/24319186/custom-authenticator-with-ember-simple-auth-ember-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!