From e6266ecedc221d9b686284ada94c0ee9b1470f37 Mon Sep 17 00:00:00 2001 From: nymkappa <1612910616@pm.me> Date: Fri, 9 Feb 2024 15:49:28 +0100 Subject: [PATCH 1/3] [auth] properly refresh user when `auth` object changes --- .../src/app/services/services-api.service.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/services/services-api.service.ts b/frontend/src/app/services/services-api.service.ts index 758829852..17ffe33f1 100644 --- a/frontend/src/app/services/services-api.service.ts +++ b/frontend/src/app/services/services-api.service.ts @@ -1,3 +1,4 @@ +import { Router, NavigationStart } from '@angular/router'; import { Injectable } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; import { StateService } from './state.service'; @@ -30,16 +31,20 @@ const SERVICES_API_PREFIX = `/api/v1/services`; providedIn: 'root' }) export class ServicesApiServices { - private apiBaseUrl: string; // base URL is protocol, hostname, and port - private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet + apiBaseUrl: string; // base URL is protocol, hostname, and port + apiBasePath: string; // network path is /testnet, etc. or '' for mainnet userSubject$ = new ReplaySubject(1); + currentAuth = null; constructor( private httpClient: HttpClient, private stateService: StateService, - private storageService: StorageService + private storageService: StorageService, + private router: Router, ) { + this.currentAuth = localStorage.getItem('auth'); + this.apiBaseUrl = ''; // use relative URL by default if (!stateService.isBrowser) { // except when inside AU SSR process this.apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT; @@ -59,6 +64,15 @@ export class ServicesApiServices { } this.getUserInfo$().subscribe(); + console.log('refresh user'); + this.router.events.subscribe((event) => { + if (event instanceof NavigationStart) { + if (this.currentAuth !== localStorage.getItem('auth')) { + console.log('refresh user'); + this.getUserInfo$().subscribe(); + } + } + }); } /** From bb43599493a8ec5ffb585f8d6145a8dcecea5fba Mon Sep 17 00:00:00 2001 From: nymkappa <1612910616@pm.me> Date: Fri, 9 Feb 2024 15:53:20 +0100 Subject: [PATCH 2/3] [auth] remove debug log --- frontend/src/app/services/services-api.service.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/frontend/src/app/services/services-api.service.ts b/frontend/src/app/services/services-api.service.ts index 17ffe33f1..3bb459b92 100644 --- a/frontend/src/app/services/services-api.service.ts +++ b/frontend/src/app/services/services-api.service.ts @@ -64,11 +64,9 @@ export class ServicesApiServices { } this.getUserInfo$().subscribe(); - console.log('refresh user'); this.router.events.subscribe((event) => { if (event instanceof NavigationStart) { if (this.currentAuth !== localStorage.getItem('auth')) { - console.log('refresh user'); this.getUserInfo$().subscribe(); } } From a2b01587b15b0d84fa9e3799c80d712d95c8d172 Mon Sep 17 00:00:00 2001 From: nymkappa <1612910616@pm.me> Date: Sat, 10 Feb 2024 10:31:56 +0100 Subject: [PATCH 3/3] [auth] small refactor --- frontend/src/app/services/services-api.service.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/services/services-api.service.ts b/frontend/src/app/services/services-api.service.ts index 3bb459b92..f11b3460c 100644 --- a/frontend/src/app/services/services-api.service.ts +++ b/frontend/src/app/services/services-api.service.ts @@ -4,7 +4,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { StateService } from './state.service'; import { StorageService } from './storage.service'; import { MenuGroup } from '../interfaces/services.interface'; -import { Observable, of, ReplaySubject, tap, catchError, share } from 'rxjs'; +import { Observable, of, ReplaySubject, tap, catchError, share, filter, switchMap } from 'rxjs'; import { IBackendInfo } from '../interfaces/websocket.interface'; import { Acceleration, AccelerationHistoryParams } from '../interfaces/node-api.interface'; @@ -64,13 +64,10 @@ export class ServicesApiServices { } this.getUserInfo$().subscribe(); - this.router.events.subscribe((event) => { - if (event instanceof NavigationStart) { - if (this.currentAuth !== localStorage.getItem('auth')) { - this.getUserInfo$().subscribe(); - } - } - }); + this.router.events.pipe( + filter((event) => event instanceof NavigationStart && this.currentAuth !== localStorage.getItem('auth')), + switchMap(() => this.getUserInfo$()), + ).subscribe(); } /**