mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-08 00:07:40 +00:00
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 <elio@zitadel.com>
This commit is contained in:
parent
f2d1cd2045
commit
412cd144ef
@ -1,5 +1,5 @@
|
|||||||
<div class="main-container">
|
<div class="main-container">
|
||||||
<ng-container *ngIf="(authService.user | async) || {} as user">
|
<ng-container *ngIf="(authService.userSubject | async) || {} as user">
|
||||||
<cnsl-header
|
<cnsl-header
|
||||||
*ngIf="user && user !== {}"
|
*ngIf="user && user !== {}"
|
||||||
[org]="org"
|
[org]="org"
|
||||||
|
@ -225,9 +225,11 @@ export class AppComponent implements OnDestroy {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.isDarkTheme = this.themeService.isDarkTheme;
|
this.isDarkTheme = this.themeService.isDarkTheme;
|
||||||
this.isDarkTheme.subscribe((dark) => this.onSetTheme(dark ? 'dark-theme' : 'light-theme'));
|
this.isDarkTheme
|
||||||
|
.pipe(takeUntil(this.destroy$))
|
||||||
|
.subscribe((dark) => this.onSetTheme(dark ? 'dark-theme' : 'light-theme'));
|
||||||
|
|
||||||
this.translate.onLangChange.subscribe((language: LangChangeEvent) => {
|
this.translate.onLangChange.pipe(takeUntil(this.destroy$)).subscribe((language: LangChangeEvent) => {
|
||||||
this.document.documentElement.lang = language.lang;
|
this.document.documentElement.lang = language.lang;
|
||||||
this.language = language.lang;
|
this.language = language.lang;
|
||||||
});
|
});
|
||||||
@ -271,7 +273,7 @@ export class AppComponent implements OnDestroy {
|
|||||||
this.translate.addLangs(supportedLanguages);
|
this.translate.addLangs(supportedLanguages);
|
||||||
this.translate.setDefaultLang(fallbackLanguage);
|
this.translate.setDefaultLang(fallbackLanguage);
|
||||||
|
|
||||||
this.authService.user.subscribe((userprofile) => {
|
this.authService.userSubject.pipe(takeUntil(this.destroy$)).subscribe((userprofile) => {
|
||||||
if (userprofile) {
|
if (userprofile) {
|
||||||
const cropped = navigator.language.split('-')[0] ?? fallbackLanguage;
|
const cropped = navigator.language.split('-')[0] ?? fallbackLanguage;
|
||||||
const fallbackLang = cropped.match(supportedLanguagesRegexp) ? cropped : fallbackLanguage;
|
const fallbackLang = cropped.match(supportedLanguagesRegexp) ? cropped : fallbackLanguage;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
|
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
|
||||||
import { Observable } from 'rxjs';
|
import { map, Observable, take } from 'rxjs';
|
||||||
import { map, tap } from 'rxjs/operators';
|
|
||||||
|
|
||||||
import { GrpcAuthService } from '../services/grpc-auth.service';
|
import { GrpcAuthService } from '../services/grpc-auth.service';
|
||||||
|
|
||||||
@ -19,11 +18,13 @@ export class UserGuard {
|
|||||||
state: RouterStateSnapshot,
|
state: RouterStateSnapshot,
|
||||||
): Observable<boolean> | Promise<boolean> | boolean {
|
): Observable<boolean> | Promise<boolean> | boolean {
|
||||||
return this.authService.user.pipe(
|
return this.authService.user.pipe(
|
||||||
map((user) => user?.id !== route.params['id']),
|
take(1),
|
||||||
tap((isNotMe) => {
|
map((user) => {
|
||||||
if (!isNotMe) {
|
const isMe = user?.id === route.params['id'];
|
||||||
|
if (isMe) {
|
||||||
this.router.navigate(['/users', 'me']);
|
this.router.navigate(['/users', 'me']);
|
||||||
}
|
}
|
||||||
|
return !isMe;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
|
|||||||
import { BehaviorSubject, from, lastValueFrom, Observable } from 'rxjs';
|
import { BehaviorSubject, from, lastValueFrom, Observable } from 'rxjs';
|
||||||
|
|
||||||
import { StatehandlerService } from './statehandler/statehandler.service';
|
import { StatehandlerService } from './statehandler/statehandler.service';
|
||||||
|
import { ToastService } from './toast.service';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
@ -15,6 +16,7 @@ export class AuthenticationService {
|
|||||||
constructor(
|
constructor(
|
||||||
private oauthService: OAuthService,
|
private oauthService: OAuthService,
|
||||||
private statehandler: StatehandlerService,
|
private statehandler: StatehandlerService,
|
||||||
|
private toast: ToastService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public initConfig(data: AuthConfig): void {
|
public initConfig(data: AuthConfig): void {
|
||||||
@ -39,7 +41,10 @@ export class AuthenticationService {
|
|||||||
}
|
}
|
||||||
this.oauthService.configure(this.authConfig);
|
this.oauthService.configure(this.authConfig);
|
||||||
this.oauthService.strictDiscoveryDocumentValidation = false;
|
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();
|
this._authenticated = this.oauthService.hasValidAccessToken();
|
||||||
if (!this.oauthService.hasValidIdToken() || !this.authenticated || partialConfig || force) {
|
if (!this.oauthService.hasValidIdToken() || !this.authenticated || partialConfig || force) {
|
||||||
const newState = await lastValueFrom(this.statehandler.createState());
|
const newState = await lastValueFrom(this.statehandler.createState());
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { SortDirection } from '@angular/material/sort';
|
import { SortDirection } from '@angular/material/sort';
|
||||||
import { OAuthService } from 'angular-oauth2-oidc';
|
import { OAuthService } from 'angular-oauth2-oidc';
|
||||||
import { BehaviorSubject, from, merge, Observable, of, Subject } from 'rxjs';
|
import { BehaviorSubject, forkJoin, from, Observable, of, Subject } from 'rxjs';
|
||||||
import {
|
import { catchError, distinctUntilChanged, filter, finalize, map, switchMap, timeout, withLatestFrom } from 'rxjs/operators';
|
||||||
catchError,
|
|
||||||
distinctUntilChanged,
|
|
||||||
filter,
|
|
||||||
finalize,
|
|
||||||
map,
|
|
||||||
mergeMap,
|
|
||||||
switchMap,
|
|
||||||
take,
|
|
||||||
timeout,
|
|
||||||
withLatestFrom,
|
|
||||||
} from 'rxjs/operators';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AddMyAuthFactorOTPEmailRequest,
|
AddMyAuthFactorOTPEmailRequest,
|
||||||
@ -184,25 +173,21 @@ export class GrpcAuthService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
this.user = merge(
|
this.user = forkJoin([
|
||||||
of(this.oauthService.getAccessToken()).pipe(filter((token) => (token ? true : false))),
|
of(this.oauthService.getAccessToken()),
|
||||||
this.oauthService.events.pipe(
|
this.oauthService.events.pipe(
|
||||||
filter((e) => e.type === 'token_received'),
|
filter((e) => e.type === 'token_received'),
|
||||||
timeout(this.oauthService.waitForTokenInMsec || 0),
|
timeout(this.oauthService.waitForTokenInMsec || 0),
|
||||||
catchError((_) => of(null)), // timeout is not an error
|
catchError((_) => of(null)), // timeout is not an error
|
||||||
map((_) => this.oauthService.getAccessToken()),
|
map((_) => this.oauthService.getAccessToken()),
|
||||||
),
|
),
|
||||||
).pipe(
|
]).pipe(
|
||||||
take(1),
|
filter((token) => (token ? true : false)),
|
||||||
mergeMap(() => {
|
distinctUntilChanged(),
|
||||||
|
switchMap(() => {
|
||||||
return from(
|
return from(
|
||||||
this.getMyUser().then((resp) => {
|
this.getMyUser().then((resp) => {
|
||||||
const user = resp.user;
|
return resp.user;
|
||||||
if (user) {
|
|
||||||
return user;
|
|
||||||
} else {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user