javascript - Stay on the current state whenever the page reloads, if the user token is in localstorage in AngularJS -


i working on token based authentication in angularjs, have stuck in 1 issue. issue whenever refreshes state goes login page token still in localstorage. want whenever loggedin user refreshes state stay on same state.

here code user authenticaton service.

function login(username, password) {   return $http.post(config.apiurl + 'token', 'grant_type=password&username=' + username + '&password=' + password, {     'content-type': 'application/x-www-form-urlencoded'   }); } 

my login controller

function login() {   if(!(vm.user.name === config.users[0].name) && !(vm.user.password === config.users[0].password)){     vm.error = true;     toastr.error('the user name/password incorrect.', 'error');     return false;   }   userservice.login(vm.user.username, vm.user.password).then(function (response) {     vm.token = angular.copy(response.data);     userservice.settoken(vm.token);     if(response.status === 200){       toastr.success('successfylly logged in', 'success!');       userservice.setauthorizeduser(true);       userservice.getuser(vm.token).then(function (res) {         userservice.setuser(res.data);         $state.go('root.teams.dashboard');       }, function (err) {         console.log(err);       });     }   }, function (err) {     console.log(err);    }); 

and routes

.state('root.teams', {     url: '/teams',     abstract: true,     template: '<div ui-view></div>',     onenter: ['userservice', '$state', function (userservice, $state) {       if (!userservice.isauthorizeduser()) {         $state.go('login');       }     }]   }) 

let me know if missed something. in advance.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -