fix(console): remove redundant user api requests, show discovery document loading errors (#6683)

* optimize user observable

* fix observable guard

* lint

* lint

---------

Co-authored-by: Elio Bischof <[email protected]>
This commit is contained in:
Max Peintner
2023-10-11 07:02:20 +00:00
committed by GitHub
co-authored by Elio Bischof
parent f2d1cd2045
commit 412cd144ef
5 changed files with 27 additions and 34 deletions
@@ -3,6 +3,7 @@ import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
import { BehaviorSubject, from, lastValueFrom, Observable } from 'rxjs';
import { StatehandlerService } from './statehandler/statehandler.service';
import { ToastService } from './toast.service';
@Injectable({
providedIn: 'root',
@@ -15,6 +16,7 @@ export class AuthenticationService {
constructor(
private oauthService: OAuthService,
private statehandler: StatehandlerService,
private toast: ToastService,
) {}
public initConfig(data: AuthConfig): void {
@@ -39,7 +41,10 @@ export class AuthenticationService {
}
this.oauthService.configure(this.authConfig);
this.oauthService.strictDiscoveryDocumentValidation = false;
await this.oauthService.loadDiscoveryDocumentAndTryLogin();
await this.oauthService.loadDiscoveryDocumentAndTryLogin().catch((error) => {
this.toast.showError(error, false, false);
});
this._authenticated = this.oauthService.hasValidAccessToken();
if (!this.oauthService.hasValidIdToken() || !this.authenticated || partialConfig || force) {
const newState = await lastValueFrom(this.statehandler.createState());
+9 -24
View File
@@ -1,19 +1,8 @@
import { Injectable } from '@angular/core';
import { SortDirection } from '@angular/material/sort';
import { OAuthService } from 'angular-oauth2-oidc';
import { BehaviorSubject, from, merge, Observable, of, Subject } from 'rxjs';
import {
catchError,
distinctUntilChanged,
filter,
finalize,
map,
mergeMap,
switchMap,
take,
timeout,
withLatestFrom,
} from 'rxjs/operators';
import { BehaviorSubject, forkJoin, from, Observable, of, Subject } from 'rxjs';
import { catchError, distinctUntilChanged, filter, finalize, map, switchMap, timeout, withLatestFrom } from 'rxjs/operators';
import {
AddMyAuthFactorOTPEmailRequest,
@@ -184,25 +173,21 @@ export class GrpcAuthService {
},
});
this.user = merge(
of(this.oauthService.getAccessToken()).pipe(filter((token) => (token ? true : false))),
this.user = forkJoin([
of(this.oauthService.getAccessToken()),
this.oauthService.events.pipe(
filter((e) => e.type === 'token_received'),
timeout(this.oauthService.waitForTokenInMsec || 0),
catchError((_) => of(null)), // timeout is not an error
map((_) => this.oauthService.getAccessToken()),
),
).pipe(
take(1),
mergeMap(() => {
]).pipe(
filter((token) => (token ? true : false)),
distinctUntilChanged(),
switchMap(() => {
return from(
this.getMyUser().then((resp) => {
const user = resp.user;
if (user) {
return user;
} else {
return undefined;
}
return resp.user;
}),
);
}),