Routes redirecting without transition history saving


#1

Hello!

Trying to make an authorization in a server-like-app approach and cant cope with transition.

I have the configuration similar to this topic author.

App.Router.map(function() {
 this.route('login', { path: '/' });
 this.route('home');
 this.route('settings');
 this.route('help');    
});

App.HomeRoute = Ember.Route.extend({
redirect: function() {
    if (!App.User.loggedIn) {
        this.transitionTo('login');
    }
}
});

When I am actually logged in and trying to access / route, it must redirect me to another default route (let it be ‘home’ to clarify). I can do it using transitionTo in the same way

App.LoginRoute = Ember.Route.extend({
redirect: function() {
    if (App.User.loggedIn) {
        this.transitionTo('home');
    }
}
});

The problem is that after this I can press the “back” button and it will transit me to the login route again. Its okay because I will be redirected to home immediately but it seems like a dead loop.

User never could back to the page he came from. So scaring…


#2

You can use the replaceWith method on Routes. See the documentation here: http://emberjs.com/api/classes/Ember.Route.html#method_replaceWith

It’s effectively the same as transitionTo but won’t add an item to the browser’s history.


#3

@brett

Works great for me! Thank you! This aspect is not reflects in guides well enough.