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.
Asked
Active
Viewed 1.1k times
3 Answers
2
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

Boulboulouboule
- 4,087
- 1
- 13
- 29
-
Is this secure like php $_SESSION – Rijo Nov 06 '17 at 13:26
-
Nop, it's not as secure. Take a look at this : https://stackoverflow.com/questions/39366673/is-it-safe-to-store-a-sensitive-data-in-local-stoarge-or-session-storage-locals – Boulboulouboule Nov 06 '17 at 13:30
1
you can use local storage for this purpose... refer this link give below for local storage in angular 4

Jay Patel
- 305
- 2
- 6
- 20

Pratik Vekariya
- 45
- 8
1
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

Shane
- 3,049
- 1
- 17
- 18
-
-
What are you trying to accomplish? Storing the value server or client sided? If you want to persist changes made in the client side to your php backend, you will have to use xhr like any other webapp. sessionStorage or localStorage stores the values per browser. localStorage is persist by cookies and cache, sessionStorage will be deleted on closing the browser. – Shane Nov 06 '17 at 13:31
-
I want to store values in server side. customer login id as session my session variable. will I face any security issue in future it will store in client side? – Rijo Nov 06 '17 at 15:39
-
1The datas aren't sahred between browsers, so if it's only a customer ID, I think it's okay, But if you want to store sensitives datas, you should at least encrypt them. – Boulboulouboule Nov 06 '17 at 17:06