From 18387fb12451560134f609f1131286b7d4fe1359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Eikeland?= Date: Thu, 14 Nov 2024 10:50:05 +0100 Subject: [PATCH] feat(legacy-interopt): protected method for aquireTokenAsync --- ...low-dodos-tease.md => sweet-keys-prove.md} | 18 +++++++++- .../src/LegacyAuthContainer.ts | 35 ++++++------------- 2 files changed, 28 insertions(+), 25 deletions(-) rename .changeset/{slow-dodos-tease.md => sweet-keys-prove.md} (58%) diff --git a/.changeset/slow-dodos-tease.md b/.changeset/sweet-keys-prove.md similarity index 58% rename from .changeset/slow-dodos-tease.md rename to .changeset/sweet-keys-prove.md index 490d18add..6d816f419 100644 --- a/.changeset/slow-dodos-tease.md +++ b/.changeset/sweet-keys-prove.md @@ -3,6 +3,7 @@ --- #### Updated Files: + - `packages/react/legacy-interopt/src/create-fusion-context.ts` - `packages/react/legacy-interopt/src/create-service-resolver.ts` @@ -11,6 +12,21 @@ 1. **create-fusion-context.ts** - Added a call to `authContainer.handleWindowCallbackAsync()` before initializing `TelemetryLogger`. -2. **create-service-resolver.ts** +```ts +const authContainer = new LegacyAuthContainer({ auth: framework.modules.auth }); + +await authContainer.handleWindowCallbackAsync(); +const telemetryLogger = new TelemetryLogger(telemetry?.instrumentationKey ?? '', authContainer); +``` + +2. **create-service-resolver.ts** - Changed the third parameter of authContainer.registerAppAsync from false to true. + +```ts +return authContainer.registerAppAsync( + id, + uris.map((x) => x.uri), + true, +); +``` diff --git a/packages/react/legacy-interopt/src/LegacyAuthContainer.ts b/packages/react/legacy-interopt/src/LegacyAuthContainer.ts index 00bfc5ad9..70090e1b1 100644 --- a/packages/react/legacy-interopt/src/LegacyAuthContainer.ts +++ b/packages/react/legacy-interopt/src/LegacyAuthContainer.ts @@ -100,21 +100,21 @@ export class LegacyAuthContainer extends AuthContainer { throw new FusionAuthAppNotFoundError(resource); } if (this._registeredApps[app.clientId]) { - // TODO - const defaultScope = app.clientId + '/.default'; - const res = await this.#auth.acquireToken({ scopes: [defaultScope] }); - if (res && res.accessToken) { - return res.accessToken; - } - // if (!accessToken) { - throw Error('failed to aquire token'); - // } - // return accessToken; + return this.__acquireTokenAsync(app); } console.trace(`FusionAuthContainer::acquireTokenAsync ${resource}`); return super.acquireTokenAsync(resource); } + protected async __acquireTokenAsync(app: AuthApp): Promise { + const defaultScope = app.clientId + '/.default'; + const res = await this.#auth.acquireToken({ scopes: [defaultScope] }); + if (res && res.accessToken) { + return res.accessToken; + } + throw Error('failed to aquire token'); + } + /** internal registry of 'new' apps registred for msal */ protected _registeredApps: Record = {}; async registerAppAsync(clientId: string, resources: string[], legacy = true): Promise { @@ -144,20 +144,7 @@ export class LegacyAuthContainer extends AuthContainer { const app = this.resolveApp(resource); if (app && app.clientId === global.clientId) { - const refreshUrl = `/auth/refresh`; - try { - const response = await fetch(refreshUrl, { - credentials: 'include', - method: 'POST', - }); - - if (response.status === 200) { - return response.text(); - } - } catch (err) { - // @todo AI - console.error(err); - } + return this.__acquireTokenAsync(app); } return super.refreshTokenAsync(resource);