1
votes

Hi I am trying to write my own Authorizer using ember simple auth. However Ember-cli doesn't see to be picking it up and it is still saying No authorizer was configured for Ember Simple Auth - specify one if backend requests need to be authorized.

I am using ember-cli 0.1.1

Here is my initializer:

var CustomAuthorizer = SimpleAuth.Authorizers.Base.extend({
  authorize: function(jqXHR, requestOptions){
    console.log('authCCC', jqXHR, requestOptions);
  }
});

export default {
  name: 'authorization',
  before: 'simple-auth',
  initialize: function(container, application) {
    container.register('authorizer:custom', CustomAuthorizer);
  }
};

Then according to the doc i need to do this in my config/environment.js

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

Not sure what is wrong here. From the author all i get is a link to the doc which doesn't help :/

Thanks in advance.

EDIT

Here is my full environment.js

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'app-client',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    },
    contentSecurityPolicy: {
      'font-src': "'self' https://fonts.gstatic.com"
    }
  };

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

  if (environment === 'development') {
    ENV.APP.LOG_RESOLVER = true;
    ENV.APP.LOG_ACTIVE_GENERATION = true;
    ENV.APP.LOG_VIEW_LOOKUPS = true;
    ENV.APP.SERVER_URL = 'http://localhost:3000';
    // ENV.APP.SERVER_URL = 'http://0.0.0.0:3000';
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.baseURL = '/';
    ENV.locationType = 'auto';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'staging') {
    ENV.APP.SERVER_URL = 'http://apistaging.server.io';
  }

  if (environment === 'production') {
    ENV.APP.SERVER_URL = 'https://api.server.io';
  }

  return ENV;
};
1
That looks correct, maybe you're configuring the authorizer only for one environment or so?marcoow
@marcoow i have added the full environment.js file. Do you see any problems? I don't think is is scopes to a env right? I am using bower version of simple-auth (0.6.7) rather then the ember-cli one will that make a diff?charleetm
You're using the SimpleAuth global which you shouldn't be doing when using Ember CLI. Make sure you're using ember-cli-simple-auth, run the generator etc.marcoow
@marcoow thanks for that. Indeed one have to use the ember-cli version of simple auth. Now I have created a authorizer! The next question is about when does the authorize method get called? There are a couple of placed I want this to be invoked. All ajax request and when the page is refreshed. I can't seem to get that to work. I read the doc but is not so clear..charleetm
The authorizer's authorize method is called for every XHR request (unless it's a cross origin request and the origin has not beed whitelisted) - check the docs here: github.com/simplabs/ember-simple-auth#authorizers.marcoow

1 Answers

1
votes

See above comments for the conversation with @marcoow. Essentially here were my mistakes.

  • Make sure you are using the ember-cli version of simple auth if you are using ember-cli.

  • Make sure you have registered the authorizer in your environment.js along with the crossOriginWhitelist so a typical will become

    ENV['simple-auth'] = { authorizer: 'authorizer:custom', crossOriginWhitelist: ['http://domain.com'] };

The information is all the his docs but took some piecing together for a simple-auth newbie.

Charlie