【问题标题】:Get a hold of Ember Simple Auth session via container通过容器获取 Ember Simple Auth 会话
【发布时间】:2014-08-14 00:45:58
【问题描述】:

在此SO answer 之后,我正在尝试使用container.lookup('simple-auth-session:main'); 进行 Ember 简单身份验证会话,但这给了我未定义

我正在使用 Ember Cli 和 Ember Simple Auth Devise 身份验证器。

【问题讨论】:

  • 是您的初始化程序,因为您试图让会话在 'simple-auth' 初始化程序之后运行?如果不是,该会话将不会被注册。
  • 我正在尝试关注this gist,但我将after: authentication 更改为after: 'simple-auth-devise',我认为 是正确的。
  • 你真正想要达到什么目的?如果它以某种方式创建 currentUser 属性,我建议您遵循此 Ember Simple Auth 示例:github.com/simplabs/ember-simple-auth/blob/master/examples/…
  • 其实我只是看了一下。我想获取该示例中提到的用户实例,但我也想将用户注入每个控制器和视图。
  • 如果你在会话对象上有一个用户属性,你也不需要将用户注入控制器,因为会话已经被注入,你可以简单地通过@987654329访问用户@.

标签: session authentication ember.js ember-simple-auth


【解决方案1】:

@marcoow 上面的评论就像一个魅力。他指给我this example

FIRST:添加一个注册自定义会话的初始化程序

Ember.Application.initializer(
  name: 'authentication'
  before: 'simple-auth'
  initialize: (container, application) ->
    container.register('session:custom', App.CustomSession)
)

第二个:用您的自定义会话替换会话

window.ENV['simple-auth'] = {
  session: 'session:custom'
}

第三:定义自定义会话

App.CustomSession = SimpleAuth.Session.extends(
  currentUser: (->
    unless Ember.isEmpty(@user_id)
      @container.lookup('session:main').load('user', @user_id)
      //Note! I'm using Ember Persistence Foundation, therefore 'session:main', 
      //but if you're using Ember Data, use 'store:main'
  ).property('user_id')
)

【讨论】:

  • 请注意container.lookup('simple-auth-session:main'); 仍将提供默认会话。要查找自定义会话,您需要使用container.lookup('session:custom:main');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多