Skip to content

Commit

Permalink
fix: expiration checker
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Sep 29, 2023
1 parent 3d913b6 commit e2fb31a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
11 changes: 1 addition & 10 deletions packages/composable-cli/src/lib/authentication/get-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
) {
Expand All @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions packages/composable-cli/src/util/has-expired.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@ 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)
})

// Test case 2: Token is still valid within the threshold
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)
})

// Test case 3: Token is still valid and outside the threshold
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)
})
})
11 changes: 4 additions & 7 deletions packages/composable-cli/src/util/has-expired.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e2fb31a

Please sign in to comment.