2

I'm having trouble creating a custom authenticator for my laravel backend. I'm not sure if this is the correct custom authenticator for laravel, but I'm using this as a starting point (https://github.com/simplabs/ember-simple-auth/blob/master/examples/6-custom-server.html).

My Ember.SimpleAuth is undefined. Here is what I have in my app.js.

import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';

Ember.MODEL_FACTORY_INJECTIONS = true;

window.ENV = window.ENV || {};
window.ENV['simple-auth'] = {
    authorizer: 'authorizer:custom'
};

Ember.Application.initializer({
    name: 'authentication',
    before: 'simple-auth',
    initialize: function(container, application) {
            //register the laravel authenticator so the session can find it
            container.register('authenticator:laravel', App.LaravelAuthenticator);
            container.register('authorizer:custom', App.CustomAuthorizer);            
    }
});


var App = Ember.Application.extend({
  modulePrefix: 'ember-simple-auth-sample', // TODO: loaded via config
  Resolver: Resolver
});

App.LaravelAuthenticator = Ember.SimpleAuth.Authenticators.Base.extend({
    tokenEndpoint: '/v4/session',

    restore: function(data) {
      return new Ember.RSVP.Promise(function(resolve, reject) {
        if (!Ember.isEmpty(data.token)) {
          resolve(data);
        } else {
          reject();
        }
      });
    },

    authenticate: function(credentials) {
      var _this = this;
      return new Ember.RSVP.Promise(function(resolve, reject) {
        Ember.$.ajax({
          url:         _this.tokenEndpoint,
          type:        'POST',
          data:        JSON.stringify({ session: { identification: credentials.identification, password: credentials.password } }),
          contentType: 'application/json'
        }).then(function(response) {
          Ember.run(function() {
            resolve({ token: response.session.token });
          });
        }, function(xhr, status, error) {
          var response = JSON.parse(xhr.responseText);
          Ember.run(function() {
            reject(response.error);
          });
        });
      });
    },

    invalidate: function() {
      var _this = this;
      return new Ember.RSVP.Promise(function(resolve) {
        Ember.$.ajax({ url: _this.tokenEndpoint, type: 'DELETE' }).always(function() {
          resolve();
        });
      });
    }
});

// the custom authorizer that authorizes requests against the custom server
App.CustomAuthorizer = Ember.SimpleAuth.Authorizers.Base.extend({
    authorize: function(jqXHR, requestOptions) {
        if (this.get('session.isAuthenticated') && !Ember.isEmpty(this.get('session.token'))) {
            jqXHR.setRequestHeader('Authorization', 'Token: ' + this.get('session.token'));
        }
    }
});

loadInitializers(App, 'ember-simple-auth-sample');

export default App;

1 Answer 1

1

Ember.SimpleAuth doesn't exist anymore, it now has it's own global SimpleAuth when you use the browserified distribution. It looks like you're using ember-cli though which means you're using the AMD distribution of Ember Simple Auth anyway which doesn't define any global at all. For instructions on how to use Ember Simple Auth with ember-cli see this blog post.

Apart from that your authenticator and authorizer look fine on first glance and should generally work that way.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. Your tutorials and youtube videos helped a lot. I'm having one problem with the authenticator authenticate the credentials is empty. How do I pass it values?
The data the authenticator uses to authenticate is passed from the controller (usually either from the LoginControllerMixin if you use that or manually to this.get('session').authenticate('authenticator:larval', { YOUR DATA HERE })).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.