Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.state data undefined on page refresh #2852

Closed
zymr-keshav opened this issue Jul 5, 2016 · 2 comments
Closed

.state data undefined on page refresh #2852

zymr-keshav opened this issue Jul 5, 2016 · 2 comments

Comments

@zymr-keshav
Copy link

as in new angular-ui-router v1.0.0alpha0 ,$stateChangeStart has been deprecated so using $transitions.on* hooks but not getting proper information where it has to written in .config block on in .run block?

I have tried to check authentications of HOME page in .config with below code


$stateProvider
        .state("home", {
            url: '/home',
            templateUrl: './home/home.html',
            controller: 'HomeController',
            requiresAuth: true,
            data : { pageTitle : 'Home', authorizedRoles: [USER_ROLES.admin, USER_ROLES.editor] }
 });
$transitionsProvider.onBefore({to: function(state) { return state.requiresAuth; } },
        function($transition$, $state, AuthService) {
            console.log(arguments);
            var from = $transition$.to().name;
            console.log(from);
            if(!AuthService.isAuthenticated()) {
                return $state.go("login", {referral : from});
            } else {
                console.log('I m logged in');
                var roles = $state.current.data.authorizedRoles;
                if(AuthService.isAuthorized(roles)) {
                    console.log('Authorized');
                }
            }
    });

it's working fine so far but when I refresh the same HOME page.. it gives error of undefined data property

see the plunker for the issue. when it asking for login, write anything in username and password

@christopherthielen
Copy link
Contributor

var roles = $state.current.data.authorizedRoles;

should probably be

var roles = $transition$.to().data.authorizedRoles;

@zymr-keshav
Copy link
Author

Thanks. I have one more confusion here

when to use .onBefore and when to use .onEnter or .onStart()

suppose user is logged in but not authorized then where do we write that logic? in resolve of .state ? or in .onStart or in onEnter()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants