diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts index 54786758a9..791d931236 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/current-user.context.ts @@ -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(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 { + const currentUser = await firstValueFrom(this.currentUser); + return currentUser?.id === userId; + } + + async fetchCurrentUser(): Promise { + const { data } = await tryExecuteAndNotify(this._host, UserResource.getUserCurrent()); + + this.#currentUser.next(data); + + return data; } } diff --git a/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.ts b/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.ts index 7c45ea7793..29f69c587a 100644 --- a/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.ts +++ b/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.ts @@ -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(undefined); - readonly currentUser = this.#currentUser.asObservable(); - #isLoggedIn = new UmbBooleanState(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 { - 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 { 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 { - const currentUser = await firstValueFrom(this.currentUser); - return currentUser?.id === userId; - } }