0

I am storing one session value on local storage on angular 4 app using the below code.

           this.http.post('http://localhost/salesneuronver4/Login/api/rest/Login/',value,
                 { headers: this.headers}).map((res: Response) => res.json())
                 .subscribe(
                  res => { this.resnonseValue = res;
                   if(this.resnonseValue.status == 'success'){
                     this.getCookie();


                        // here I am storing session value 

                   localStorage.setItem('userSession', this.resnonseValue.session_id); 

                        // and its stored for current browser session

                     this.router.navigate(['/home']); 
                     return false;

                   }else if(this.resnonseValue.status == 'expired'){
                     this.router.navigate(['/login']);
                     return false;
                   }
                   else{
                     this.errorMsg = this.resnonseValue.status;
                   }
                 },
                  err =>console.error(err)
                 );
             }

Now When I close the browser tab or whole browser, the locally stored item userSesssion is lost. How to keep it persistent until the user has logged out?

  • 1
    Use sessionStorage instead – Vinod Bhavnani Jan 30 '18 at 07:15
  • I tried that thing but here I find this "localStorage and sessionStorage accomplish the exact same thing and have the same API, but with sessionStorage the data is persisted only until the window or tab is closed, while with localStorage the data is persisted until the user manually clears the browser cache or until your web app clears the data. The examples in this post are for localStorage, but the same syntax works for sessionStorage." https://alligator.io/js/introduction-localstorage-sessionstorage/ – Talk is Cheap Show me Code Jan 30 '18 at 07:28
  • Data set in localStorage should persist for some time: https://stackoverflow.com/questions/9948284/how-persistent-is-localstorage/9948339. Are you sure you don`t clear localStorage anywhere in your app before user closes tab/browser? – Mateusz Witkowski Jan 30 '18 at 07:32

0 Answers0