Skip to content

Commit

Permalink
feat(legacy-interopt): protected method for aquireTokenAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
eikeland committed Nov 14, 2024
1 parent 53cfabf commit 18387fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
---

#### Updated Files:

- `packages/react/legacy-interopt/src/create-fusion-context.ts`
- `packages/react/legacy-interopt/src/create-service-resolver.ts`

Expand All @@ -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,
);
```
35 changes: 11 additions & 24 deletions packages/react/legacy-interopt/src/LegacyAuthContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
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<string, AuthApp> = {};
async registerAppAsync(clientId: string, resources: string[], legacy = true): Promise<boolean> {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 18387fb

Please sign in to comment.