angularjs - Javascript Object abnormality in Angular application refresh/ deep link from address bar -


i using angular 1.3.15 , ui-router, hosted on iis 7.5.

the following code contains setup $statechangestart event. when app has been loaded main link, code invoked correctly; when user accesses state role, there no issue. when try hit link manually via address bar or refresh current page on , application reloads, function runs property on authentication.profile object empty. you'll see doing console.dir(authentication.profile) when $statechangestart first fires off. shows there indeed data there, , methods on object. if try console.dir(authentication.profile.token), empty.

i unsure if related refresh of app different path or totally different.

any appreciated.

'use strict';  var serviceid = 'authentication'; angular.module('app').factory(serviceid,         ['common', '$localforage', '$injector', authentication]);  angular.module('app').run(['$rootscope','$state', '$stateparams',    'authentication', function ($rootscope,$state, $stateparams, authentication) {     $rootscope.$on('$statechangestart',          function (e, tostate, toparams, fromstate, fromparams) {         console.dir(authentication.profile);         if (tostate.data.roles.length > 0) {             console.log('has roles');             console.dir(authentication.profile.roles());             if (!authentication.profile.isinanyrole(tostate.data.roles)) {                 e.preventdefault();                 $state.go('home');             }         }     }); }]); 

authentication service:

function authentication(common, $localforage, $injector) {     var userkey = "utoken";      var setprofile = function (username, token) {         profile.username = username;         profile.token = token;         localforage.setitem(userkey, {               'username': username,               'token': token          })     };      var initialize = function () {         var user = {             username: "",             token: "",              isauthenticated: function () {                 return this.token;             },             isuserinrole : function (role) {                 if (this.token != "") {                     var decoded = jwt_decode(this.token);                     return decoded.role.indexof(role) != -1;                 }                 else return false;              },             isinanyrole: function (roles) {                  (var = 0; < roles.length; i++) {                     if (this.isuserinrole(roles[i])) return true;                 }                  return false;             },             roles: function(){                 if (this.token != "") {                     var decoded = jwt_decode(this.token);                     return decoded.role;                 }                 else return false;             },             istokenexpired: function () {                 var decoded = jwt_decode(this.token);                 if (moment.unix(decoded.exp).isbefore( moment())) {                     return true;                 }                 else                     return false;             }         };            var localuser = null;         $localforage.getitem(userkey).then(function (data) {             localuser = data;             if (localuser) {                 user.username = localuser.username;                 user.token = localuser.token;                 if (user.istokenexpired())                     logout();             }         });           return user;     };      var logout = function () {         profile.username = "";         profile.token = "";         $localforage.removeitem(userkey);         $injector.get('$state').transitionto('home');     };      var profile = initialize();      return {         setprofile: setprofile,         profile: profile,         logout: logout     }; }; 

which behavior expect? when reload page or navigate url 'manually' app starts scratch. , never call authentication.setprofile().

to keep session, once logged in, on reload or manual navigation have keep data offline usage. can use cookies or localstorage service.

then in state tracking code read storage/cookies once detect missing profile , attempt restore storage/cookie


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -