page refresh causing user to get log out automatically in angular js page refresh causing user to get log out automatically in angular js angularjs angularjs

page refresh causing user to get log out automatically in angular js


You are trying to save the data into localStorage, but you are not reading it.The currentUser variable you have the user data in is a regular javascript variable which gets reset when you reload the page.

You need to do something like this:

// ...// user logs in, remember it into the local storage// Note: is is better to use it via $window.localStorage$window.localStorage.setItem('user', JSON.stringify(user));this.currentUser = user;//...// function to get the user, if this.currentUser is not set,// try to load from the local storagegetUser: function() {  if (this.currentUser) {      return this.currentUser;  }  var storageUser = $window.localStorage.getItem('user');  if (storageUser) {    try {      this.user = JSON.parse(storageUser);    } catch (e) {      $window.localStorage.removeItem('user');    }  }  return this.currentUser;}// you may also want to remove the user data from storage when he logs outlogout: function() {    $window.localStorage.removeItem('user');    this.currentUser = null;},


you need to create a session for login so that it does not log out after page refresh. have a look at this link:

http://maffrigby.com/maintaining-session-info-in-angularjs-when-you-refresh-the-page/