3

I'm trying to use ember-simple-auth with a custom session. After logging in, when I try to access the session in a template, like so:

{{session.current_user.email}}

I get the following error:

Uncaught Error: Assertion Failed: Required contextualElement for view <Ember._HandlebarsBoundView:ember375> is missing

if I reload the page, the error goes away. Also, if I use an unbound helper, it goes away:

{{unbound session.current_user.email}}

I have the following code to set the current_user when the user_id changes:

import Session from 'simple-auth/session';
import Ember from 'ember';

export function initialize(container) {
  Session.reopen({
    setCurrentUser: function() {
      var id = this.get('user_id');
      var _this = this;
      if(!Ember.isEmpty(id)) {
        return container.lookup('store:main').find('user', id).then(function(user){
          _this.set('current_user', user);
        });
      }
    }.observes('user_id')
  });
}

export default {
  name: 'authentication',
  before: 'simple-auth',
  initialize: initialize
};

What am I doing wrong?

Edit #1:

Where would I put this code? An initializer? app/sessions/custom.js?

export default Session.extend({
  currentUser: function() {
    var userId = this.get('user_id');
    if (!Ember.isEmpty(userId)) {
     return this.container.lookup('store:main').find('user', userId);
    }
  }.property('user_id')
});

Then in the environment.js I set:

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

and in the initializer I register the custom session?

container.register('session:custom', Session);

Edit #2:

Moved custom session to sessions/custom.js. Still same error:

Cryptic Ember Error

Kori John Roys
  • 2,621
  • 1
  • 19
  • 27

2 Answers2

1

I would define a currentUser method on the Session that returns the user as a promise:

export default Session.extend({
  currentUser: function() {
    var userId = this.get('user_id');
    if (!Ember.isEmpty(userId)) {
     return this.container.lookup('store:main').find('user', userId);
    }
  }.property('user_id')
});

Also you should better define your own customer session class that extends from the Ember Simple Auth Session class instead of reopening that.

Kori John Roys
  • 2,621
  • 1
  • 19
  • 27
marcoow
  • 4,062
  • 1
  • 14
  • 21
  • the original question, couldn't get code formatting in a comment, apparently. – Kori John Roys Jan 15 '15 at 15:08
  • 1
    You put the session into `app/sessions/custom.js` and if you're using the most recent Ember CLI you don't have to register it with the container - simply configure `ENV['simple-auth'].session = 'session:custom'` – marcoow Jan 15 '15 at 15:19
  • Thanks for your help so far marcoow. Simplified a lot of my code. Still same error message though. See edit #2. Are there some special rules for accessing the session in a template? – Kori John Roys Jan 15 '15 at 15:55
  • Hm, should actually work that way, see the example here: https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L32. Which versions of Ember, Ember Data etc. are you using? – marcoow Jan 15 '15 at 19:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68945/discussion-between-kori-john-roys-and-marcoow). – Kori John Roys Jan 16 '15 at 09:20
  • running into similar issue...what was the final solution? – Xander Feb 23 '15 at 17:14
0

Similar to what marcoow said, but we are using a promise object:

app/initializers/simple-auth-session.js

import Session from 'simple-auth/session';
import DS from 'ember-data';

export default {
  name: 'simple-auth-custom-session',
  before: 'simple-auth',
  initialize: function(container) {
    Session.reopen({
      currentUser: function() {
        return DS.PromiseObject.create({
          promise: container.lookup('store:main').find('user', 'me')
        });
      }.property()
    });
  }
};
Asgaroth
  • 4,274
  • 3
  • 20
  • 36