Skip to content

Commit

Permalink
Make token grace period configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Oct 21, 2023
1 parent 516f154 commit 628311e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/SaleorAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export interface SaleorAuthClientProps {
onAuthRefresh?: (isAuthenticating: boolean) => void;
saleorApiUrl: string;
storage?: Storage;
tokenGracePeriod?: number;
}

export class SaleorAuthClient {
// we'll assume a generous time of 2 seconds for api to
// process our request
private tokenGracePeriod = 2000;

private accessToken: string | null = null;
private tokenRefreshPromise: null | Promise<Response> = null;
private onAuthRefresh?: (isAuthenticating: boolean) => void;
Expand All @@ -37,7 +42,11 @@ export class SaleorAuthClient {
* ```
*/

constructor({ saleorApiUrl, storage, onAuthRefresh }: SaleorAuthClientProps) {
constructor({ saleorApiUrl, storage, onAuthRefresh, tokenGracePeriod }: SaleorAuthClientProps) {
if (tokenGracePeriod) {
this.tokenGracePeriod = tokenGracePeriod;
}

const internalStorage = storage || (typeof window !== "undefined" ? window.localStorage : undefined);
this.storageHandler = internalStorage
? new SaleorAuthStorageHandler(internalStorage, saleorApiUrl)
Expand Down Expand Up @@ -104,7 +113,7 @@ export class SaleorAuthClient {
invariant(refreshToken, "Missing refresh token in token refresh handler");

// the refresh already finished, proceed as normal
if (this.accessToken && !isExpiredToken(this.accessToken)) {
if (this.accessToken && !isExpiredToken(this.accessToken, this.tokenGracePeriod)) {
return this.fetchWithAuth(input, init, additionalParams);
}

Expand Down Expand Up @@ -186,7 +195,7 @@ export class SaleorAuthClient {
}

// access token is fine, add it to the request and proceed
if (this.accessToken && !isExpiredToken(this.accessToken)) {
if (this.accessToken && !isExpiredToken(this.accessToken, this.tokenGracePeriod)) {
return this.runAuthorizedRequest(input, init, additionalParams);
}

Expand Down
7 changes: 2 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TypedDocumentString } from "./graphql";

const MILLI_MULTIPLYER = 1000;
const TOKEN_GRACE_PERIOD = 2000;

interface TokenData {
iat: number;
Expand Down Expand Up @@ -34,10 +33,8 @@ export const getTokenIss = (token: string): string => {
return parsedTokenData.iss;
};

export const isExpiredToken = (token: string) => {
// we'll assume a generous time of 2 seconds for api to
// process our request
return getTokenExpiry(token) - TOKEN_GRACE_PERIOD <= Date.now();
export const isExpiredToken = (token: string, tokenGracePeriod: number) => {
return getTokenExpiry(token) - tokenGracePeriod <= Date.now();
};

// query here is document node but because of graphql-tag using
Expand Down

0 comments on commit 628311e

Please sign in to comment.