Skip to content

Commit

Permalink
Zeta 9273 fix remaining auth bug (#290)
Browse files Browse the repository at this point in the history
* remove unused create project

* ZETA-9273: fix the access token logic for the app
  • Loading branch information
CharlieGreenman authored Nov 9, 2024
1 parent d1f6b64 commit 2718b6a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
20 changes: 12 additions & 8 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 0 additions & 32 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down

0 comments on commit 2718b6a

Please sign in to comment.