1

I have been working on implementing user authentication in my angular application. I implemented in a way that stores the token in local storage.

This is my code that is executed when a user has entered user credentials and pressed submit.

this.http.post('http://localhost:3000/users/login', this.user).subscribe(data => {
  if (data['token']) {
    localStorage.setItem('mean-token', data['token']);
    location.reload();
    this.router.navigate(['/']);
  }
});

My application stores the token in the local storage and the token does not get deleted when the browser is closed. I want the token to be deleted when the browser is closed. How can I do that?


Question update: I understand that I need to store the data in session storage instead of local storage, but I have not been able to find how to do that in Angular 4. I would like to know how it is done in development with Angular 4.

M. Kim
  • 48
  • 5

1 Answers1

4

"I want the token to be deleted when the browser is closed."

localStorage saves session forever, but SessionStorage closes session when a browser is closed.

https://code.google.com/p/sessionstorage/

P.S.
  • 15,970
  • 14
  • 62
  • 86
  • Thanks Commercial Suicide for your desire to help. I think I should have been more clear with my question. I will update my question. The question I wanted to ask was how I could store data in session storage instead of local storage in angular 4. I searched how I could do it but I have not been able to find it. – M. Kim Oct 08 '17 at 17:00
  • @M.Kim, I think this post will help you: https://stackoverflow.com/questions/39840457/how-to-store-token-in-local-or-session-storage-in-angular-2 – P.S. Oct 08 '17 at 17:04