From e2fb31a3c2d8a3cc4fa8142cef9389c27c903802 Mon Sep 17 00:00:00 2001 From: Robert Field Date: Fri, 29 Sep 2023 20:34:30 +0100 Subject: [PATCH] fix: expiration checker --- .../src/lib/authentication/get-token.ts | 11 +---------- packages/composable-cli/src/util/has-expired.test.ts | 11 ++++------- packages/composable-cli/src/util/has-expired.ts | 11 ++++------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/packages/composable-cli/src/lib/authentication/get-token.ts b/packages/composable-cli/src/lib/authentication/get-token.ts index e0136799..59d06865 100644 --- a/packages/composable-cli/src/lib/authentication/get-token.ts +++ b/packages/composable-cli/src/lib/authentication/get-token.ts @@ -57,13 +57,11 @@ export async function getToken( } } - const { expires, expires_in, refresh_token, access_token } = - credentialsResult.data + const { expires, refresh_token, access_token } = credentialsResult.data if ( hasExpiredWithThreshold( expires, - expires_in, 300, // 5 minutes ) ) { @@ -88,13 +86,6 @@ async function handleExpiredToken( const renewedToken = await renewToken(apiUrl, refresh_token) - if (process.env.NODE_ENV === "development") { - console.log( - "CALL WAS MADE TO RENEW TOKEN DID YOU EXPECT THIS? ", - renewedToken, - ) - } - if (!renewedToken.success) { handleClearCredentials(store) return { diff --git a/packages/composable-cli/src/util/has-expired.test.ts b/packages/composable-cli/src/util/has-expired.test.ts index ef9f0351..5e2767f8 100644 --- a/packages/composable-cli/src/util/has-expired.test.ts +++ b/packages/composable-cli/src/util/has-expired.test.ts @@ -4,9 +4,8 @@ describe("hasExpiredWithThreshold", () => { // Test case 1: Token has expired it("hasExpiredWithThreshold returns true when the token has expired", () => { const unixTimestamp = 1695056417 - const expiresIn = 3600 const threshold = 300 - const result = hasExpiredWithThreshold(unixTimestamp, expiresIn, threshold) + const result = hasExpiredWithThreshold(unixTimestamp, threshold) expect(result).toBe(true) }) @@ -14,9 +13,8 @@ describe("hasExpiredWithThreshold", () => { it("hasExpiredWithThreshold returns false when the token is still valid within the threshold", () => { const currentTimestamp = Math.floor(Date.now() / 1000) const unixTimestamp = currentTimestamp + 1800 // Expires in 30 minutes - const expiresIn = 3600 - const threshold = 3600 // 1 hour threshold - const result = hasExpiredWithThreshold(unixTimestamp, expiresIn, threshold) + const threshold = 300 // 1 hour threshold + const result = hasExpiredWithThreshold(unixTimestamp, threshold) expect(result).toBe(false) }) @@ -24,9 +22,8 @@ describe("hasExpiredWithThreshold", () => { it("hasExpiredWithThreshold returns false when the token is still valid and outside the threshold", () => { const currentTimestamp = Math.floor(Date.now() / 1000) const unixTimestamp = currentTimestamp + 3600 // Expires in 1 hour - const expiresIn = 7200 // Expires in 2 hours const threshold = 1800 // 30 minutes threshold - const result = hasExpiredWithThreshold(unixTimestamp, expiresIn, threshold) + const result = hasExpiredWithThreshold(unixTimestamp, threshold) expect(result).toBe(false) }) }) diff --git a/packages/composable-cli/src/util/has-expired.ts b/packages/composable-cli/src/util/has-expired.ts index 406d7a96..19654172 100644 --- a/packages/composable-cli/src/util/has-expired.ts +++ b/packages/composable-cli/src/util/has-expired.ts @@ -1,15 +1,12 @@ /** * - * @param timestamp in unix time - * @param expiresIn in seconds + * @param expiresTimestamp in unix time * @param threshold in seconds */ export function hasExpiredWithThreshold( - timestamp: number, - expiresIn: number, - threshold: number + expiresTimestamp: number, + threshold: number, ): boolean { const currentTimestamp = Math.floor(Date.now() / 1000) // Convert current time to Unix timestamp - const expirationTimestamp = timestamp + expiresIn - return expirationTimestamp - threshold <= currentTimestamp + return expiresTimestamp - threshold <= currentTimestamp }