diff --git a/src/commands/serverCommand.ts b/src/commands/serverCommand.ts index d8d7cea7..2bbc8118 100644 --- a/src/commands/serverCommand.ts +++ b/src/commands/serverCommand.ts @@ -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, @@ -321,34 +316,7 @@ export async function removeConnection(viewItem: KdbNode): Promise { export async function connectInsights(viewItem: InsightsNode): Promise { 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}` diff --git a/src/services/kdbInsights/codeFlowLogin.ts b/src/services/kdbInsights/codeFlowLogin.ts index 0597e4a8..2cce30b6 100644 --- a/src/services/kdbInsights/codeFlowLogin.ts +++ b/src/services/kdbInsights/codeFlowLogin.ts @@ -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; }