Skip to content

[FEAT] Add customizable min. seconds before refreshing #32

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

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { currentTimeSeconds, getLocalStorageNumber, hasLocalStorage, hasWindow }
const LOGGED_IN_AT_KEY = "__PROPEL_AUTH_LOGGED_IN_AT"
const LOGGED_OUT_AT_KEY = "__PROPEL_AUTH_LOGGED_OUT_AT"
const AUTH_TOKEN_REFRESH_BEFORE_EXPIRATION_SECONDS = 10 * 60
const DEBOUNCE_DURATION_FOR_REFOCUS_SECONDS = 60
const DEFAULT_MIN_SECONDS_BEFORE_REFRESH = 60 * 2
const ACTIVE_ORG_ACCESS_TOKEN_REFRESH_EXPIRATION_SECONDS = 60 * 5

const encodeBase64 = (str: string) => {
Expand Down Expand Up @@ -178,6 +178,12 @@ export interface IAuthOptions {
* Default true
*/
enableBackgroundTokenRefresh?: boolean

/**
* Minimum number of seconds before refreshing the token again.
* Defaults to 120 seconds.
*/
minSecondsBeforeRefresh?: number
}

interface AccessTokenActiveOrgMap {
Expand Down Expand Up @@ -216,6 +222,8 @@ function validateAndCleanupOptions(authOptions: IAuthOptions) {
}

export function createClient(authOptions: IAuthOptions): IAuthClient {
const { minSecondsBeforeRefresh = DEFAULT_MIN_SECONDS_BEFORE_REFRESH } = authOptions

validateAndCleanupOptions(authOptions)

// Internal state
Expand Down Expand Up @@ -631,10 +639,7 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
// If we were offline or on a different tab, when we return, refetch auth info
// Some browsers trigger focus more often than we'd like, so we'll debounce a little here as well
const onOnlineOrFocus = async function () {
if (
clientState.lastRefresh &&
currentTimeSeconds() > clientState.lastRefresh + DEBOUNCE_DURATION_FOR_REFOCUS_SECONDS
) {
if (clientState.lastRefresh && currentTimeSeconds() > clientState.lastRefresh + minSecondsBeforeRefresh) {
await forceRefreshToken(true)
} else {
await client.getAuthenticationInfoOrNull()
Expand Down
Loading