Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application route mixin can register duplicate event handlers #256

Closed
cleaverm opened this issue Jul 20, 2014 · 5 comments
Closed

Application route mixin can register duplicate event handlers #256

cleaverm opened this issue Jul 20, 2014 · 5 comments

Comments

@cleaverm
Copy link
Contributor

When my application loads directly on an authenticated route and the user isn't logged in, the initial transition is aborted as expected and it redirects to the login route. The Application route mixin's beforeModel hook is called before the transition is aborted, and again during the transition to the login route, because the application route hasn't been set up yet (this behaviour is described here). This seems to result in listeners for events such as sessionAuthenticationSucceeded being registered twice on the simple-auth session object.

Both transition 0 and transition 1 call the application route's beforeModel hook:

DEBUG: ------------------------------- ember.js:3521
DEBUG: Ember      : 1.5.1 ember.js:3521
DEBUG: Ember Data : 1.0.0-beta.7+canary.b45e23ba ember.js:3521
DEBUG: Handlebars : 1.1.2 ember.js:3521
DEBUG: jQuery     : 1.9.1 ember.js:3521
DEBUG: ------------------------------- ember.js:3521
Attempting URL transition to /demo ember.js:3521
Transition #0: application: calling beforeModel hook ember.js:3521
Transition #0: application: calling deserialize hook ember.js:3521
Transition #0: application: calling afterModel hook ember.js:3521
Transition #0: demo: calling beforeModel hook ember.js:3521
Transition #0: demo.index: transition was aborted ember.js:3521
Attempting transition to login ember.js:3521
Transition #1: application: calling beforeModel hook ember.js:3521
Transition #1: application: calling deserialize hook ember.js:3521
Transition #0: detected abort. ember.js:3521
Transition #1: application: calling afterModel hook ember.js:3521
Transition #1: login: calling beforeModel hook ember.js:3521
Transition #1: login: calling deserialize hook ember.js:3521
Transition #1: login: calling afterModel hook ember.js:3521
Transition #1: Resolved all models on destination route; finalizing transition. 

It seems that when a user logs in, the application route mixin's sessionAuthenticationSucceeded action is fired twice. Which means it tries to handle the original aborted transition twice:

sessionAuthenticationSucceeded: function() {
  var attemptedTransition =   this.get(Configuration.sessionPropertyName).get('attemptedTransition');
  if (attemptedTransition) {
      attemptedTransition.retry();
      this.get(Configuration.sessionPropertyName).set('attemptedTransition', null);
  } else {
    this.transitionTo(Configuration.routeAfterAuthentication);
  }
}

In my case it never redirects to the original (authentication required) route, because the second time sessionAuthenticationSucceeded is triggered, attemptedTransition is already null, which triggers a transition to the routeAfterAuthentication route.

For now I'm locked to an older release of Ember (and using the now-deprecated Ember App Kit), so I'm not sure whether this is still an issue, or whether I'm doing something facepalm-worthy? As far as I can tell aborted transitions still seem to behave this way in more current Ember releases, since it was described as "the way transitions work" in another thread which was resolved well before the release I'm using. Have I stumbled onto a valid issue or should I fix this in my own application route?

@cleaverm
Copy link
Contributor Author

I've uploaded an example application demonstrating the issue, but found the same behaviour on ember-cli-simple-auth-example when opening the application directly at /protected - it doesn't redirect to the intended route after login.

@cleaverm cleaverm changed the title Application route mixin assigns event handlers twice Application route mixin can assign event handlers twice Jul 21, 2014
@cleaverm cleaverm changed the title Application route mixin can assign event handlers twice Application route mixin can register duplicate event handlers Jul 21, 2014
@marcoow
Copy link
Member

marcoow commented Jul 21, 2014

closed by #257

@marcoow marcoow closed this as completed Jul 21, 2014
@tehviking
Copy link

@marcoow I'm seeing this same "duplicated call" behavior with 0.7.2 and Ember 1.10. Is it possible this fix regressed somehow, or is there something I'm missing?

Here's what I'm seeing with some debugging log statements added:

Attempting transition to login
ember.debug.js:49980Transition #1: application: calling beforeModel hook
ember.debug.js:49980Transition #1: application: calling deserialize hook
ember.debug.js:49980Transition #1: application: calling afterModel hook
ember.debug.js:49980Transition #0: detected abort.
ember.debug.js:49980Transition #1: login: calling beforeModel hook
ember.debug.js:49980Transition #1: login: calling deserialize hook
ember.debug.js:49980Transition #1: login: calling afterModel hook
ember.debug.js:49980Transition #1: Resolved all models on destination route; finalizing transition.
ember.debug.js:24398Rendering application with <hlsaas@view:application::ember396> Object {fullName: "view:application"}
ember.debug.js:24398Rendering login with <hlsaas@view:login::ember405> Object {fullName: "view:login"}
ember.debug.js:49980Transition #1: TRANSITION COMPLETE.
ember.debug.js:25017generated -> route:help.password Object {fullName: "route:help.password"}
ember.debug.js:25017generated -> route:help Object {fullName: "route:help"}
:4200/login:1 GET http://localhost:4200/assets/bootstrap-select.css.map 404 (Not Found)
VM6074:66 Ember Inspector Active
jquery.js:9659 XHR finished loading: POST "http://localhost:4200/api/authenticate".
application.js:11 sessionAuthenticationSucceeded called
application.js:14 YES transition object found:  Transition {state: TransitionState, intent: C, router: Router, data: Object, resolvedModels: Object…}
application.js:11 sessionAuthenticationSucceeded called
application.js:18 NO transition object found
ember.debug.js:25017 generated -> route:dashboard.index Object {fullName: "route:dashboard.index"}
ember.debug.js:25017 generated -> route:dashboard Object {fullName: "route:dashboard"}
ember.debug.js:49983 Attempting transition to dashboard
ember.debug.js:49980 Transition #2: assets.overview: transition was aborted
ember.debug.js:49980 Transition #3: authenticated: calling beforeModel hook
ember.debug.js:49980 Transition #2: detected abort.
ember.debug.js:49980 Transition #3: authenticated: calling deserialize hook

@marcoow
Copy link
Member

marcoow commented Apr 1, 2015

@tehviking: can you update to 0.7.3 first to make sure the problem still exists there?

@tehviking
Copy link

On checking again, I thought I was on 0.7.2 but I was on 0.7.3 the whole time.
I had "ember-cli-simple-auth": "^0.7.2" in package.json, and that had resolved to 0.7.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants