Search code examples
ember.jsember-simple-auth

ember-simple-auth update data in session


I have the following code:

changeUsername: function(){
    var _this = this;
    this.store.findById('user', this.session.get('user.id')).then(function(user){
        user.set('username', _this.get('newUsername'));
        user.save();

        _this.session.set('user.username', _this.get('newUsername'));
        _this.session.persist();
    });
}

As you can see - I am persisting to the ember simple auth session a user object which I want to update in this method.

Calling persist on the session doesn't do the trick.

I have also tried:

_this.session.store.set('user.username', _this.get('newUsername'));
_this.session.store.save();

Which didn't work either.

How can I update the user object on the session?


Solution

  • Assuming you have extended or reopened the simple auth session with a method of user with a computed property on userId the answer would be

      var _this = this;
      if(!Ember.isEmpty(this.get('session.content.userId'))) {
         this.get('session.user').then(function(user){
           user.set('username', _this.get('newUsername'));
           user.save();
         });
      }
    

    and your template would use {{ session.user.username }}