From d39f8976c10c2de5233d4f04a8c77be8a7d67990 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 10 Nov 2023 14:27:32 +0100 Subject: [PATCH] move redirect after login responsibility into auth context --- .../src/apps/app/app.element.ts | 3 +-- .../src/shared/auth/auth.context.ts | 27 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/apps/app/app.element.ts b/src/Umbraco.Web.UI.Client/src/apps/app/app.element.ts index 0622398f75..ea2dc6e877 100644 --- a/src/Umbraco.Web.UI.Client/src/apps/app/app.element.ts +++ b/src/Umbraco.Web.UI.Client/src/apps/app/app.element.ts @@ -74,12 +74,11 @@ export class UmbAppElement extends UmbLitElement { async #setup() { if (this.serverUrl === undefined) throw new Error('No serverUrl provided'); - const redirectUrl = `${window.location.origin}${this.backofficePath}`; this.#serverConnection = new UmbServerConnection(this.serverUrl); await this.#serverConnection.connect(); - this.#authContext = new UmbAuthContext(this, this.serverUrl, redirectUrl, this.bypassAuth); + this.#authContext = new UmbAuthContext(this, this.serverUrl, this.backofficePath, this.bypassAuth); new UmbAppContext(this, { backofficePath: this.backofficePath, serverUrl: this.serverUrl }); // Try to initialise the auth flow and get the runtime status 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 87e9e82647..b89dba99de 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 @@ -8,14 +8,17 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth { #isAuthorized = new UmbBooleanState(false); readonly isAuthorized = this.#isAuthorized.asObservable(); - isBypassed = false; + #isBypassed = false; + #backofficePath: string; #authFlow; - constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string, isBypassed: boolean) { + constructor(host: UmbControllerHostElement, serverUrl: string, backofficePath: string, isBypassed: boolean) { super(host); - this.isBypassed = isBypassed; - this.#authFlow = new UmbAuthFlow(serverUrl, redirectUrl); + this.#isBypassed = isBypassed; + this.#backofficePath = backofficePath; + + this.#authFlow = new UmbAuthFlow(serverUrl, this.#getRedirectUrl()); this.provideContext(UMB_AUTH_CONTEXT, this); } @@ -26,8 +29,12 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth { return this.#authFlow.makeAuthorizationRequest(); } + /** + * Checks if the user is authorized. If Authorization is bypassed, the user is always authorized. + * @returns {boolean} True if the user is authorized, otherwise false. + */ getIsAuthorized() { - if (this.isBypassed) { + if (this.#isBypassed) { this.#isAuthorized.next(true); return true; } else { @@ -37,6 +44,10 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth { } } + /** + * Sets the initial state of the auth flow. + * @returns {Promise} + */ setInitialState(): Promise { return this.#authFlow.setInitialState(); } @@ -55,8 +66,14 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth { /** * Signs the user out by removing any tokens from the browser. + * @return {*} {Promise} + * @memberof UmbAuthContext */ signOut(): Promise { return this.#authFlow.signOut(); } + + #getRedirectUrl() { + return `${window.location.origin}${this.#backofficePath}`; + } }