How to pass url to $stateChangeStart of ui-router

Warpasaur

I use the following to redirect routes to login if a user is not logged in:

angular.module('app').run(['$rootScope', '$location', '$state', 'AuthService', 
    function($rootScope, $location, $state, AuthService) {

    /* redirect to login if not logged in */
    $rootScope.$on( '$stateChangeStart', 
        function(e, toState, toParams, fromState, fromParams) {
            if(toState.name === "login"){
               return; // already going to login 
            }

            if(!AuthService.user) {
                e.preventDefault(); 
                $state.go('login'); 
            }
        });
}]);

How can I pass the url from the "prevented" state to the login state so that I can continue navigating to that state once logged in?

Example: User clicks link for myapp.com/#/path/to/data --> redirects to myapp.com/#/login --> user logs in --> user routed to myapp.com/#/path/to/data

I basically need /path/to/data to be sent to my login controller so that I can handle that. I cannot seem to find '/path/to/data' in any of the state or param variables in the $stateChangeStart listener.

Anid Monsur

The $state service has an href method which can create a URL given the state and its parameters. Since those are provided to the state change listener, you can create a URL and then pass it as a param to the login state.

$rootScope.$on('$stateChangeStart',
        function(e, toState, toParams, fromState, fromParams) {
            if (toState.name === "login") {
                return; // already going to login
            }

            if (!AuthService.user) {
                e.preventDefault();

                var redirectUrl = $state.href(toState.name, toParams);

                $state.go('login', {
                    redirect: redirectUrl
                });
            }
        });

You'll need to add a redirect parameter to your login state so you can use that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I access the 'resolve' property of a UI-Router root state from $stateChangeStart?

From Dev

UI Router- stateChangeStart fires like 50 times

From Dev

Why does AngularJS with ui-router keep firing the $stateChangeStart event?

From Dev

Angular UI Router state.go parameters not available in stateChangeStart

From Dev

Using $state methods with $stateChangeStart toState and fromState in Angular ui-router

From Dev

how to pass in state parameter to ui-router?

From Dev

UI Router: How to pass parameters to nested views

From Dev

UI-Router Let URL Pass when not defined in a state

From Dev

angular.js ui-router pass variable to state url

From Dev

How to make URL simplier in UI-router

From Dev

Ui-Router $state.go inside $on('$stateChangeStart') is cauzing an infinite loop

From Dev

Angular 1.4.1 UI Router 10 $digest() iterations when $state.go called on $stateChangeStart

From Java

How to pass an object into a state using UI-router?

From Dev

How to pass $stateParams from ui-router to service in resolve?

From Dev

How do to pass parameters to sibling ui-router named views?

From Dev

How to pass data between angular ui-router states?

From Dev

Angular-ui-router: how to pass parameters to $state.get?

From Dev

Express, how to pass long url path to react router?

From Dev

How do I pass an url as parameter in router.get() method?

From Dev

how to remove hash tag (#) from url address in ui-router

From Dev

How to change the URL when paging using UI Router?

From Dev

ui.router: how to omit a default parameter from URL

From Dev

How to make img source url working with ui-router

From Dev

angular ui-router, how to nest states with a url that updates

From Dev

How to add search parameter to the URL using AngularJS UI Router .go()?

From Dev

angular ui-router, how to nest states with a url that updates

From Dev

How to make img source url working with ui-router

From Dev

How to append query strings to the URL in Angular ui-router

From Dev

How can I inject a ui router's url parameter into the controller

Related Related

  1. 1

    How do I access the 'resolve' property of a UI-Router root state from $stateChangeStart?

  2. 2

    UI Router- stateChangeStart fires like 50 times

  3. 3

    Why does AngularJS with ui-router keep firing the $stateChangeStart event?

  4. 4

    Angular UI Router state.go parameters not available in stateChangeStart

  5. 5

    Using $state methods with $stateChangeStart toState and fromState in Angular ui-router

  6. 6

    how to pass in state parameter to ui-router?

  7. 7

    UI Router: How to pass parameters to nested views

  8. 8

    UI-Router Let URL Pass when not defined in a state

  9. 9

    angular.js ui-router pass variable to state url

  10. 10

    How to make URL simplier in UI-router

  11. 11

    Ui-Router $state.go inside $on('$stateChangeStart') is cauzing an infinite loop

  12. 12

    Angular 1.4.1 UI Router 10 $digest() iterations when $state.go called on $stateChangeStart

  13. 13

    How to pass an object into a state using UI-router?

  14. 14

    How to pass $stateParams from ui-router to service in resolve?

  15. 15

    How do to pass parameters to sibling ui-router named views?

  16. 16

    How to pass data between angular ui-router states?

  17. 17

    Angular-ui-router: how to pass parameters to $state.get?

  18. 18

    Express, how to pass long url path to react router?

  19. 19

    How do I pass an url as parameter in router.get() method?

  20. 20

    how to remove hash tag (#) from url address in ui-router

  21. 21

    How to change the URL when paging using UI Router?

  22. 22

    ui.router: how to omit a default parameter from URL

  23. 23

    How to make img source url working with ui-router

  24. 24

    angular ui-router, how to nest states with a url that updates

  25. 25

    How to add search parameter to the URL using AngularJS UI Router .go()?

  26. 26

    angular ui-router, how to nest states with a url that updates

  27. 27

    How to make img source url working with ui-router

  28. 28

    How to append query strings to the URL in Angular ui-router

  29. 29

    How can I inject a ui router's url parameter into the controller

HotTag

Archive