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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is this secure like php $_SESSION
Nop, it's not as secure. Take a look at this : stackoverflow.com/questions/39366673/…
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

Comments

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

4 Comments

Is this secure method?
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.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.