1

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?

Erto Pena
  • 11
  • 2

1 Answers1

0

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.

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • Thank you. I know that the value is not present (in fact `this.get('session.data.authenticated')` is an empty object in the session service). However, we can't simply null-check it because the app needs the value to work. What would make sense to me is for ember-simple-auth to get it from the internal session if it is available (for the app to work in normal conditions), and if not then to try to restore it from local storage (for cases like when the site reloads). I believe this is the intended functionality, so I wonder what I need to change. – Erto Pena Mar 28 '16 at 14:46
  • I imagine that the session should be restored by calling the `restore()` method in the authenticator, but the restore method is not even being called. – Erto Pena Mar 28 '16 at 15:25