javascript - State routes back to previous page even after login -


after refreshing page, state changes login. using $sessionstorage store data , able print them after login, when refresh page, page should stay on dashboard.html routes login state.

angular.module('app', [     'nganimate',     'ngcookies',     'ngresource',     'ngsanitize',     'nganimate',     'ngtouch',     'ui.bootstrap',     'ui.router',     'toaster',     'ngstorage' ]) .config(function ($stateprovider, $urlrouterprovider) {      $stateprovider             .state('login', {                 url: '/login',                 templateurl: "views/login.html"             })             .state('dashboard', {                 url: '/dashboard',                 templateurl: "views/dashboard.html",                 required: true             });      $urlrouterprovider.otherwise('/login');  }) .run(["$rootscope", "$state", "$location", "authservice", "$cookies", "$sessionstorage", function ($rootscope, $state, $location, authservice, $cookies, $sessionstorage) {         $rootscope.$on('$statechangestart', function (e, tostate, toparams, fromstate, fromparams) {              if (tostate.required && !$sessionstorage.authuserid) {                 alert("state not authenticated");                 e.preventdefault();                 $state.go('login');             }         });     }]); 

here controller:

angular.module('app') .controller('logincontroller', ['$scope', '$http', '$timeout', '$location', '$rootscope', 'authevents', 'authservice', '$cookies', '$state', 'usermanagementfactory', '$localstorage', '$sessionstorage', function ($scope, $http, $timeout, $location, $rootscope, authevents, authservice, $cookies, $state, usermanagementfactory, $localstorage, $sessionstorage) {      $scope.login = function () {          authservice.login(data).then(function (response) {             $rootscope.$broadcast(authevents.loginsuccess);             console.log("response data is: ", response);             $sessionstorage.auth = response;             $sessionstorage.authname = response.data.name;             $sessionstorage.authrole = response.data.roles[0].name;             $sessionstorage.authuserid = response.data.user_id;             $sessionstorage.authprofilepic = response.data.profile_picture;              if ($sessionstorage.authuserid) {                 $state.go('dashboard');             } else {                 alert('user not authenticated');             }          }, function (error) {              $scope.responsemessage = error.data.error;             $rootscope.$broadcast(authevents.loginfailed);         }         )     } }]) 

html:

<div ng-controller="logincontroller">     <header class="main-header" ng-include src="'views/header.html'"></header><!--/main top header -->     <div ui-view></div> </div> 

i not sure causing change route on page refresh?

stateconfig doesnt seem accept 'required' field, please refer https://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateprovider

instead should have

.state('dashboard', {                 url: '/dashboard',                 templateurl: "views/dashboard.html",                 data: {                    required: true                 }             }); 

and should check this.

if (tostate.data && tostate.data.required && !$sessionstorage.authuserid) { 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -