1

使用 ember-data 1.0.0-beta.8,ember-simple-auth 在发出任何其他请求之前从我的 API 加载当前用户。这保证了当前用户数据将在其他路由中可用。升级到 ember-data 1.0.0-beta.9 后,它会在发出其他 API 请求加载当前用户。

有没有办法强制 ember-simple-auth 在使用 ember-data 1.0.0-beta.9 的其他数据之前加载当前用户?

我的自定义简单身份验证会话如下所示:

import SimpleAuthSession from 'simple-auth/session';

export default SimpleAuthSession.extend({
  updateCurrentUser: function() {
    var self = this;
    var userId = this.get('user_id');
    if (!Ember.isEmpty(userId)) {
      self.set('currentUser', self.container.lookup('store:main').find('current-user', userId));
    }
  }.observes('user_id')
});

我的身份验证初始化程序:

import MyAppSession from 'my-app-ember/lib/my-app-session';
import FacebookAuthenticator from 'my-app-ember/lib/facebook-authenticator';

export default {
  name: 'authentication',
  before: 'simple-auth',

  initialize: function(container) {
    container.register('session:my-app-session', MyAppSession);
    container.register('simple-auth-authenticator:facebook', FacebookAuthenticator);

    window.ENV = window.ENV || {};

    window.ENV['simple-auth'] = {
      session: 'session:my-app-session',
      authorizer: 'simple-auth-authorizer:oauth2-bearer',
      routeAfterAuthentication: 'moments'
    };

    window.ENV['simple-auth-oauth2'] = {
      serverTokenEndpoint: MyAppEmberENV.API_NAMESPACE + '/oauth/token'
    };
  }
};

这是一个地方的例子,我依赖于在 afterModel 钩子中的会话上设置的 currentUser 对象,并且在升级后被破坏:

export default Ember.Route.extend({
  model: function() {
    return this.store.find('moment');
  },

  afterModel: function() {
    if (!this.get('session.currentUser.isReturningUser')) {
      this.transitionTo('settings');
    }
  }
});
4

3 回答 3

2

我正在做类似的事情,并且 beta 9 没有这个问题。我可能会尝试在初始化程序中执行此逻辑,以便您可以在 simple-auth init 之前运行它。

import Ember from 'ember';                               
import Session from 'simple-auth/session';               
import Authorizer from 'myapp/authorizers/custom';

export default {
  name: 'simple-auth-config',       
  before: 'simple-auth',            
  initialize: function(container) { 
    container.register('authorizer:custom', Authorizer);                                

    window.ENV = window.ENV || {};                                                      
    window.ENV['simple-auth'] = {                                                       
      authorizer: 'authorizer:custom',                                                  
      crossOriginWhitelist: [MyappENV.API_HOST]
    };                                                                                  

    window.ENV['simple-auth-oauth2'] = {                                                
      serverTokenEndpoint: MyappENV.API_HOST + '/token'                             
    };                                                                                  

    Session.reopen({                                                                    
      currentUser: function() {                                                         
        var userId = this.get('user_id');                                               
        if (!Ember.isEmpty(userId)) {                                                   
          return container.lookup('store:main').find('user', userId);                   
        }                                                                               
      }.property('user_id')                                                             
    });
  }
}                                                                                 

```

于 2014-08-21T15:59:53.180 回答
2

如果您需要确保在发出任何其他 API 请求之前成功加载用户,则必须添加一个自定义初始化程序,该初始化程序延迟准备就绪,仅在用户成功加载后才推进准备就绪:

// app/initializers/session-user.js
export default {
  name:       'session-user',       
  after:      'simple-auth',            
  initialize: function(container, application) { 
    var session = container.lookup('simple-auth-session:main');
    if (session.get('isAuthenticated')) {
      application.deferReadiness();
      session.get('currentUser').then(function() {
        application.advanceReadiness();
      }, function() {
        //handle error...
      });
    }
  }
}
于 2014-09-01T06:32:56.890 回答
0

在尝试了我能想到的所有可能的组合之后,唯一对我有用的是从应用程序路由中的会话中获取当前用户。这看起来很 hacky,但我可能会稍后进行更多实验,看看是否还有其他事情发生。

应用程序/路由/application.js:

import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  model: function() {
    if (this.get('session.user_id')) {
      return this.get('session.currentUser');
    }
  }
});
于 2014-08-31T13:57:04.057 回答