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

Removing the States after logout #2711

Closed
ashiquemohammed opened this issue Apr 26, 2016 · 7 comments
Closed

Removing the States after logout #2711

ashiquemohammed opened this issue Apr 26, 2016 · 7 comments
Milestone

Comments

@ashiquemohammed
Copy link

I am facing this issue mentioned in page . I am building states dynamically using $futureStateProvider , and need to remove the states once the user logs out . Or else , the states will be defined again on login , and it breaks . Hoping for a solution.

THanks

@eddiemonge
Copy link
Contributor

The page is inaccessible. Also needs a plnkr example.

@ashiquemohammed
Copy link
Author

ashiquemohammed commented May 13, 2016

Sorry for mentioning a dead link , The issue is that ,

I have used Future State Provider to build the states dynamically and now trying to remove/reset the states once the user logs out . How do i achieve that ,

The code for building the states are given below ,

var buildStates = function ($http, $state) {

      return $http.get(apiHost + 'getRoutes').then(function (result) {
        if (result.data.response != false) {

          angular.forEach(result.data.response, function (menus) {
            angular.forEach(menus.menu, function (state) {
              var assets = [];
              var controllerName = state.controller_path.split('/').reverse()[0].replace('.js', '');
              if (state.asset) {
                assets = state.asset.map(function (item) {
                  return item.asset;
                })
              }

              assets.push(state.controller_path);

              var newState = {
                url: state.url.substring(1),
                data: {tree: [menus.module.name, state.name]},
                reloadOnSearch: true,
                resolve: {
                  deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                    return $ocLazyLoad.load({
                      name: 'SomeApp',
                      files: assets
                    });
                  }]
                },
                views: {
                  'main_content': {
                    templateUrl: state.partial_path,
                    controller: controllerName,
                  }
                }
              }
              $stateProvider.state('parent.' + state.state_name, newState)
              //  }
            });
          });
        } else {
          $state.go('login');
        }
      })
    }

    // Define the future states
    $futureStateProvider.addResolve(buildStates);```

So once the user logsout , I need to remove all the states , or else what happens is that , if he logs in again , the routes will be defined again , which gives an error . Hoping you can help me out . 

@ghost
Copy link

ghost commented May 13, 2016

There are hooks into ui-router that would work just fine:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
    //Validate user's authentication
   /// ...

   if (true === isUserAuthenticated && 'login' === toState) {
       event.preventDefault();
     //Send user to your primary route since they're already logged in, and attempting to hit the login route.
   }

   if (false === isUserAuthenteicated) {
       event.preventDefault();
       $rootScope.$broadcast('unauthorized'); //or something that indicates to your app that you should go to a login screen.
   }
});

@ashiquemohammed
Copy link
Author

@killerspaz I am not facing an issue to redirect user to the login page , I need to remove the dynamically created states once the user logs out .

@christopherthielen
Copy link
Contributor

I would like to support this in the future, but it's not currently possible to remove state definitions.

@christopherthielen
Copy link
Contributor

This is a prerequisite for #2739

@gochinj
Copy link

gochinj commented Jul 9, 2016

I have a mod that adds a removeState method to $stateProvider and a remove method to $urlRoutesProvider that removes the associated rule. My problem is I forked the most recent version from GitHub, but the project won't build. I have opened an issue waiting for a response.

In the interim I just did a 'copy pasta' of the distro version of the angular-ui-router.js and made the changes in there. Once I can build, I will make the changes to the TS files and make a pull request.

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

No branches or pull requests

4 participants