Search code examples
javascriptember.jsember-simple-auth

How do I access currentUser from Controllers?


Here's my initializer:

...
Ember.SimpleAuth.Session.reopen({
    currentUser: function() {
        var userId = this.get('user_id');

        if (!Ember.isEmpty(userId)) {
            return container.lookup('store:main').find('user', userId);
        }
    }.property('user_id')
});
...

Controller:

isAdmin: function() {
    var session = this.get('session.currentUser'),
        role = session.get('role'); // 'role' is undefined

    return role.get('name') == "Administrator";
}.property()

But when I tried from Templates:

{{session.currentUser.role.name}}

It works perfectly.

How do I access the currentUser to all Controllers or even in Routes?


Solution

  • I think it's because session.currentUser is a promise. Try this in your controller:

    isAdmin: function() {
        return this.get('session.currentUser').then(function(r) {
          return r.get('role.name') == 'Administrator';
        });
    }.property()