Search code examples
angularjsangular-ui-routerangular-ui-router-extras

Registering future state after login using ui-router-extras-future-state module


Myself trying to register a future state after login based on user type.

For example: If user_type is buyer, then I need to register all routers that I maintained from futureStates_buyer.json

If user_type is admin, then I need to register all routers that I maintained from futureStates_admin.json

Experiment 1 (As per sample)

Sample URL: http://christopherthielen.github.io/ui-router-extras/example/future/

I can set the following configuration in config phase. I understand that provider settings should be inside config phase.

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

 $futureStateProvider.stateFactory('ngload', ngloadStateFactory); 
 $futureStateProvider.addResolve(loadAndRegisterFutureStates);

});

Working demo: http://plnkr.co/edit/9dUm0f6g2V5PM4m7YlTD?p=preview

Experiment 2 (This is what I am trying)

but, In my case, I don't want to register all the routers for the entire application in config phase. it will be very high configuration. So what I am trying is, once logged in, I can get the user type in login response, based on that user type, I need to register the future states.

//config phase
app.config(function($stateProvider, $urlRouterProvider, $futureStateProvider) {

 //Planned to use futureStateProvider later in controller
  lazyLoad = {stateProvider: $stateProvider, futureStateProvider:$futureStateProvider};

});


//Login controller
app.controller('LoginController', ['$scope', '$rootScope', '$state', '$http', function($scope, $rootScope, $state, $http) {

     $scope.doLogin = function(){

            //After successfull login
            //Service invocation comes here
            console.log("Logged in!!");
            $rootScope.userInfo = {"loggedIn" : true };
            $state.go('dashboard');

            //Now I want to register future state based on login response i.e user_type
            // If user type is buyer, I want to registrer futureStates_buyer.jsom
            // If user type is vendor, I want to registrer futureStates_vendor.jsom

            lazyLoad.futureStateProvider.stateFactory('ngload', ngloadStateFactory); // register AngularAMD ngload state factory
            lazyLoad.futureStateProvider.addResolve(loadAndRegisterFutureStates);

        }

}]);

Not working: http://plnkr.co/edit/pCEKLopOqSKghermTHP8?p=preview

Any help would be much appreciated. Thanks in advance :)


Solution

  • I have fixed the issue by changing the code in controller section as follows and it is working fine.

       //Login controller
        app.controller('LoginController', ['$scope', '$rootScope', '$state', '$http', function($scope, $rootScope, $state, $http) {
            $scope.login = {"username":"admin", "password":"admin123"};
    
            $scope.doLogin = function(){
    
                //After successfull login
                //Service invocation comes here
                console.log("Logged in!!");
                $rootScope.userInfo = {"loggedIn" : true };
    
    
                //Now I want to register future state based on login response i.e user_type
                // If user type is buyer, I want to registrer futureStates_buyer.jsom
                // If user type is vendor, I want to registrer futureStates_vendor.jsom
    
                /*          
                var loadAndRegisterFutureStates = function ($http) {
                  // $http.get().then() returns a promise
                  return $http.get('futureStates_buyer.json').then(function (resp) {
                    angular.forEach(resp.data, function (fstate) {
                      // Register each state returned from $http.get() with $futureStateProvider
                      lazyLoad.futureStateProvider.futureState(fstate);
                    });
                  });
                };
                */
    
                $http.get('futureStates_buyer.json').then(function (resp) {
                    angular.forEach(resp.data, function (fstate) {
                      // Register each state returned from $http.get() with $futureStateProvider
                      lazyLoad.futureStateProvider.futureState(fstate);
                  });
    
                  $state.go('dashboard');
          });
    
                lazyLoad.futureStateProvider.stateFactory('ngload', ngloadStateFactory); // register AngularAMD ngload state factory
                //lazyLoad.futureStateProvider.addResolve(loadAndRegisterFutureStates);
    
            }
    
        }]);
    

    Working demo: http://plnkr.co/edit/ZXty2SkmGbuwnpaJoh7C?p=preview