I want to store user session value in Angular application after login. I have php background, in PHP store session value using $_SESSION, for the same manner how to store session value Angular4.
3 Answers
In Angular you can use LocalStorage to store this kind of value :
localStorage.setItem('currentUser', yourUser);
Get the value back :
const user = localStorage.getItem('currentUser');
Remove an item from storage :
localStorage.removeItem('currentUser');
It's client side, so it's not as secure as the PHP Session
Source : https://developer.mozilla.org/nl/docs/Web/API/Window/localStorage
2 Comments
you can use local storage for this purpose... refer this link give below for local storage in angular 4
Comments
You will have to make use of either sessionStorage or localStorage.
Best practice would be to implement a (injectable) service to access the stored session variables anywhere in your Angular app.
Alternatively you can use libraries which make use of this approach such as Angular2 persistence service. The usage is self explanatory