ember-simple-auth icon indicating copy to clipboard operation
ember-simple-auth copied to clipboard

Bug if app is entered through a protected route

Open balinterdi opened this issue 13 years ago • 11 comments

If I load the application on a non-protected route and then transition to a protected route, everything works normally.

However, when I load the application on the protected route, it throws the following error:

screen shot 2013-10-29 at 15 55 57

I also created a couple of jsbins:

JSbin for working example JSbin for failing example

Unfortunately the error can not be seen in JSBin hence the screenshot. I checked this both with ember-1.0.0 and the latest canary build.

balinterdi avatar Oct 29 '13 14:10 balinterdi

This is also happening to me too using the latest canary build. I'm not sure whats causing it!

According to the documentation send() should bubble up the tree, therefore reaching the ApplicationRoute and triggering the login action. But for some reason it isn't working.

I've tried changing the order of my code so that the ApplicationRoute is created early on in the code, incase it wasn't initialised quick enough but this doesn't seem to make any difference for me :(

adamlc avatar Oct 29 '13 15:10 adamlc

I think this is a similar problem as the one described here: https://gist.github.com/machty/6879857 (see point 4 in the list) - not sure how to fix it yet...

marcoow avatar Oct 29 '13 16:10 marcoow

Changing triggerLogin in Ember.SimpleAuth.AuthenticatedRouteMixin from this:

triggerLogin: function(transition) {
  this.set('session.attemptedTransition', transition);
  this.send('login');
}

to this:

triggerLogin: function(transition) {
  this.set('session.attemptedTransition', transition);
  App.__container__.lookup('route:application')._actions['login'].apply(this);
}

actually does fix it but of course isn't desirable. Problem really seems to be that there's no active view hierarchy after a fresh reload that the action could be triggered on. Maybe somehow it can be triggered on the transition or so...

marcoow avatar Oct 29 '13 16:10 marcoow

Ok, got it - solution is here: https://github.com/machty/ember.js/commit/160a41cf4b687a2e35d81f9078c522c341130a84 -call #send on the transition rather than on this:

triggerLogin: function(transition) {
  this.set('session.attemptedTransition', transition);
  transition.send('login');
}

Transition#send is only available with Ember.js 1.2 though. Not sure how I want to handle that - probably ok to require Ember.js 1.2 I guess... What do you guys think?

marcoow avatar Oct 29 '13 16:10 marcoow

Thank you for fixing it so fast, Marco.

I think it is ok to require Ember 1.2 or higher. I would even say that it is required (not just recommended) since that is quite a serious bug, in my opinion.

balinterdi avatar Oct 30 '13 12:10 balinterdi

What I'm doing is this now:

if (Ember.canInvoke(transition, 'send')) {
  transition.send('login');
} else {
  this.send('login');
}

That means the bug is fixed for 1.2 without completely breaking functionality for < 1.2. I think I'd remove that again some time after 1.2 is officially released...

marcoow avatar Oct 30 '13 12:10 marcoow

Looks good. Thank you for this library!

balinterdi avatar Oct 30 '13 12:10 balinterdi

Hi (and happy new year !), I still have the initial problem mentioned by balinterdi. I run my app with ember 1.2. Why don't you simply use ? this.transitionTo(Ember.SimpleAuth.loginRoute); This works for me but I would prefer keep your code ;-)

jmvandamme avatar Jan 01 '14 12:01 jmvandamme

Can you add a stack trace? Would be strange if you really saw the same error as above with 1.2.

marcoow avatar Jan 01 '14 20:01 marcoow

Sincerely sorry for this mistake in my first post :( . I've changed my bower.json file to upgrade in 1.2 but, you know, it's not enough... So, It works perfectly now. Thanks a lot for your help and also for your component.

jmvandamme avatar Jan 02 '14 13:01 jmvandamme

Hi, everyone. I have the same issue, as described here, but without any errors in my console. I use Ember 1.13.3.

UPD. Problem is in my code: my login route is nested to 'lang' route:

Router.map(function () {
  this.route("lang", { path: '/:lang' }, function () {
    this.route('404', { path: '/*:' });
    this.route('login', { path: '/login' });
    this.route('countries', {path: '/countries'}, function() {
      this.route('country', {path: '/:id'});
    });
  });
});

Protected routes are lang.countries and lang.countries.country.

UPD2. I changed sessionRequiresAuthentication action in application-route-mixin and this helped me to solve this issue:

sessionRequiresAuthentication: function () {
  var attemptedTransition = this.get(Configuration.sessionPropertyName).get('attemptedTransition');
  var params;
  if (attemptedTransition && attemptedTransition.params) {
    var authRouteParts = Configuration.authenticationRoute.split('.');
    var route = '';
    for (var i = 0; i < authRouteParts.length; i++) {
      if (route !== '') {
        route += '.';
      }
      route += authRouteParts[i];
      if (attemptedTransition.params[route] !== undefined) {
        for (var k in attemptedTransition.params[route]) {
          if (attemptedTransition.params[route].hasOwnProperty(k)) {
            if (params === undefined) {
              params = {};
            }
            params[k] = attemptedTransition.params[route][k];
          }
        }
      }
    }
  }
  if (params !== undefined) {
    this.transitionTo(Configuration.authenticationRoute, params);
  } else {
    this.transitionTo(Configuration.authenticationRoute);
  }
}

GendelfLugansk avatar Aug 03 '15 10:08 GendelfLugansk