|
|
@@ -1,31 +1,50 @@
|
|
|
import { Injectable } from "@angular/core";
|
|
|
import { AuthService, User } from "@auth0/auth0-angular";
|
|
|
+import { BehaviorSubject, firstValueFrom, Subject } from "rxjs";
|
|
|
+import { UserProfile } from "./user-profile";
|
|
|
+import { UserState } from "./user-state";
|
|
|
import { UsersApiService } from "./users-api.service";
|
|
|
import { UserValidationResult } from "./validate-user-result";
|
|
|
|
|
|
@Injectable()
|
|
|
export class PermissionService {
|
|
|
|
|
|
- private _user?: User;
|
|
|
+ private readonly _validationResult: Promise<UserValidationResult | null>;
|
|
|
|
|
|
- public get user(): User | undefined {
|
|
|
- return this._user;
|
|
|
+ public get user(): Promise<User | null> {
|
|
|
+ return this._validationResult.then(r => r?.user ?? null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public get userProfile(): Promise<UserProfile | null> {
|
|
|
+ return this._validationResult.then(r => r?.userProfile ?? null);
|
|
|
}
|
|
|
|
|
|
public constructor(
|
|
|
public readonly authService: AuthService,
|
|
|
private readonly usersApi: UsersApiService
|
|
|
) {
|
|
|
- this.authService.user$.subscribe(user => {
|
|
|
- if (user) {
|
|
|
- this._user = user;
|
|
|
- } else {
|
|
|
- this._user = undefined;
|
|
|
- }
|
|
|
+ this._validationResult = new Promise<UserValidationResult | null>(resolve => {
|
|
|
+ firstValueFrom(this.authService.user$)
|
|
|
+ .then(user => {
|
|
|
+ if (user) {
|
|
|
+ return this.usersApi.validatePermissions(user);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then(result => {
|
|
|
+ resolve(result);
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public isRegistered(): Promise<UserValidationResult> {
|
|
|
- return this.usersApi.validatePermissions();
|
|
|
+ public isRegistered(): Promise<UserState> {
|
|
|
+ return this._validationResult.then(r => {
|
|
|
+ return r == null
|
|
|
+ ? UserState.Unauthenticated
|
|
|
+ : (r.claims.length > 0
|
|
|
+ ? UserState.Registered
|
|
|
+ : UserState.Authenticated);
|
|
|
+ });
|
|
|
}
|
|
|
}
|