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.
source share