1

I am using $window.sessionStorage.isLoggedIn variable for handling logged in in angularjs as follows,

$routeProvider
        .when('/', {
            templateUrl: 'app/components/main/dashboard.html',
            controller: 'dashboardController',
            resolve: {
                loggedIn: function ($location, $window) {
                    console.info($window.sessionStorage.isLoggedIn)
                    if (!$window.sessionStorage.isLoggedIn) {
                        $location.path('/login');
                    }
                }
            };
        })

Normally it is working fine,but when I am opening my web app in new tab in same browser,the value of $window.sessionStorage.isLoggedIn will be undefined and redirecting to login page, meanwhile in my previous tab,my app is working as logged in . Why it is happening? Local storage is tab independent?

gsk
  • 2,329
  • 8
  • 32
  • 56

1 Answers1

1

The value of sessionStorage does not persist between tabs. You can use localStorage to do it.

A weakness of the localStorage is that you cannot set an expire date for it. So in some cases the use of cookies is the best solution for storing data.

So, I prefer to use $cookies:

angular.module('cookiesExample', ['ngCookies'])
.controller('ExampleController', ['$cookies', function($cookies) {
  // Retrieving a cookie
  var favoriteCookie = $cookies.get('myFavorite');
  // Setting a cookie
  $cookies.put('myFavorite', 'oatmeal');
}]);

To learn more about localStorage and sessionStorage check out this link: HTML5 Local storage vs. Session storage

Community
  • 1
  • 1
Wédney Yuri
  • 1,267
  • 12
  • 21