应用程序重新启动时,每次更新会话时触发操作

时间:2016-01-12 16:11:49

标签: ember.js ember-simple-auth http-token-authentication

我正在使用ember-simple-auth(和ember-simple-auth-token,但我认为这不相关)。每当登录用户重新加载页面时,都会正确地重新创建会话。但是我有一个问题:当用户输入凭据时,我正在运行此代码:

// 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);
            });
        }
    },
});

但我不知道如何在应用程序重新加载时触发refreshActiveUser。我怎样才能听“会话初始化”事件?

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,那么您可以为 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'),