Skip to content
Snippets Groups Projects
Commit 1bcc40ac authored by FORESTIER Fabien's avatar FORESTIER Fabien
Browse files

Do not use localStorage to store userInfo but call auth service on app init

parent 673d2df5
No related branches found
No related tags found
1 merge request!49Version 2.3.1
Pipeline #1769 passed
...@@ -16,8 +16,13 @@ import { AppConfigService } from './core/services'; ...@@ -16,8 +16,13 @@ import { AppConfigService } from './core/services';
export function initUserService(authService: UserService) { export function initUserService(authService: UserService) {
return (): Promise<any> => { return (): Promise<any> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
authService.initializeService(); authService.initializeService().subscribe(
resolve(); () => {
resolve();
},
() => {
resolve();
});
}); });
}; };
} }
......
...@@ -3,7 +3,6 @@ import { HttpClient } from '@angular/common/http'; ...@@ -3,7 +3,6 @@ import { HttpClient } from '@angular/common/http';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { IUserInfo, PasswordUpdateForm, User, ILoginResponse, LegacyAccount, IPasswordForgottenForm } from '../models'; import { IUserInfo, PasswordUpdateForm, User, ILoginResponse, LegacyAccount, IPasswordForgottenForm } from '../models';
import { map, mergeMap, tap } from 'rxjs/operators'; import { map, mergeMap, tap } from 'rxjs/operators';
import * as JwtDecode from 'jwt-decode';
import * as NodeRSA from 'node-rsa'; import * as NodeRSA from 'node-rsa';
import { APP_CONFIG } from '../../core/services/app-config.service'; import { APP_CONFIG } from '../../core/services/app-config.service';
...@@ -20,16 +19,19 @@ export class UserService { ...@@ -20,16 +19,19 @@ export class UserService {
} }
initializeService() { 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... // Function and helpers allowing the management of the user session (jwt), info...
setSession(authResult): boolean { setSession(authResult): boolean {
let success = false; let success = false;
if (authResult && authResult.userInfo && authResult.xsrfToken) { if (authResult && authResult.userInfo && authResult.xsrfToken) {
localStorage.setItem('userInfo', JSON.stringify(authResult.userInfo));
localStorage.setItem('xsrfToken', authResult.xsrfToken); localStorage.setItem('xsrfToken', authResult.xsrfToken);
this.setUserInfo(); this._user = new User(authResult.userInfo);
success = true; success = true;
this._userStatusChangedSubject.next(true); this._userStatusChangedSubject.next(true);
} else { } else {
...@@ -39,19 +41,7 @@ export class UserService { ...@@ -39,19 +41,7 @@ export class UserService {
return success; 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() { resetAuth() {
localStorage.removeItem('userInfo');
localStorage.removeItem('xsrfToken'); localStorage.removeItem('xsrfToken');
this.logout().subscribe(); this.logout().subscribe();
this._user = null; this._user = null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment