jeudi 24 mars 2016

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://ift.tt/1VKNt0O 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( {
  session: 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.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?




Aucun commentaire:

Enregistrer un commentaire