1

I've seen other questions about this (like this one), and I believe this should be working

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

export default {
  name: 'session-with-me',
  before: 'simple-auth',
  initialize: function() {
    Session.reopen({
      me: function() {
        if (this.get('isAuthenticated')) {
          return this.container.lookup('service:store').find('me', { singleton: true });
        }
      }.property('isAuthenticated')
    });
  }
};

the find('me', { singleton: true }) is a working patch of ember-jsonapi-resources. While debugging I can see the request being sent, and the payload comes through. I use the same find call elsewhere in the app, and can confirm a model gets instantiated fine.

On the inspector, under container > simple-auth-session I can see me as a session property, but it shows as { _id: 68, _label: undefined ...}

Has the way to set a session property changed? I may have seen a mention about this somewhere, but I can't find it anymore.

This is in the same domain of another question I asked earlier, but I'm giving up on that approach and trying simply to fetch the user independently of the authentication process.

Community
  • 1
  • 1
oliverbarnes
  • 2,111
  • 1
  • 20
  • 31

3 Answers3

2

Set up a custom session like that:

export default Session.extend({
  me: function() {
    var accessToken = this.get('secure.access_token');
    if (!Ember.isEmpty(accessToken)) {
      return DS.PromiseObject.create({
        promise: this.container.lookup('service:me').find({});
      });
    }
  }.property('secure.access_token')
});

// app/config/environment.js
ENV['simple-auth'] = {
  session: 'session:me'
}

DS.PromiseObject is actually part of Ember Data which you're not using - I don't know whether there's an equivalent in the library you chose.

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • There isn't, but I asked about it on slack, and it looks like defining it inline is as easy as `var PromiseObject = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin);` – oliverbarnes Aug 03 '15 at 15:58
0

This is most likely an issue with ember-jsonapi-resources, not with Ember Simple Auth.

Instead of reopening the session though you should define your own one that extends the default one that Ember Simple Auth provides - see e.g. this answer: How to store the user in a session

Community
  • 1
  • 1
marcoow
  • 4,062
  • 1
  • 14
  • 21
0

We ended up making it work like this:

// app/sessions/me.js
export default Session.extend({
  me: function() {
    var accessToken = this.get('secure.access_token');
    if (!Ember.isEmpty(accessToken)) {
      let self = this;
      return this.container.lookup('service:me').find({}).then((me) => {
        self.set('me', me);
      });
    }
  }.property('secure.access_token')
});

// app/config/environment.js
ENV['simple-auth'] = {
  session: 'session:me'
}

Partly this was due to the way resource services are initialized in EJR (so @marcoow's hunch on this was correct), the other part was just bad coding on my part.

Interestingly we didn't have to explicitly register the session in the container

oliverbarnes
  • 2,111
  • 1
  • 20
  • 31
  • It's not so great to set a property named like the cp though (which effectively replaces the cp). I think with your solution the `me` property would also retain its value once the user logs out. You should return the promise or wrap that in a promise object instead or use an observer that sets (or unsets) the `me` property. – marcoow Jul 30 '15 at 16:32
  • @marcoow I see. I've 'unaccepted' my answer then. Problem is, I tried going the promise route, but I'm really not yet proficient on how to use them. These two questions here on SO are about this too, I'm asking what this code would look like. References are helpful, as is pointing out the problems with the above solution, but not really what I need here - I'm looking for the end solution. It'd be specially interesting to see how this would be done with ESA 1.0, as well – oliverbarnes Aug 02 '15 at 13:30