0

I am using ember-cli-simple-auth and ember-cli-simple-auth-devise in an ember-cli project.

I'm also customizing simple-auth's Session via an initializer:

// app/initializers/custom-session.js
import Ember from 'ember';
import Session from 'simple-auth/session';

export default {
  name: 'custom-session',
  before: 'simple-auth',

  initialize: function(container, application) {
    Session.reopen({
      setCurrentUser: function() {
        var id = this.get('user_id'),
            self = this;

        if (!Ember.isEmpty(id)) {
          return container.lookup('store:main').find('user', id)
            .then(function(user) {
              self.set('currentUser', user);
            });
        }
      }.observes('user_id')
    });
  }
};

In a simple acceptance test (one that simply calls ok(1)), I'm getting the following error

Error: Assertion Failed: calling set on destroyed object  
Source:     
    at Adapter.extend.exception (localhost:4900/assets/vendor.js:57907:19)
    at apply (http://localhost:4900/assets/vendor.js:21143:27)
    at superWrapper [as exception] (localhost:4900/assets/vendor.js:20721:15)
    at RSVP.onerrorDefault (localhost:4900/assets/vendor.js:59827:26)
    at Object.__exports__.default.trigger (localhost:4900/assets/vendor.js:22673:13)
    at Promise._onerror (localhost:4900/assets/vendor.js:23397:16)
    at publishRejection (localhost:4900/assets/vendor.js:23804:17)
    at http://localhost:4900/assets/vendor.js:29217:9

If I comment out the self.set('currentUser', user); line, the error goes away.

What's the appropriate way to handle this? Is there a way to ignore this initializer in tests?

I also get this log message:

No authorizer factory was configured for Ember Simple Auth - specify one if backend requests need to be authorized.

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

1 Answers1

1

First of all you should update Ember Simple Auth to the latest version 0.6.4 that allows you to specify a custom session class without having to reopen the default Session: https://github.com/simplabs/ember-simple-auth/releases/tag/0.6.4.

Secondly, the warning about the authorizer simply means that requests going to a backend server will not be authorized (e.g. will not have an Authorization header) as no authorizer is defined. That might be ok though depending on your setup.

Regarding the destroyed object problem you could simply check whether the object is destroyed:

if (!self.isDestroyed) {
  self.set('currentUser', user);
}
marcoow
  • 4,062
  • 1
  • 14
  • 21