In the client side authentication of embercasts, have a nice way to handle it:
Basically, you have a App.AuthenticatedRoute
, where all routes that need authentication will extend. In the beforeModel
hook, is checked if the user is authenticated, in that case the presence of the token. If the token isn't present the current transition is stored.
App.AuthenticatedRoute = Ember.Route.extend({
...
beforeModel: function(transition) {
if (!this.controllerFor('login').get('token')) {
this.redirectToLogin(transition);
}
},
redirectToLogin: function(transition) {
alert('You must log in!');
var loginController = this.controllerFor('login');
loginController.set('attemptedTransition', transition);
this.transitionTo('login');
}
...
});
When the login is performed and valid, the previous transition is take by self.get('attemptedTransition')
, and is called retry
. This will retry the transition, in the case, the transition where the user attempted go to, before the login authentication redirect:
...
var attemptedTransition = self.get('attemptedTransition');
if (attemptedTransition) {
attemptedTransition.retry();
self.set('attemptedTransition', null);
} else {
// Redirect to 'articles' by default.
self.transitionToRoute('articles');
}
...
With this, you will have the same behavior.
I hope it helps.