Angularjs ui-router. How to redirect to login page

I have 4 states: panel , dahboard.main , dashboard.minor , login . the dashboard is abstract and is the parent state for the .minor and .main states. Below is my code:

.state('dashboard', { url: "/dashboard", abstract: true, templateUrl: "views/dashboard.html", resolve: { auth: function ($q, authenticationSvc) { var userInfo = authenticationSvc.getUserInfo(); if (userInfo) { return $q.when(userInfo); } else { return $q.reject({ authenticated: false }); } } }, controller: "DashboardCtrl", data: { pageTitle: 'Example view' } }) .state('dashboard.main', { url: "", templateUrl: "views/main.html", controller: "DashboardCtrl", data: { pageTitle: 'Main view' } }) 

As you can see in the control panel state, I have a permission option. In this case, I would like to redirect the user to the login page if he is not authorized. For this reason, I use a special authenticationSvc service:

 .factory("authenticationSvc", ["$http", "$q", "$window", function ($http, $q, $window) { var userInfo; function login(email, password) { var deferred = $q.defer(); $http.post("/api/login", { email: email, password: password }) .then(function (result) { if(result.data.error == 0) { userInfo = { accessToken: result.data.accessToken }; $window.sessionStorage["userInfo"] = JSON.stringify(userInfo); deferred.resolve(userInfo); } else { deferred.reject(error); } }, function (error) { deferred.reject(error); }); return deferred.promise; } function getUserInfo() { return userInfo; } return { login: login, logout: logout, getUserInfo: getUserInfo }; }]); 

I check the auth value in config :

 .run(function($rootScope, $location, $state) { $rootScope.$state = $state; $rootScope.$on("routeChangeSuccess", function(userInfo) { consol.log(userInfo); }); $rootScope.$on("routeChangeError", function(event, current, previous, eventObj) { if(eventObj.authenticated === false) { $state.go('login'); } }); }); 

But unfortunately, when I go to my root or dashboard site, I get a blank page. What is wrong with this code? Thank!

+55
angularjs state config angular-ui-router resolve
Nov 30 '14 at 10:18
source share
2 answers

The fact is that do not redirect if you do not need === if you are already redirected to the intended state. There is a working plunger with a similar solution.

 .run(function($rootScope, $location, $state, authenticationSvc) { $rootScope.$on( '$stateChangeStart', function(e, toState , toParams , fromState, fromParams) { var isLogin = toState.name === "login"; if(isLogin){ return; // no need to redirect } // now, redirect only not authenticated var userInfo = authenticationSvc.getUserInfo(); if(userInfo.authenticated === false) { e.preventDefault(); // stop current execution $state.go('login'); // go to login } }); }); 

Check them out for a similar explanation:

  • Angular ui router - redirect does not work at all
  • How can I fix the "Maximum Call Stack Size" of AngularJS
+76
Nov 30 '14 at 16:12
source share

Since you are using the UI-Router module, you should use the events $stateChangeStart , $stateChangeSuccess .

Check out this link for more: https://github.com/angular-ui/ui-router/issues/17

There is also a typo in consol.log(userInfo) in the console .

Check out the console in your chrome dev tools. This will give an idea if something else is missing.

+4
Nov 30 '14 at 13:30
source share



All Articles