2
votes

Using ember-simple-auth I set up a session and set the currentUser key equal to the user object returned from the server (edit: contents of entire session.js file):

//app/services/session.js

import Ember from 'ember';
import ESASession from "ember-simple-auth/services/session";

export default ESASession.extend({

  store: Ember.inject.service(),
  session: Ember.inject.service(),


  setCurrentUser: function () {
    if (this.get('isAuthenticated')) {
        const accountId = this.get('session.content.authenticated.user_id');
        console.log(accountId);
        this.get('store').findRecord('user', accountId).then(user => {
            this.set('currentUser', user);
        });
    }
  }.observes('isAuthenticated')
});

Problem is when I am trying to access the 'currentUser' key from a route for a data relation:

user: Ember.computed(function(){
    console.log(this.get('session'));
    //return `${this.get('session.currentUser')}`;
  }),

In the console I see (in addition to a bunch of other stuff):

 currentUser: Class 

but if I try to log 'session.currentUser' I am seeing undefined.

What can I do to access the user ID from this route? Having a lot of trouble with this! Thanks in advance :D

1
Did you inject the session service into the class the setCurrentUser observer is defined on?marcoow
@marcoow thanks for your comment, I had not injected session! But now that I have the problem persists. I can even log something like 'session._super' and see the proper results, but not 'session.currentUser' even though that key is visible in the console when I inspect the output of console.log(this.get('session'). I will update the above code with the entire content of my session.js file just to make sure I am not making any silly mistakes. Thanks again.Daniel Thompson

1 Answers

3
votes

Try

this.get('session.data.currentUser')