简体   繁体   中英

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.

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. 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:

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

I notice that the error happens before the authenticator's restore() method gets called.

The property ( currentUserId ) that causes the error when called from a reloading route is in an extension of the session service (abridged):

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 {} .

I have included the AuthenticatedRouteMixin in the routes I'm trying this in, and the problem persists.

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?

The session service's currentUserId only returns the current user's id when it's actually present. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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