3

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?

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104

3 Answers3

5

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);
  }
};
AWM
  • 1,130
  • 11
  • 23
michael
  • 2,977
  • 3
  • 20
  • 26
  • This is the way to do it in the most recent versions of ember-cli and simple-auth. However, it might be more advisable to bind `ENV` to `MyAppENV`. So, in your index.html: `window.ENV = window.MyAppENV;` (see: http://stackoverflow.com/a/24845613/2637573). Then in environtment.js add this to ENV: `'simple-auth': { authorizer: 'simple-auth-authorizer:devise', session: 'session:withCurrentUser' }` – AWM Aug 19 '14 at 13:33
  • With ember-cli 0.0.46 and simple-auth 0.6.7 you don't need to include `window.ENV = window.MyAppENV;` or anything else in `index.html` anymore. – AWM Oct 02 '14 at 16:07
1

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?

alvincrespo
  • 9,224
  • 13
  • 46
  • 60
  • Meant to have `.extend` and not `.reopen`, though it seems reopening the included authenticators is a short-term solution. Thanks! – Sam Selikoff Jun 21 '14 at 04:46
0

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
);
Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
marcoow
  • 4,062
  • 1
  • 14
  • 21