From 58f4cf6be6b0690d0de4c1849a233cedfbf30154 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 10 Nov 2023 11:42:46 +0100 Subject: [PATCH] make auth context now if auth is bypassed --- .../src/apps/app/app.element.ts | 15 +++++++-------- .../src/shared/auth/auth.context.ts | 7 +++++-- 2 files changed, 12 insertions(+), 10 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 f7b9bd445d..2112e85c0d 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 @@ -78,7 +78,7 @@ export class UmbAppElement extends UmbLitElement { OpenAPI.BASE = this.serverUrl; const redirectUrl = `${window.location.origin}${this.backofficePath}`; - this.#authContext = new UmbAuthContext(this, this.serverUrl, redirectUrl); + this.#authContext = new UmbAuthContext(this, this.serverUrl, redirectUrl, this.bypassAuth); new UmbAppContext(this, { backofficePath: this.backofficePath, serverUrl: this.serverUrl }); // Try to initialise the auth flow and get the runtime status @@ -207,14 +207,13 @@ export class UmbAppElement extends UmbLitElement { } } - #isAuthorized(): boolean { - if (!this.#authContext) return false; - return this.bypassAuth ? true : this.#authContext.isAuthorized(); - } - #isAuthorizedGuard(): Guard { return () => { - if (this.#isAuthorized()) { + if (!this.#authContext) { + throw new Error('[Fatal] AuthContext requested before it was initialized'); + } + + if (this.#authContext.isAuthorized()) { return true; } @@ -222,7 +221,7 @@ export class UmbAppElement extends UmbLitElement { window.sessionStorage.setItem('umb:auth:redirect', location.href); // Make a request to the auth server to start the auth flow - this.#authContext!.login(); + this.#authContext.login(); // Return false to prevent the route from being rendered return false; 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 135c8afc9e..01d4f8ea7e 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,10 +8,13 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth { #isLoggedIn = new UmbBooleanState(false); readonly isLoggedIn = this.#isLoggedIn.asObservable(); + isBypassed = false; + #authFlow; - constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string) { + constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string, isBypassed: boolean) { super(host); + this.isBypassed = isBypassed; this.#authFlow = new UmbAuthFlow(serverUrl, redirectUrl); this.provideContext(UMB_AUTH_CONTEXT, this); } @@ -29,7 +32,7 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth { } isAuthorized() { - return this.#authFlow.isAuthorized(); + return this.isBypassed ? true : this.#authFlow.isAuthorized(); } setInitialState(): Promise {