From 8798d4cd8606bd0416a6c91b14cd1fdb7ce6edf2 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:45:50 +0100 Subject: [PATCH] make OpenAPI and the server url public accessible through the UmbAuthContext --- .../src/shared/auth/auth.context.ts | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) 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 7fa2415579..6e86150fbc 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 @@ -3,19 +3,22 @@ import { UMB_AUTH_CONTEXT } from './auth.context.token.js'; import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api'; import { UmbBaseController } from '@umbraco-cms/backoffice/class-api'; import { UmbBooleanState } from '@umbraco-cms/backoffice/observable-api'; +import { OpenAPI } from '@umbraco-cms/backoffice/backend-api'; export class UmbAuthContext extends UmbBaseController { #isAuthorized = new UmbBooleanState(false); readonly isAuthorized = this.#isAuthorized.asObservable(); #isBypassed = false; - #backofficePath: string; - + #serverUrl; + #backofficePath; #authFlow; + #openApi = OpenAPI; constructor(host: UmbControllerHostElement, serverUrl: string, backofficePath: string, isBypassed: boolean) { super(host); this.#isBypassed = isBypassed; + this.#serverUrl = serverUrl; this.#backofficePath = backofficePath; this.#authFlow = new UmbAuthFlow(serverUrl, this.#getRedirectUrl()); @@ -65,7 +68,7 @@ export class UmbAuthContext extends UmbBaseController { * * NB! The user may experience being redirected to the login screen if the token is expired. * - * @example + * @example Using the latest token * ```js * const token = await authContext.getLatestToken(); * const result = await fetch('https://my-api.com', { headers: { Authorization: `Bearer ${token}` } }); @@ -94,6 +97,42 @@ export class UmbAuthContext extends UmbBaseController { return this.#authFlow.signOut(); } + /** + * Get the server url to the Management API. + * @memberof UmbAuthContext + * @example Using the server url + * ```js + * const serverUrl = authContext.getServerUrl(); + * OpenAPI.BASE = serverUrl; + * ``` + * @example + * ```js + * const serverUrl = authContext.getServerUrl(); + * const token = await authContext.getLatestToken(); + * const result = await fetch(`${serverUrl}/umbraco/management/api/v1/my-resource`, { headers: { Authorization: `Bearer ${token}` } }); + * ``` + * @returns The server url to the Management API + */ + getServerUrl() { + return this.#serverUrl; + } + + /** + * Get the default OpenAPI configuration, which is set up to communicate with the Management API. + * @remark This is useful if you want to communicate with your own resources generated by the [openapi-typescript-codegen](https://github.com/ferdikoomen/openapi-typescript-codegen) library. + * @memberof UmbAuthContext + * + * @example Using the default OpenAPI configuration + * ```js + * const defaultOpenApi = authContext.getOpenApiConfiguration(); + * OpenAPI = { ...OpenAPI, ...openApi }; + * ``` + * @returns The default OpenAPI configuration + */ + getOpenApiConfiguration() { + return this.#openApi; + } + #getRedirectUrl() { return `${window.location.origin}${this.#backofficePath}`; }