Skip to content

Commit

Permalink
Merge pull request #97 from KxSystems/caleteet-update-insights-renew
Browse files Browse the repository at this point in the history
Update to fix browser screen flash when access token is expired.
  • Loading branch information
caleteeter authored Sep 7, 2023
2 parents 81796a1 + 32b6718 commit 713cc5b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
36 changes: 2 additions & 34 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ import { ScratchpadResult } from "../models/scratchpadResult";
import { Server } from "../models/server";
import { ServerObject } from "../models/serverObject";
import { DataSourcesPanel } from "../panels/datasource";
import {
getCurrentToken,
IToken,
refreshToken,
signIn,
} from "../services/kdbInsights/codeFlowLogin";
import { getCurrentToken } from "../services/kdbInsights/codeFlowLogin";
import { InsightsNode, KdbNode } from "../services/kdbTreeProvider";
import {
addLocalConnectionContexts,
Expand Down Expand Up @@ -321,34 +316,7 @@ export async function removeConnection(viewItem: KdbNode): Promise<void> {
export async function connectInsights(viewItem: InsightsNode): Promise<void> {
commands.executeCommand("kdb-results.focus");

let token: IToken | undefined;
const existingToken = await ext.context.secrets.get(viewItem.details.alias);
if (existingToken !== undefined) {
const storedToken: IToken = JSON.parse(existingToken);
if (new Date(storedToken.accessTokenExpirationDate) < new Date()) {
token = await refreshToken(
viewItem.details.server,
storedToken.refreshToken
);
if (token === undefined) {
token = await signIn(viewItem.details.server);
ext.context.secrets.store(
viewItem.details.alias,
JSON.stringify(token)
);
} else {
ext.context.secrets.store(
viewItem.details.alias,
JSON.stringify(token)
);
}
} else {
token = storedToken;
}
} else {
token = await signIn(viewItem.details.server);
ext.context.secrets.store(viewItem.details.alias, JSON.stringify(token));
}
await getCurrentToken(viewItem.details.server, viewItem.details.alias);

ext.outputChannel.appendLine(
`Connection established successfully to: ${viewItem.details.server}`
Expand Down
23 changes: 15 additions & 8 deletions src/services/kdbInsights/codeFlowLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,25 @@ export async function getCurrentToken(
return undefined;
}

const rawToken = await ext.context.secrets.get(serverAlias);

let token;
if (rawToken !== undefined) {
token = JSON.parse(rawToken!);
if (new Date(token.accessTokenExpirationDate) < new Date()) {
token = await signIn(serverName);
let token: IToken | undefined;
const existingToken = await ext.context.secrets.get(serverAlias);

if (existingToken !== undefined) {
const storedToken: IToken = JSON.parse(existingToken);
if (new Date(storedToken.accessTokenExpirationDate) < new Date()) {
token = await refreshToken(serverName, storedToken.refreshToken);
if (token === undefined) {
token = await signIn(serverName);
ext.context.secrets.store(serverAlias, JSON.stringify(token));
}
ext.context.secrets.store(serverAlias, JSON.stringify(token));
return token;
} else {
return storedToken;
}
} else {
token = await signIn(serverName);
ext.context.secrets.store(serverName, JSON.stringify(token));
ext.context.secrets.store(serverAlias, JSON.stringify(token));
}
return token;
}
Expand Down

0 comments on commit 713cc5b

Please sign in to comment.