0

I'm using ember-simple-auth-devise.

When a user logs in, the user's email and token are added to the headers of every request.

When the user changes his email address, I can update the session's email but this does not propagate to the header as well.

What is the best way to trigger an update to the session email and the header email?

Shagymoe
  • 1,296
  • 1
  • 15
  • 22

1 Answers1

0

Here's what I did in the end:

app/sessions/custom.js

import Ember from 'ember';
import DS from 'ember-data';
import Session from 'simple-auth/session';

export default Session.extend({
  currentUser: function() {
    var userId = this.get('secure.userId');

    if (!Ember.isEmpty(userId)) {
      return DS.PromiseObject.create({
        promise: this.container.lookup('store:main').find('user', userId)
      });
    }
  }.property('secure.userId'),

  // Update the session email when the user successfully changes their email.
  updateEmail: Ember.observer('currentUser.email', function() {
    var currentUserEmail = this.get('currentUser.email'),
        sessionEmail = this.get('secure.email');

    if(currentUserEmail && !this.get('currentUser.isDirty') && sessionEmail !== currentUserEmail) {
      this.set('secure.email', currentUserEmail);
      this.set('email', currentUserEmail);
    }
  }),
});

It seems that updateStore on the session doesn't get called when you set 'secure.email' but it does get called when you set 'email', however, just setting 'email' is not sufficient because updateStore uses 'secure.email' to persist the data (email & token).

Shagymoe
  • 1,296
  • 1
  • 15
  • 22