簡體   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