1

I'm having a simple blog application. I defined some states for the routing and also defined that on each state-change event it will check the authentication level (in a very naive manner for now) and if the user is allowed to go to this state or not.

app.run(function($rootScope, $state, UserService){
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if (!UserService.getCurrentUser()){
        $state.go('app.login');
    }
});

My JS entry point include this configuration:

app.config(function($stateProvider, $urlRouterProvider){

    $stateProvider.state('app', {
        url: '',
        abstract: true
    });
});

I want that when ever going into my application the main route will be the blogs "feed" (and of course when trying to get there it will also fire the $stateChangeStart event so that the authentication will be verified. How can I achive that?

1 Answer 1

2

You can use otherwise method:

app.config(function($stateProvider, $urlRouterProvider){

    $urlRouterProvider.otherwise('/feed');

    $stateProvider.state('app', {
        url: '',
        abstract: true
    });
});

Don't forget that /feed must also be defined with $stateProvider.state

Sign up to request clarification or add additional context in comments.

2 Comments

I think that in this way the $stateChangesStart is not fired and therefore the function that is checking if the user is authenticated is not getting called.
Use $urlRouterProvider more and load your state when /feed like this app.config(function($stateProvider, $urlRouterProvider){ $urlRouterProvider.otherwise('/feed'); $urlRouterProvider.when('/feed','your state configuration'); $stateProvider.state('app', { url: '', abstract: true }); }); So it will fire your event,too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.