move current user methods from auth context into current user context
This commit is contained in:
@@ -1,10 +1,47 @@
|
||||
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
|
||||
import { UmbBaseController, UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
import { firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
|
||||
import { UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import { UmbLoggedInUser } from '@umbraco-cms/backoffice/auth';
|
||||
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
|
||||
|
||||
export class UmbCurrentUserContext extends UmbBaseController {
|
||||
#currentUser = new UmbObjectState<UmbLoggedInUser | undefined>(undefined);
|
||||
readonly currentUser = this.#currentUser.asObservable();
|
||||
|
||||
readonly languageIsoCode = this.#currentUser.asObservablePart((user) => user?.languageIsoCode ?? 'en-us');
|
||||
|
||||
constructor(host: UmbControllerHost) {
|
||||
super(host);
|
||||
this.provideContext(UMB_CURRENT_USER_CONTEXT, this);
|
||||
|
||||
/*
|
||||
this.observe(this.isLoggedIn, (isLoggedIn) => {
|
||||
if (isLoggedIn) {
|
||||
this.fetchCurrentUser();
|
||||
}
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user is the current user.
|
||||
*
|
||||
* @param userId The user id to check
|
||||
* @returns True if the user is the current user, otherwise false
|
||||
*/
|
||||
async isUserCurrentUser(userId: string): Promise<boolean> {
|
||||
const currentUser = await firstValueFrom(this.currentUser);
|
||||
return currentUser?.id === userId;
|
||||
}
|
||||
|
||||
async fetchCurrentUser(): Promise<UmbLoggedInUser | undefined> {
|
||||
const { data } = await tryExecuteAndNotify(this._host, UserResource.getUserCurrent());
|
||||
|
||||
this.#currentUser.next(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,17 @@
|
||||
import { IUmbAuth } from './auth.interface.js';
|
||||
import { UmbAuthFlow } from './auth-flow.js';
|
||||
import { UmbLoggedInUser } from './types.js';
|
||||
import { UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import { UmbBaseController, UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { UmbBooleanState, UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
import { firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
|
||||
import { UmbBooleanState } from '@umbraco-cms/backoffice/observable-api';
|
||||
|
||||
export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
|
||||
|
||||
#currentUser = new UmbObjectState<UmbLoggedInUser | undefined>(undefined);
|
||||
readonly currentUser = this.#currentUser.asObservable();
|
||||
|
||||
#isLoggedIn = new UmbBooleanState<boolean>(false);
|
||||
readonly isLoggedIn = this.#isLoggedIn.asObservable();
|
||||
readonly languageIsoCode = this.#currentUser.asObservablePart((user) => user?.languageIsoCode ?? 'en-us');
|
||||
|
||||
#authFlow;
|
||||
|
||||
constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string) {
|
||||
super(host)
|
||||
super(host);
|
||||
this.#authFlow = new UmbAuthFlow(serverUrl, redirectUrl);
|
||||
|
||||
this.observe(this.isLoggedIn, (isLoggedIn) => {
|
||||
if (isLoggedIn) {
|
||||
this.fetchCurrentUser();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,14 +34,6 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
|
||||
return this.#authFlow.setInitialState();
|
||||
}
|
||||
|
||||
async fetchCurrentUser(): Promise<UmbLoggedInUser | undefined> {
|
||||
const { data } = await tryExecuteAndNotify(this._host, UserResource.getUserCurrent());
|
||||
|
||||
this.#currentUser.next(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the latest token from the Management API.
|
||||
* If the token is expired, it will be refreshed.
|
||||
@@ -75,15 +52,4 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
|
||||
signOut(): Promise<void> {
|
||||
return this.#authFlow.signOut();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user is the current user.
|
||||
*
|
||||
* @param userId The user id to check
|
||||
* @returns True if the user is the current user, otherwise false
|
||||
*/
|
||||
async isUserCurrentUser(userId: string): Promise<boolean> {
|
||||
const currentUser = await firstValueFrom(this.currentUser);
|
||||
return currentUser?.id === userId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user