0

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?

3 Answers3

1

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()
Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104
0

Why don't you add isAdmin to your SimpleAuth Session:

isAdmin: function() {
  return @get('current_user.role.name') == 'Administrator';
}.property('current_user')

Then you should be able to do

{{session.isAdmin}}

in your templates.

Martin Stannard
  • 811
  • 8
  • 22
0

while login or register save user information in codeigniter session.

then access current login user information form session across the pages.

$userinfo = array(name=>'abc','userid'=>234);

$this->session->set_userdata($userinfo );//use for store user inforamtion

$current = $this->session->userdata('userid'); //for access

echo $current;

Ranjeet Singh
  • 662
  • 5
  • 12