Using flash with Ember-Simple-Atuth


#1

I can’t figure out why flash messsages are displayed whe using in current-user service but nothing happens if I move them to the route? Here is the code from services/current-user.js:

export default Service.extend({
  session: service('session'),
  store: service(),
  flashMessages: service('flash-messages'),

  loadCurrentUser() {
    this.get('flashMessages').clearMessages();
    if (this.get('session.isAuthenticated')) {
      return this.get('store').queryRecord('user', { me: true }).then((user) => {
        this.get('flashMessages').success("Signed in successfully !");
        this.set('user', user);
      });
    } else {
      this.get('flashMessages').danger('You are not signed in!');
      return RSVP.resolve();
    }
  }

Now if I move the flash from else part to application.js router:

export default Route.extend(ApplicationRouteMixin, {
  currentUser: service('current-user'),
  flashMessages: service('flash-messages'),

  actions: {
    logout() {
      this.get('session').invalidate();
      let message = this.get('i18n').t('flash.signed_off');
      this.get('flashMessages').danger(message);
    }
},

beforeModel() {
    return this._loadCurrentUser();
  },

  sessionAuthenticated() {
    this._super(...arguments);
    this._loadCurrentUser();
  },

  _loadCurrentUser() {
    return this.get('currentUser').loadCurrentUser().catch(() => this.get('session').invalidate());
  }

No errors, nothing at all. Any idea ? Thank you.


#2

Even ifI put the flash code in _loadCurrentUser method in catch block:

_loadCurrentUser() {
    return this.get('currentUser').loadCurrentUser().catch(() =>  {
      this.get('session').invalidate();
      let message = this.get('i18n').t('flash.signed_off');
      this.get('flashMessages').danger(message);
    });
  }

it does not work :frowning: