0

I'm trying to implement custom auth with ember-simple-auth and I stuck at the start. I have app/autheticators/digest.js

import Base from 'ember-simple-auth/authenticators/base';
import Ember from 'ember';

export default Base.extend({
  restore(data) {
    //
  },
  authenticate(email, password) {
    console.log(email, password);
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run(function() {
          resolve({email: email, password: password});
        });
    });
  },
  invalidate(data) {
    //
  }
});

app/authorizers/digest.js

import Base from 'simple-auth/authorizers/base';  
import Ember from 'ember';

export default Base.extend({  
  header: function() {
    return "test-digest";
  },

  authorize: function(sessionData, block) {
    console.log('authorize...');
    block('Authorization', this.get('header'));
  }
});

Login component:

import Ember from 'ember';
import CryptoJS from 'npm:crypto-js';

export default Ember.Component.extend({
    session: Ember.inject.service('session'),
    actions: {
        login() {
            let { email, password } = this.getProperties('email', 'password');
            this.get("session").authenticate('autheticator:digest', 
                email, CryptoJS.SHA256(password).toString()).catch((reason) => {
                this.set('errorMessage', reason.error);
            });
        }
    }

});

Authentication called properly (I hope), but "authorize" in authorizer never called. I also tried add some values to ENV:

  ENV['simple-auth'] = {
    authorizer: 'authorizer:digest',
    crossOriginWhitelist: ['http://prod-drunkedguru.rhcloud.com:80/'] // ['*'] I also tried
  }; 

But nothing changed. What I'm doing wrong?

P.S. I'm using EmberJS 1.13.0 with EAS 1.0.

Crabar
  • 1,829
  • 1
  • 14
  • 26
  • Hi, Crabar. I have same problem. Can you tell me how to fix this problem?I check marcoow's answer. But I very confused how to set session to $.ajax. If you have the example, can you show me? Thanks a lot. – JeskTop Nov 04 '15 at 11:17
  • Sure. It looks next: this.get('session').authorize('authorizer:digest', (headerName, headerValue) => { const headers = {}; headers[headerName] = headerValue; Ember.$.ajax({ url: "url", method: "GET", data: { }) }, headers: headers }).then(function(result) { // }); }); – Crabar Nov 04 '15 at 11:58

1 Answers1

1

I assume you're using ESA 1.0. In that version the authorizer isn't automatically called anymore but you need to call it manually. There is the DataAdapterMixin that you can use to automatically authorizer Ember Data requests though. See this blog post for guidance on migrating to 1.0: http://log.simplabs.com/post/131698328145/updating-to-ember-simple-auth-10

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • Thx for answer. Will it work for Ember.$.ajax requests? – Crabar Nov 02 '15 at 20:58
  • Sure you can authorize all sorts of requests - check the docs: http://ember-simple-auth.com/api/classes/SessionService.html#method_authorize – marcoow Nov 03 '15 at 09:02
  • If you're using `Ember.$.ajax` you need to do that manually yes - in an actual app you should have an `Ember.Service` of course that wraps `Ember.$.ajax` and implements authorization as well. – marcoow Nov 03 '15 at 09:37
  • Thanks for help. I will try it soon. – Crabar Nov 03 '15 at 10:29
  • Yeah! It's really work. So hard to find not outdated example of using ESA =(. – Crabar Nov 03 '15 at 13:56
  • @marcoow can you give an example of the service that wraps Ember.$.ajax and implements authorization as well? – Navneet Feb 03 '16 at 13:48