简体   繁体   English

在livereload期间恢复会话之前,使用ember-simple-auth的Ember应用程序错误

[英]Ember app errors with ember-simple-auth before restoring session during livereload

I am working on an app in Ember 1.13.8 with a Rails backend that uses Devise for authentication, and I'm trying to upgrade to ember-simple-auth. 我正在开发一个Ember 1.13.8中的应用程序,其中一个Rails后端使用Devise进行身份验证,我正在尝试升级到ember-simple-auth。

I've followed the upgrade path guide at http://simplabs.com/blog/2015/11/27/updating-to-ember-simple-auth-1.0.html as closely as I can see. 我已经按照我所看到的那样,按照http://simplabs.com/blog/2015/11/27/updating-to-ember-simple-auth-1.0.html上的升级路径指南进行了操作。 Everything is working as before, except that when I save an update to the code, livereload causes the following error instead of reloading certain pages: 一切都像以前一样工作,除了在我保存代码更新时,livereload导致以下错误,而不是重新加载某些页面:

ember.debug.js:24180 Error while processing route: users.index Error: Assertion Failed: You may not pass undefined as id to the store's find method ember.debug.js:24180处理路由时出错:users.index错误:断言失败:您可能无法将undefined id作为id传递给商店的find方法

I notice that the error happens before the authenticator's restore() method gets called. 我注意到错误发生在验证者的restore()方法被调用之前。

The property ( currentUserId ) that causes the error when called from a reloading route is in an extension of the session service (abridged): 从重载路由调用时导致错误的属性( currentUserId )在会话服务的扩展名中(节略):

import Ember from 'ember';
import SessionService from 'ember-simple-auth/services/session';

export default SessionService.extend({
  session: Ember.inject.service('session'),
  store: Ember.inject.service(),

  currentUserId: Ember.computed( 'session.content.authenticated.user.id', function() {
    var sessionUserId = this.get( 'session.content.authenticated.user.id' );
    if ( !Ember.isEmpty( sessionUserId ) ) {
      return this.get( 'session.content.authenticated.user.id' );
    }
  }).property( 'sessionUserId' ),

});

Console logs show that in the currentUserId property this.get('session.isAuthenticated') is false and that this.get('session.data.authenticated') is {} . 控制台日志显示在currentUserId属性中this.get('session.isAuthenticated')false ,而this.get('session.data.authenticated'){}

I have included the AuthenticatedRouteMixin in the routes I'm trying this in, and the problem persists. 我在尝试使用的路由中包含了AuthenticatedRouteMixin,问题仍然存在。

Here is my custom authenticator: 这是我的自定义验证器:

import Ember from 'ember';
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
import ENV from '../config/environment';
import sha1 from 'npm:sha1';

export default DeviseAuthenticator.extend( {
  store: Ember.inject.service(),

  tokenAttributeName: 'auth_token',
  identificationAttributeName: 'email',
  resourceName: 'user',

  authenticate: function( credentials ) {
    var _this = this;

    this.get( 'session' )
      .set( 'currentUserPassword', sha1( credentials.password ) );
    let promise =  new Ember.RSVP.Promise( function( resolve, reject ) {
      _this.makeRequest( credentials ).then( function( response ) {
        Ember.run( function() {
          resolve( response );
        } );
      }, function(xhr, status, error) {
                var response = xhr.responseText;
                Ember.run(function() {
                    reject(response);
                } );
      } );
    });
      return promise;
  },

  restore: function(data) {
    console.log("Trying to restore; data: ");
    console.log( data );
    return new Ember.RSVP.Promise(function(resolve, reject) {
      if (!Ember.isEmpty(data.auth_token)) {
        resolve(data);
      } else {
        reject();
      }
    });
  },

  invalidate: function() {
    console.log('invalidate...');
    return Ember.RSVP.resolve();
  },

  makeRequest: function( credentials ) {
    var uuid = "abc123";

    if( typeof( window.device ) !== 'undefined' ) {
      uuid = window.device.uuid;
    }

    return Ember.$.ajax( {
      type: 'POST',
      dataType: 'json',
      url: ENV.apiHost + '/api/v1/users/sign_in',
      data: {
        "user": {
          "email": credentials.identification,
          "password": credentials.password,
        },
        "device": {
          "uuid": uuid
        }
      }
    } );
  },
} );

What changes do I need to make to fix livereload after the upgrade, instead of having to log in again after every code change? 在升级后,我需要进行哪些更改以修复livereload,而不必在每次代码更改后都重新登录?

The session service's currentUserId only returns the current user's id when it's actually present. 会话服务的currentUserId仅在实际存在时返回当前用户的ID。 When you use that to load the current user record via the Ember Data store you'd have to check whether it's actually present, otherwise you'd be passing undefined which would causes the error you're seeing. 当您使用它来通过Ember数据存储加载当前用户记录时,您必须检查它是否实际存在,否则您将传递undefined ,这将导致您看到的错误。

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

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