Skip to content

Commit

Permalink
Merge pull request #4 from auth0-training/fix/create-new-client-on-to…
Browse files Browse the repository at this point in the history
…kenset-update

recreate the managment client on tokenset update
  • Loading branch information
Bobby Johnson authored Sep 10, 2021
2 parents a5e1cd9 + 90321fe commit d3512cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [1.1.2] - 2021-09-09
### Changed
- fixed bug that caused incorrect tokenset to be used when switching tenants.
- fixed bug that caused incorrect tokenset to be used when switching tenants.

## [1.1.3] - 2021-09-10
### Changed
- fixed bug that caused incorrect management client to be used when switching tenants.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-labs",
"preview": true,
"displayName": "Auth0 Labs",
"version": "1.1.2",
"version": "1.1.3",
"description": "A Visual Studio Code extension for training lab automation and quick access to tenant information.",
"main": "./dist/extension.js",
"publisher": "auth0",
Expand Down
19 changes: 13 additions & 6 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import type { TokenSet } from 'openid-client';
import { Auth } from '../auth';

let managementClient: ManagementClient;
let tokenSet: TokenSet | undefined;
let tokenSet: TokenSet;

Auth.onAuthStatusChanged(async (newTokenSet) => {
tokenSet = newTokenSet;
if (newTokenSet) {
tokenSet = newTokenSet;
createManagementClient(newTokenSet);
}
});

function createManagementClient(tokenSet: TokenSet): ManagementClient {
return (managementClient = new ManagementClient({
token: tokenSet.access_token,
domain: getDomainFromToken(tokenSet.access_token!),
}));
}

export async function getClient(): Promise<ManagementClient> {
if (!tokenSet || tokenSet?.expired()) {
const newTokenSet = await Auth.getTokenSet();
if (newTokenSet && newTokenSet.access_token) {
tokenSet = newTokenSet;
managementClient = new ManagementClient({
token: newTokenSet.access_token,
domain: getDomainFromToken(newTokenSet.access_token),
});
managementClient = createManagementClient(newTokenSet);
}
}
return managementClient;
Expand Down

0 comments on commit d3512cc

Please sign in to comment.