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
Pipeline #1769 passed with stages
in 4 minutes and 49 seconds
......@@ -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();
});
});
};
}
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment