Skip to content

Commit

Permalink
chore(refactor): extract common functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-acondy committed Jul 1, 2021
1 parent 0fa56f7 commit b37ba85
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('isAccessTokenExpiring', () => {
const token = generateToken(7200)
expect(isAccessTokenExpiring(token)).toBeFalsy()
})

it('should return true if the token is expiring within the given time', () => {
const token = generateToken(7200)
expect(isAccessTokenExpiring(token, 8000)).toBeTruthy()
})

it('should return false if the token is not expiring within the given time', () => {
const token = generateToken(120)
expect(isAccessTokenExpiring(token, 60)).toBeFalsy()
})
})

describe('isRefreshTokenExpiring', () => {
Expand All @@ -42,6 +52,16 @@ describe('isRefreshTokenExpiring', () => {
const token = generateToken(120)
expect(isRefreshTokenExpiring(token)).toBeFalsy()
})

it('should return true if the token is expiring within the given time', () => {
const token = generateToken(7200)
expect(isRefreshTokenExpiring(token, 8000)).toBeTruthy()
})

it('should return false if the token is not expiring within the given time', () => {
const token = generateToken(120)
expect(isRefreshTokenExpiring(token, 60)).toBeFalsy()
})
})

const generateToken = (timeToLiveSeconds: number): string => {
Expand Down
25 changes: 18 additions & 7 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import jwtDecode from 'jwt-decode'
* Access Token. In the case that the Refresh Token is expired, 1 hour is enough to let
* most jobs finish.
* @param {string} token- token string that will be evaluated
* @param {number} timeToLiveSeconds - the amount of time that the token has before it expires, defaults to 3600
* @returns {boolean} a value indicating whether the token is about to expire
*/
export function isAccessTokenExpiring(token?: string): boolean {
export function isAccessTokenExpiring(
token?: string,
timeToLiveSeconds = 3600
): boolean {
if (!token) {
return true
}
const payload = jwtDecode<{ exp: number }>(token)
const timeToLive = payload.exp - new Date().valueOf() / 1000

return timeToLive <= 60 * 60 // 1 hour
return isTokenExpiring(token, timeToLiveSeconds)
}

/**
Expand All @@ -23,13 +25,22 @@ export function isAccessTokenExpiring(token?: string): boolean {
* credentials in a browser to obtain an authorisation code). 30 seconds is enough time
* to make a request for a final Access Token.
* @param {string} token- token string that will be evaluated
* @param {number} timeToLiveSeconds - the amount of time that the token has before it expires, defaults to 30
* @returns {boolean} a value indicating whether the token is about to expire
*/
export function isRefreshTokenExpiring(token?: string): boolean {
export function isRefreshTokenExpiring(
token?: string,
timeToLiveSeconds = 30
): boolean {
if (!token) {
return true
}
return isTokenExpiring(token, timeToLiveSeconds)
}

function isTokenExpiring(token: string, timeToLiveSeconds: number) {
const payload = jwtDecode<{ exp: number }>(token)
const timeToLive = payload.exp - new Date().valueOf() / 1000

return timeToLive <= 30 // 30 seconds
return timeToLive <= timeToLiveSeconds
}

0 comments on commit b37ba85

Please sign in to comment.