繁体   English   中英

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

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

我正在开发一个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作为id传递给商店的find方法

我注意到错误发生在验证者的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')false ,而this.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,而不必在每次代码更改后都重新登录?

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

暂无
暂无

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

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