Daniel Thompson
Daniel Thompson

Reputation: 2351

Ember.js -- how to access data in Ember session from route

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

Upvotes: 2

Views: 2448

Answers (1)

TheCompiler
TheCompiler

Reputation: 308

Try

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

Upvotes: 3

Related Questions