From 1bcc40acb2334e731a9859922c1a9a9afc9bba00 Mon Sep 17 00:00:00 2001 From: FORESTIER Fabien <fabien.forestier@soprasteria.com> Date: Fri, 11 Oct 2019 16:34:13 +0200 Subject: [PATCH] Do not use localStorage to store userInfo but call auth service on app init --- src/app/app.module.ts | 9 +++++++-- src/app/user/services/user.service.ts | 22 ++++++---------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index db66563e..cf9faf2c 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -16,8 +16,13 @@ import { AppConfigService } from './core/services'; export function initUserService(authService: UserService) { return (): Promise<any> => { return new Promise((resolve, reject) => { - authService.initializeService(); - resolve(); + authService.initializeService().subscribe( + () => { + resolve(); + }, + () => { + resolve(); + }); }); }; } diff --git a/src/app/user/services/user.service.ts b/src/app/user/services/user.service.ts index 93882aae..3a0cda32 100644 --- a/src/app/user/services/user.service.ts +++ b/src/app/user/services/user.service.ts @@ -3,7 +3,6 @@ import { HttpClient } from '@angular/common/http'; import { Observable, Subject } from 'rxjs'; import { IUserInfo, PasswordUpdateForm, User, ILoginResponse, LegacyAccount, IPasswordForgottenForm } from '../models'; import { map, mergeMap, tap } from 'rxjs/operators'; -import * as JwtDecode from 'jwt-decode'; import * as NodeRSA from 'node-rsa'; import { APP_CONFIG } from '../../core/services/app-config.service'; @@ -20,16 +19,19 @@ export class UserService { } initializeService() { - this.setUserInfo(); + return this.getUserInfo().pipe( + map((userInfo) => { + this._user = new User(userInfo); + }), + ); } // Function and helpers allowing the management of the user session (jwt), info... setSession(authResult): boolean { let success = false; if (authResult && authResult.userInfo && authResult.xsrfToken) { - localStorage.setItem('userInfo', JSON.stringify(authResult.userInfo)); localStorage.setItem('xsrfToken', authResult.xsrfToken); - this.setUserInfo(); + this._user = new User(authResult.userInfo); success = true; this._userStatusChangedSubject.next(true); } else { @@ -39,19 +41,7 @@ export class UserService { return success; } - setUserInfo() { - const userInfo = JSON.parse(localStorage.getItem('userInfo')); - if (userInfo) { - if (userInfo.exp > Date.now()) { - this._user = new User(userInfo); - } else { - this.resetAuth(); - } - } - } - resetAuth() { - localStorage.removeItem('userInfo'); localStorage.removeItem('xsrfToken'); this.logout().subscribe(); this._user = null; -- GitLab