在livereload恢复会话之前使用ember-simple-auth的Ember app错误

时间:2016-03-24 21:29:49

标签: ember.js ember-simple-auth

我正在开发一个Ember 1.13.8中的应用程序,其中一个Rails后端使用Devise进行身份验证,我正在尝试升级到ember-simple-auth。

我已尽可能地按照http://simplabs.com/blog/2015/11/27/updating-to-ember-simple-auth-1.0.html处的升级路径指南进行操作。一切都像以前一样工作,除了当我保存代码更新时,livereload会导致以下错误,而不是重新加载某些页面:

  

ember.debug.js:24180处理路由时出错:users.index错误:   断言失败:您不能将undefined作为ID传递给商店   找到方法

我注意到错误发生在验证者的restore()方法被调用之前。

从重新加载路由调用时导致错误的属性(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' ),

});

控制台日志显示currentUserId属性this.get('session.isAuthenticated')falsethis.get('session.data.authenticated'){}

我已将AuthenticatedRouteMixin包含在我尝试使用的路线中,问题仍然存在。

这是我的自定义身份验证器:

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

升级后需要进行哪些更改才能修复livereload,而不必在每次更改代码后再次登录?

1 个答案:

答案 0 :(得分:0)

会话服务currentUserId仅在当前用户的ID实际存在时返回该ID。当您使用它来通过Ember数据存储加载当前用户记录时,您必须检查它是否实际存在,否则您将通过undefined这将导致错误你正在看。