Sam Selikoff
Sam Selikoff

Reputation: 12684

Custom authenticator with Ember simple auth + Ember CLI

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?

Upvotes: 3

Views: 2581

Answers (3)

marcoow
marcoow

Reputation: 4062

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

Upvotes: 0

michael
michael

Reputation: 2997

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

Upvotes: 5

alvincrespo
alvincrespo

Reputation: 9334

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?

Upvotes: 1

Related Questions