1

currently I'm changing my project from ember to ember-cli and run in a issue I can't get rid of. I am really not sure where the problem is, since I also updated ember and ember-data.

I get the Error:

Uncaught Error: Cannot re-register: `store:main`, as it has already been resolved.

I'm trying to load a user via the simple-auth Session.

import UserSession from '../session/user';

export default {
  name: 'user-session',
  before: 'simple-auth',
  after: 'store',
  initialize: function(container, application) {
    container.register('session:user', UserSession);
  }
};

and the session:

import Session from 'simple-auth/session';

export default Session.extend({
  setup: function(authenticator, content, trigger) {
    // Do not let setup trigger
    this._super.call(this, authenticator, content, false);
    // Lookup user and trigger events ourselves
    var store = this.container.lookup('store:main');
    console.log(store.find);

    var self = this;
    store.find('user', content.userId)
      .then(function(user) {
        self.set('user', user);
        self.trigger('sessionAuthenticationSucceeded');
      }, function() {
        console.log('ERROR: Could not resolve user of session!');
      });
  }
});

the store.find is there but then the error breaks it.

I also tired to inject the store like this: Ember-Simple-Auth currentUser example help required but had the same result.

Further I tried to make it via the instance-initalizer for ember-data beta.19. I do stuff like this:

Session from '../session/user';

export default {
  name: 'user-session1',
  after: 'ember-data',
  initialize: function(container, application) {
      var store = container.lookup('store:main');
  }
};

but this ends up in:

Uncaught TypeError: container.lookup is not a function

Using:

 DEBUG: -------------------------------
ember.debug.js:4874DEBUG: Ember             : 1.12.1
ember.debug.js:4874DEBUG: Ember Data        : 1.0.0-beta.19.2
ember.debug.js:4874DEBUG: jQuery            : 1.11.3
ember.debug.js:4874DEBUG: Ember Simple Auth : 0.8.0
ember.debug.js:4874DEBUG: -------------------------------

Thx for the help

------------------ EDIT --------------------------------------------

I updated my instance-initializers based on @Artych comment where I get the store. I removed the custom session from simple-auth and tried it with reopening

ENV['simple-auth'] = {
    //session: 'session:user', ...

My function:

initialize: function(application) {

    var store = application.container.lookup('store:main');

    Session.reopen({
      setCurrentUser: function() {
        console.log('never get here');
        var accessToken = this.get('access_token');
        var self = this;

        if (!Ember.isEmpty(accessToken)) {
          //never gets here, doesn't matter if I take other variables
        }
      }.observes('access_token', 'id', 'userId', 'user_id')
    });

  }

the problem now is that it never goes into "setCurrentUser". I still can logout and in. my autenticator:

authenticate: function(credentials) {
return new Ember.RSVP.Promise(function(resolve, reject) {
  var ttl = 30*60*1000;  // Request login for 30 minutes
  var data = _.extend(credentials, {ttl: ttl});
  Ember.$.ajax({
    type: 'POST',
    url: ENV.api + '/users/login',
    data: data,
    dataType: 'json'
  })
  .then(function(response) {
  console.log(response);
    Ember.run(null, resolve, response);
  }, function(xhr, status, error) {
    Ember.run(null, reject, error);
  });
});

},

returns:

Object {id: "xI3sPSsgdOiHLd8DcFyuOE42KhbuO8gi8BjWBJRrgHgeCESWoma99C2RtvC6tnxG", ttl: 1800000, created: "2015-07-02T14:00:06.600Z", userId: 1}

As you can see, I added a bunch of observed variables: observes('access_token', 'id', 'userId', 'user_id') which I saw in different other questions but nothings helps. Any idea on this? Thx

Community
  • 1
  • 1
PowPi
  • 164
  • 1
  • 1
  • 10
  • container.lookup is not a function: should be `initialize: function(instance) { var store = instance.container.lookup('store:application'); //... }`. I do not know if this helps you, but this is correct syntax for instance-initializers and as far as I know in 1.0.0-beta19 `store:application` is using instead `store:main` – artych Jul 02 '15 at 11:18
  • @Artych your right. That is the correct syntax and gives me the store. my problem is now that i got until now this `ENV['simple-auth'] = {session: 'session:user', ...` in my environment.js. removing this and instant simple repopen the class in the instance-initalizer `initialize: function(application) { Session.reopen({` didn't help me further to load my own user :( – PowPi Jul 02 '15 at 13:33
  • Could you edit your question to show where you are stopped now? – artych Jul 02 '15 at 13:39
  • There is some misundestanding. You mentioned my answer http://stackoverflow.com/a/30894842/4950029 doesn't work for 1.0.0-beta19, it is true and there is notice in it why (needed change initializer to instance-initializer and use `store:application` and may be something else ). So I could try to help you on this way (since it is known for me), so i've asked where you stopped there. I personally do not appreciate idea of `reopen` session. – artych Jul 02 '15 at 14:38
  • @Artych The "edit" part of my question is a instance-initalizer. How do you would do it without a reopen? As far as I understand, there should not be a container.register in a instance-initalizer. Further, when I use store:application I only get a undefined. I did some testing with my old ember only code. There my code works fine with ember-data beta.19.2 and ember 1.12.1. It must be related to ember-cli -.- – PowPi Jul 03 '15 at 07:39

1 Answers1

0

I Solved it. Simple don't use Ember Data 1.0.0-beta.19.2. I upgraded to 1.13.4 and I was able to get the store like before.

PowPi
  • 164
  • 1
  • 1
  • 10