简体   繁体   English

使用ember-simple-auth时出现Ember cli错误

[英]Ember cli error with ember-simple-auth

I'm trying to use ember-simple-auth with a custom session. 我正在尝试使用ember-simple-auth和自定义会话。 After logging in, when I try to access the session in a template, like so: 登录后,当我尝试在模板中访问会话时,如下所示:

{{session.current_user.email}}

I get the following error: 我收到以下错误:

Uncaught Error: Assertion Failed: Required contextualElement for view <Ember._HandlebarsBoundView:ember375> is missing

if I reload the page, the error goes away. 如果我重新加载页面,错误就会消失。 Also, if I use an unbound helper, it goes away: 此外,如果我使用未绑定的帮助程序,它会消失:

{{unbound session.current_user.email}}

I have the following code to set the current_user when the user_id changes: 我有以下代码来在user_id更改时设置current_user:

import Session from 'simple-auth/session';
import Ember from 'ember';

export function initialize(container) {
  Session.reopen({
    setCurrentUser: function() {
      var id = this.get('user_id');
      var _this = this;
      if(!Ember.isEmpty(id)) {
        return container.lookup('store:main').find('user', id).then(function(user){
          _this.set('current_user', user);
        });
      }
    }.observes('user_id')
  });
}

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

What am I doing wrong? 我究竟做错了什么?

Edit #1: 编辑#1:

Where would I put this code? 我会把这段代码放在哪里? An initializer? 初始化器? app/sessions/custom.js ? app/sessions/custom.js

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

Then in the environment.js I set: 然后在environment.js中我设置:

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

and in the initializer I register the custom session? 在初始化程序中我注册自定义会话?

container.register('session:custom', Session);

Edit #2: 编辑#2:

Moved custom session to sessions/custom.js . 将自定义会话移动到sessions/custom.js Still same error: 还是一样的错误:

隐秘的灰烬错误

I would define a currentUser method on the Session that returns the user as a promise: 我将在Session上定义一个currentUser方法,该方法将用户作为承诺返回:

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

Also you should better define your own customer session class that extends from the Ember Simple Auth Session class instead of reopening that. 此外,您应该更好地定义自己的客户会话类,该类从Ember Simple Auth Session类扩展而不是重新打开它。

Similar to what marcoow said, but we are using a promise object: 类似于marcoow所说,但我们使用的是promise对象:

app/initializers/simple-auth-session.js

import Session from 'simple-auth/session';
import DS from 'ember-data';

export default {
  name: 'simple-auth-custom-session',
  before: 'simple-auth',
  initialize: function(container) {
    Session.reopen({
      currentUser: function() {
        return DS.PromiseObject.create({
          promise: container.lookup('store:main').find('user', 'me')
        });
      }.property()
    });
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM