From 2718b6a6ec51b08e8205294e405dcac0e3a4fb2b Mon Sep 17 00:00:00 2001 From: Charlie Greenman Date: Sat, 9 Nov 2024 16:42:43 -0500 Subject: [PATCH] Zeta 9273 fix remaining auth bug (#290) * remove unused create project * ZETA-9273: fix the access token logic for the app --- src/auth/auth.ts | 20 ++++++++++++-------- src/extension.ts | 32 -------------------------------- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 7f42ac1..c7a7ef5 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -55,16 +55,20 @@ export async function getAccessToken(context: vscode.ExtensionContext, isProduct let refreshTokenExpiry: number | undefined = 0; if (accessToken) { - const decodedAccessToken: any = jwtDecode(accessToken); - accessTokenExpiry = decodedAccessToken.exp * 1000; // Convert to milliseconds - } + try { + const decodedAccessToken: any = jwtDecode(accessToken); + accessTokenExpiry = decodedAccessToken.exp * 1000; // Convert to milliseconds - const now = Date.now(); + const now = Date.now(); + const fiveMinutesInMs = (5 * 60) * 1000; - if (now >= accessTokenExpiry - 5 * 60 * 1000) { // 5 minutes before expiry - try { - const newTokens = await refreshAccessToken(context, isProduction); - return newTokens.access_token; + // Check if token is expired or will expire in next 5 minutes + if (now >= accessTokenExpiry - fiveMinutesInMs) { + const newToken = await refreshAccessToken(context, isProduction); + return newToken; + } + + return accessToken; } catch (error) { console.error('Failed to refresh token:', error); await vscode.commands.executeCommand(COMMAND_AUTH0_AUTH); diff --git a/src/extension.ts b/src/extension.ts index 07f0a0d..b07fc0b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -176,38 +176,6 @@ export async function activate(context: vscode.ExtensionContext) { } ); - const createProject = vscode.commands.registerCommand( - COMMAND_CREATE_PROJECT, - async({path, projectName}) => { - const parameters = { - name: projectName, - infrastructureCommandPath: '.' - }; - const userId = await context.globalState.get(MEMENTO_RAZROO_USER_ID) as string; - const userOrgId = context.globalState.get(MEMENTO_RAZROO_ORG_ID) as string; - const generateVsCodeDownloadCodeParameters = { - pathId: path.pathId, - recipeId: path.recipeId, - stepId: path.stepId, - projectName: EMPTY, - pathOrgId: path.orgId, - userId: userId, - userOrgId: userOrgId, - vsCodeInstanceId: `${userId}-${userOrgId}`, - parameters: JSON.stringify(parameters) - }; - try { - const result = await generateVsCodeDownloadCode(generateVsCodeDownloadCodeParameters, context, isProduction); - const starterFolderPath = createPathForStarterRepo(context, projectName); - const data = result?.data?.generateVsCodeDownloadCode; - await saveFiles(data, context, isProduction, starterFolderPath); - } catch (error) { - console.log('COMMAND_CREATE_PROJECT'); - console.error(error); - } - } - ); - const connectProjectsTryToAuthCommmand = vscode.commands.registerCommand( COMMAND_CONNECT_PROJECTS_TRY_TO_AUTH, async({selectedProjects = [], disconnectedProjects = [], orgId}) => {