-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vue SDK v1.1.0 #98
Merged
Merged
Vue SDK v1.1.0 #98
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
import { describe, it, expect, afterEach } from 'vitest'; | ||
|
||
import { getAccessTokenExpirationMoment } from '.'; | ||
import { CookieAdapter, getAccessTokenExpirationMoment } from '.'; | ||
|
||
describe('getAccessTokenExpirationMoment', () => { | ||
afterEach(() => { | ||
document.cookie = | ||
'app.at_exp' + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | ||
}); | ||
|
||
it('Should get the "app.at_exp" cookie value in milliseconds', () => { | ||
const exp = Date.now(); | ||
document.cookie = `app.at_exp=${exp}`; | ||
expect(getAccessTokenExpirationMoment()).toBe(exp * 1000); | ||
}); | ||
it('Should return null if the cookie is not set', () => { | ||
expect(getAccessTokenExpirationMoment()).toBeNull(); | ||
|
||
it('Should return -1 if the cookie is not set', () => { | ||
expect(getAccessTokenExpirationMoment()).toBe(-1); | ||
}); | ||
|
||
it('Accepts a specific cookieName if one is provided', () => { | ||
const cookieName = 'my-special-cookie'; | ||
const exp = 1200; | ||
document.cookie = `${cookieName}=${exp}`; | ||
|
||
expect(getAccessTokenExpirationMoment(cookieName)).toBe(1200000); | ||
|
||
document.cookie = | ||
cookieName + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | ||
}); | ||
|
||
it('Will get the value from a cookieAdapter if one is passed in', () => { | ||
const value = '500'; | ||
const cookieAdapter: CookieAdapter = { | ||
at_exp() { | ||
return value; | ||
}, | ||
}; | ||
|
||
expect(getAccessTokenExpirationMoment(undefined, cookieAdapter)).toBe( | ||
500000, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
/** | ||
* Parses document.cookie for the access token expiration cookie value. | ||
* @returns {(number | null)} The moment of expiration in milliseconds since epoch. | ||
* Gets the `app.at_exp` cookie and converts it to milliseconds since epoch. | ||
* Returns -1 if the cookie is not present. | ||
* @param cookieName - defaults to `app.at_exp`. | ||
* @param adapter - SSR frameworks like Nuxt and Next will pass in an adapter. | ||
*/ | ||
export function getAccessTokenExpirationMoment( | ||
cookieName: string = 'app.at_exp', | ||
): number | null { | ||
const expCookie = document.cookie | ||
adapter?: CookieAdapter, | ||
): number | -1 { | ||
if (adapter) { | ||
return toMilliseconds(adapter.at_exp(cookieName)); | ||
} | ||
|
||
let cookie; | ||
|
||
try { | ||
// `document` throws a ReferenceError if this runs in a | ||
// non-browser environment such as an SSR framework like Nuxt or Next. | ||
cookie = document.cookie; | ||
} catch { | ||
console.error( | ||
'Error accessing cookies in fusionauth. If you are using SSR you must configure the SDK with a cookie adapter', | ||
); | ||
return -1; | ||
} | ||
|
||
const expCookie = cookie | ||
.split('; ') | ||
.map(c => c.split('=')) | ||
.find(([name]) => name === cookieName); | ||
const cookieValue = expCookie?.[1]; | ||
return cookieValue ? parseInt(cookieValue) * 1000 : null; | ||
|
||
return toMilliseconds(cookieValue); | ||
} | ||
|
||
export interface CookieAdapter { | ||
/** returns the `app.at_exp` cookie without manipulating the value. */ | ||
at_exp: (cookieName?: string) => number | string | undefined; | ||
} | ||
|
||
function toMilliseconds(seconds?: number | string): number { | ||
if (!seconds) return -1; | ||
else return Number(seconds) * 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JakeLo123
isLoggedIn()
feels like it should always return a bool no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will return a bool. I removed this
if(!this.at_exp) { return false }
guard because I switchat_exp
from typenumber | null
tonumber
where it's just-1
if it's not present.