From d0a659d539256ff473a32b2fd887672cd050a011 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:23:58 +0100 Subject: [PATCH] split signOut and clear token into two methods --- src/Umbraco.Web.UI.Client/src/apps/app/app.element.ts | 6 +++--- .../src/shared/auth/auth-flow.ts | 11 +++++++++++ .../src/shared/auth/auth.context.interface.ts | 5 +++++ .../src/shared/auth/auth.context.ts | 8 ++++++++ 4 files changed, 27 insertions(+), 3 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 745e057c76..c1f4e35d4f 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 @@ -77,8 +77,8 @@ export class UmbAppElement extends UmbLitElement { async #setup() { if (this.serverUrl === undefined) throw new Error('No serverUrl provided'); - /* All requests to the server requires the base URL to be set. - We make sure it happens before we get the server status. + /* All requests to the server requires the base URL to be set. + We make sure it happens before we get the server status. TODO: find the right place to set this */ OpenAPI.BASE = this.serverUrl; @@ -93,7 +93,7 @@ export class UmbAppElement extends UmbLitElement { // If the runtime level is "install" we should clear any cached tokens // else we should try and set the auth status if (this.#serverConnection.getStatus() === RuntimeLevelModel.INSTALL) { - await this.#authContext.signOut(); + await this.#authContext.clearTokenStorage(); } else { await this.#setAuthStatus(); } diff --git a/src/Umbraco.Web.UI.Client/src/shared/auth/auth-flow.ts b/src/Umbraco.Web.UI.Client/src/shared/auth/auth-flow.ts index 8f9ec99743..0cf116b1cb 100644 --- a/src/Umbraco.Web.UI.Client/src/shared/auth/auth-flow.ts +++ b/src/Umbraco.Web.UI.Client/src/shared/auth/auth-flow.ts @@ -223,6 +223,17 @@ export class UmbAuthFlow { return !!this.#accessTokenResponse && this.#accessTokenResponse.isValid(); } + /** + * Forget all cached token state + */ + async clearTokenStorage() { + await this.#storageBackend.removeItem(TOKEN_RESPONSE_NAME); + + // clear the internal state + this.#accessTokenResponse = undefined; + this.#refreshToken = undefined; + } + /** * This method will sign the user out of the application. */ diff --git a/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.interface.ts b/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.interface.ts index 33b70eceac..3ec2beb473 100644 --- a/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.interface.ts +++ b/src/Umbraco.Web.UI.Client/src/shared/auth/auth.context.interface.ts @@ -34,6 +34,11 @@ export interface IUmbAuthContext { */ getLatestToken(): Promise; + /** + * Clears the token storage. + */ + clearTokenStorage(): Promise; + /** * Signs the user out by removing any tokens from the browser. */ 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 d9e45daca3..af3768cad2 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 @@ -65,6 +65,14 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuthContext return this.#authFlow.performWithFreshTokens(); } + /** + * Clears the token storage. + * @memberof UmbAuthContext + */ + clearTokenStorage() { + return this.#authFlow.clearTokenStorage(); + } + /** * Signs the user out by removing any tokens from the browser. * @return {*} {Promise}