Routes redirecting without transition history saving

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ā€¦

1 Like

You can use the replaceWith method on Routes. See the documentation here:

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

3 Likes

@brett

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