From cb7f38b46bfcd6d2a8b9e38fdcfb4707e8a64745 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 14 Dec 2021 19:28:50 -0500 Subject: [PATCH] Improve AppState --- src/app/modules/accounts/accounts.ts | 4 +-- .../accounts/services/account.service.ts | 33 +------------------ .../modules/accounts/services/jwt.service.ts | 8 +---- src/app/modules/shared/app-state.ts | 23 +++++++++---- src/app/modules/shared/service/api.service.ts | 6 ++-- 5 files changed, 24 insertions(+), 50 deletions(-) diff --git a/src/app/modules/accounts/accounts.ts b/src/app/modules/accounts/accounts.ts index 2c42c84..0fcc07e 100644 --- a/src/app/modules/accounts/accounts.ts +++ b/src/app/modules/accounts/accounts.ts @@ -48,9 +48,9 @@ export class Login extends ErrorHandlingComponent { } submit() { - this.subscribeAndNavigate( + this.subscribe( this.accountService.login(this.userIdControl.value, this.passwordControl.value), - '/color' + () => {} ) // Does not use SubscribingComponent shortcut because backend doesn't return expected error type // this.accountService.login(this.userIdControl.value, this.passwordControl.value) diff --git a/src/app/modules/accounts/services/account.service.ts b/src/app/modules/accounts/services/account.service.ts index 7295c3d..9cee84e 100644 --- a/src/app/modules/accounts/services/account.service.ts +++ b/src/app/modules/accounts/services/account.service.ts @@ -7,7 +7,6 @@ import {environment} from '../../../../environments/environment' import {ApiService} from '../../shared/service/api.service' import {Permission} from '../../shared/model/user' import {ErrorService} from '../../shared/service/error.service' -import {globalLoadingWheel} from '../../shared/components/loading-wheel/loading-wheel.component' import {AlertService} from '../../shared/service/alert.service' import {JwtService} from "./jwt.service"; @@ -92,37 +91,7 @@ export class AccountService implements OnDestroy { const authorization = response.headers.get("Authorization") const jwt = this.jwtService.parseJwt(authorization) - this.appState.authenticatedUser = jwt.user - this.appState.authenticationExpiration = jwt.exp - } - - loginOld(id: number, password: string, success: () => void) { - const loginForm = {id, password} - globalLoadingWheel.show() - this.http.post(`${environment.apiUrl}/login`, loginForm, { - withCredentials: true, - observe: 'response' as 'body' - }) - .pipe( - take(1), - takeUntil(this.destroy$) - ) - .subscribe({ - next: (response: HttpResponse) => { - this.appState.authenticationExpiration = parseInt(response.headers.get('X-Authentication-Expiration')) - this.appState.isAuthenticated = true - this.setLoggedInUserFromApi() - success() - }, - error: err => { - globalLoadingWheel.hide() - if (err.status === 401 || err.status === 403) { - this.alertService.pushError('Les identifiants entrés sont invalides') - } else { - this.errorService.handleError(err) - } - } - }) + this.appState.authenticateUser(jwt) } logout(success: () => void) { diff --git a/src/app/modules/accounts/services/jwt.service.ts b/src/app/modules/accounts/services/jwt.service.ts index d2a88f5..6121290 100644 --- a/src/app/modules/accounts/services/jwt.service.ts +++ b/src/app/modules/accounts/services/jwt.service.ts @@ -1,7 +1,7 @@ import {Injectable} from "@angular/core"; -import {User} from "../../shared/model/user"; import jwtDecode from "jwt-decode"; import {parseJson} from "@angular/cli/utilities/json-file"; +import {CreJwt} from "../../shared/app-state"; @Injectable({ providedIn: 'root' @@ -17,9 +17,3 @@ export class JwtService { } } } - -interface CreJwt { - readonly sub: string - readonly exp: number, - readonly user: User -} diff --git a/src/app/modules/shared/app-state.ts b/src/app/modules/shared/app-state.ts index 1504d28..3ed0e23 100644 --- a/src/app/modules/shared/app-state.ts +++ b/src/app/modules/shared/app-state.ts @@ -20,6 +20,12 @@ export class AppState { ) { } + authenticateUser(jwt: CreJwt) { + this.authenticatedUser = jwt.user + this.authenticationExpiration = jwt.exp + this.isAuthenticated = true + } + resetAuthenticatedUser() { this.isAuthenticated = false this.authenticationExpiration = -1 @@ -37,7 +43,7 @@ export class AppState { return sessionStorage.getItem(this.KEY_AUTHENTICATED) === 'true' } - set isAuthenticated(value: boolean) { + private set isAuthenticated(value: boolean) { sessionStorage.setItem(this.KEY_AUTHENTICATED, value.toString()) this.authenticatedUser$.next({ authenticated: value, @@ -49,7 +55,8 @@ export class AppState { return parseInt(sessionStorage.getItem(this.KEY_AUTHENTICATION_EXPIRATION)) } - set authenticationExpiration(value: number) { + private set authenticationExpiration(value: number) { + console.error(value) sessionStorage.setItem(this.KEY_AUTHENTICATION_EXPIRATION, value.toString()) } @@ -58,12 +65,10 @@ export class AppState { return userString ? JSON.parse(userString) : null } - set authenticatedUser(value: User) { + private set authenticatedUser(value: User) { if (value === null) { - console.log(1) - sessionStorage.removeItem(this.KEY_LOGGED_IN_USER) + // sessionStorage.removeItem(this.KEY_LOGGED_IN_USER) } else { - console.log(2) sessionStorage.setItem(this.KEY_LOGGED_IN_USER, JSON.stringify(value)) } this.authenticatedUser$.next({ @@ -76,3 +81,9 @@ export class AppState { this.titleService.setTitle(`CRE: ${value}`) } } + +export interface CreJwt { + readonly sub: string + readonly exp: number, + readonly user: User +} diff --git a/src/app/modules/shared/service/api.service.ts b/src/app/modules/shared/service/api.service.ts index 7c39b28..1e4c269 100644 --- a/src/app/modules/shared/service/api.service.ts +++ b/src/app/modules/shared/service/api.service.ts @@ -4,10 +4,9 @@ import {Observable, Subject} from 'rxjs' import {environment} from '../../../../environments/environment' import {AppState} from '../app-state' import {Router} from '@angular/router' -import {map, share, takeUntil, tap} from 'rxjs/operators' +import {map, share, takeUntil} from 'rxjs/operators' import {valueOr} from '../utils/utils' import {ErrorService} from './error.service' -import {globalLoadingWheel} from '../components/loading-wheel/loading-wheel.component' @Injectable({ providedIn: 'root' @@ -78,7 +77,7 @@ export class ApiService implements OnDestroy { console.error('httpOptions need to be specified to use credentials in HTTP methods.') } } else { - this.appState.resetAuthenticatedUser() + // this.appState.resetAuthenticatedUser() this.navigateToLogin() } } @@ -91,6 +90,7 @@ export class ApiService implements OnDestroy { } private checkAuthenticated(): boolean { + console.log(Date.now() / 1000, this.appState.authenticationExpiration) return (this.appState.isAuthenticated && Date.now() <= this.appState.authenticationExpiration) || (this.appState.authenticatedUser && this.appState.authenticatedUser.group != null) }