Saving a user object in session storage

I use Angular 2 and Typescript and wanted to save the user object as a global variable so that it would not be retrieved multiple times. I found the session store and now save the user object there.

Do you think it’s good practice to store it there or the data is too sensitive? If so, what other kind of cache could I use?

Here is the code I'm using right now:

user.service.ts:

getProfile() { let cached: any; if (cached = sessionStorage.getItem(this._baseUrl)) { return Observable.of(JSON.parse(cached)); } else { return this.http.get(this._baseUrl).map((response: Response) => { sessionStorage.setItem(this._baseUrl, response.text()); return response.json(); }); } } 

GetProfile () is called in app.component when ngOnInit (). A custom object is also needed in other components of the application.

+5
source share
2 answers

It is good to use secure / confidential data in session storage.

Since the session store is only available for the current table and domain ...

If the user checks the same session store data in another tab of the window, then he will not be there ... so his safe store ....

If you want to know more, look at sessionStorage

+2
source

You can use sessionStorage or use service .

Your interface :

 export interface ISession { session:Object } 

Your actual service class:

  import {Injectable} from '@angular/core'; @Injectable() export class SessionService implements ISession { private _session: session constructor(){ } set session(value){ this._session = value; } get session(){ return this._session } } 

Now you can introduce this SessionService class into another constructors class and use it.

+2
source

Source: https://habr.com/ru/post/1262561/


All Articles