Skip to content

Commit

Permalink
ZETA-9273: fix the access token logic for the app
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieGreenman committed Nov 9, 2024
1 parent 4b43305 commit 2b509f8
Showing 1 changed file with 12 additions and 8 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

0 comments on commit 2b509f8

Please sign in to comment.