0

I am using ember-simple-auth (and ember-simple-auth-token, but I think this is not relevant). Whenever a logged-in user reloads the page, the session is properly re-created. I have one problem, though: when the user enters the credentials, I am running this code:

// controllers/login.js

import Ember from 'ember';

import helpers from '../lib/helpers';

function refreshActiveUser(store, session) {
    store.find('user', 'me').then(function(user) {
        Ember.set(session, 'activeUser', user);
    });
}

export default Ember.Controller.extend({
    session: Ember.inject.service('session'),
    actions: {
        authenticate: function() {
            var identification = this.get('model.identification'),
                password = this.get('model.password'),
                authenticator = 'authenticator:token',
                store = this.get('store'),
                session = this.get('session');
            session.authenticate(authenticator, {identification, password}).then(function() {
                refreshActiveUser(store, session);
            });
        }
    },
});

But I do not know how to trigger my refreshActiveUser on application reload. How can I listen to a "session initialized" event?

blueFast
  • 41,341
  • 63
  • 198
  • 344

1 Answers1

1

If I understand your issue correctly then you could make an observer for session.isAuthenticated

loggedIn: function() {
        if(!this.get('session.isAuthenticated'))
            return;

        var store = this.get('store');
        var profileLoaded = get_personal_settings(store);
        var self = this;

        profileLoaded.then(function(profile) {
            // TODO: only used for user, save user, not the whole profile

            self.set('session', profile);
        });


}.observes('session.isAuthenticated').on('init'),
kristjan reinhold
  • 2,038
  • 1
  • 17
  • 34