Skip to content

Rejected state transition leaves unsynchronized url #242

@Danita

Description

@Danita

Given the following states:

$stateProvider.state('account', {
    url: "/account",
    templateUrl: 'app/account/account.html',
    controller: 'AccountCtrl',
    data : {
        access : "EVERYBODY"
    }
});
$stateProvider.state('contacts', {
    url: "/contacts",
    templateUrl: 'app/contacts/contacts.html',
    controller: 'ContactsCtrl',
    data : {
        access : "LOGGED_IN"
    }
});

I'm trying to prevent anonymous users from visiting any LOGGED_IN states. So I'm setting this up in the main application controller:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if ( toState && toState.data && toState.data.access == "LOGGED_IN" && !user.loggedIn() ) {
        event.preventDefault();
        $state.transitionTo('account');
        return false;
    }
})

So far it works -- the user is redirected to the account state if he's not logged in; but the resulting url in the browser is that which would have been if the user transitioned to one of the "forbidden" states. How can I prevent this from happening?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions