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
-
-
Nop, it's not as secure. Take a look at this : stackoverflow.com/questions/39366673/… Nov 6, 2017 at 13:30
you can use local storage for this purpose... refer this link give below for local storage in angular 4
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
-
-
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.– ShaneNov 6, 2017 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?– RijoNov 6, 2017 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. Nov 6, 2017 at 17:06