2

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.

Rijo
  • 2,963
  • 5
  • 35
  • 62

3 Answers3

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
1

you can use local storage for this purpose... refer this link give below for local storage in angular 4

https://www.npmjs.com/package/angular-localstorage4

Jay Patel
  • 305
  • 2
  • 6
  • 20
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
  • Is this secure method? – Rijo Nov 06 '17 at 13:24
  • 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
  • 1
    The 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