diff --git a/src/SaleorAuthClient.ts b/src/SaleorAuthClient.ts index f3ce99a..4a4aa89 100644 --- a/src/SaleorAuthClient.ts +++ b/src/SaleorAuthClient.ts @@ -135,7 +135,6 @@ export class SaleorAuthClient { // the refresh already finished, proceed as normal if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) { - console.log("Not expired, proceed"); return this.fetchWithAuth(input, init, additionalParams); } @@ -143,11 +142,9 @@ export class SaleorAuthClient { // if the promise is already there, use it if (this.tokenRefreshPromise) { - console.log("Token refresh promise already exists, wait for it"); const response = await this.tokenRefreshPromise; const res = (await response.clone().json()) as TokenRefreshResponse; - console.log(res); const { errors: graphqlErrors, @@ -168,15 +165,11 @@ export class SaleorAuthClient { const a = this.acessTokenStorage.getAccessToken(); this.acessTokenStorage.setAccessToken(token); const b = this.acessTokenStorage.getAccessToken(); - console.log(a === b); - console.log(a === token); - console.log(b === token); this.tokenRefreshPromise = null; return this.runAuthorizedRequest(input, init, additionalParams); } // this is the first failed request, initialize refresh - console.log("Initialize token refresh"); this.tokenRefreshPromise = fetch(this.saleorApiUrl, getRequestData(TOKEN_REFRESH, { refreshToken })); return this.fetchWithAuth(input, init, additionalParams); }; @@ -218,10 +211,8 @@ export class SaleorAuthClient { */ fetchWithAuth: FetchWithAdditionalParams = async (input, init, additionalParams) => { const refreshToken = this.refreshTokenStorage?.getRefreshToken(); - console.log({ refreshToken }); let accessToken = this.acessTokenStorage.getAccessToken(); - console.log({ accessToken }); if (!accessToken) { if (typeof document !== "undefined") { const tokenFromCookie = cookie.parse(document.cookie).token ?? null; @@ -235,18 +226,15 @@ export class SaleorAuthClient { // access token is fine, add it to the request and proceed if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) { - console.log("Not expired, proceed"); return this.runAuthorizedRequest(input, init, additionalParams); } // refresh token exists, try to authenticate if possible if (refreshToken) { - console.log("Refresh token exists, try to authenticate"); return this.handleRequestWithTokenRefresh(input, init, additionalParams); } // any regular mutation, no previous sign in, proceed - console.log("No refresh token, proceed"); return fetch(input, init); }; diff --git a/test/SaleorAuthClient.test.ts b/test/SaleorAuthClient.test.ts index 847dc3e..40aaebf 100644 --- a/test/SaleorAuthClient.test.ts +++ b/test/SaleorAuthClient.test.ts @@ -1,13 +1,14 @@ import { it, describe, vi, expect } from "vitest"; import { SaleorAuthClient } from "../src/SaleorAuthClient"; import { getRefreshTokenKey } from "../src/SaleorRefreshTokenStorageHandler"; +import type { StorageRepository } from "../src"; describe("SaleorAuthClient", () => { const mockStorage = { getItem: vi.fn(), setItem: vi.fn(), - }; - const storage = mockStorage as unknown as Storage; + removeItem: vi.fn(), + } satisfies StorageRepository; const masterStagingUrl = "https://master.staging.saleor.cloud/graphql/"; const otherApiUrl = "https://some-other-domain-auth-sdk.saleor.cloud/graphql/"; @@ -15,7 +16,7 @@ describe("SaleorAuthClient", () => { const onAuthRefresh = vi.fn(); const saleorAuthClient = new SaleorAuthClient({ saleorApiUrl: masterStagingUrl, - storage, + refreshTokenStorage: mockStorage, onAuthRefresh, }); @@ -34,7 +35,7 @@ describe("SaleorAuthClient", () => { const onAuthRefresh = vi.fn(); const saleorAuthClient = new SaleorAuthClient({ saleorApiUrl: masterStagingUrl, - storage, + refreshTokenStorage: mockStorage, onAuthRefresh, }); @@ -43,7 +44,7 @@ describe("SaleorAuthClient", () => { if (key === getRefreshTokenKey(masterStagingUrl)) { return refreshToken; } - return undefined; + return null; }); fetchMock.mockResponse(async (req) => { @@ -74,7 +75,7 @@ describe("SaleorAuthClient", () => { const onAuthRefresh = vi.fn(); const saleorAuthClient = new SaleorAuthClient({ saleorApiUrl: masterStagingUrl, - storage, + refreshTokenStorage: mockStorage, onAuthRefresh, }); @@ -83,7 +84,7 @@ describe("SaleorAuthClient", () => { if (key === getRefreshTokenKey(masterStagingUrl)) { return refreshToken; } - return undefined; + return null; }); fetchMock.mockResponse(async (req) => { @@ -114,7 +115,7 @@ describe("SaleorAuthClient", () => { const onAuthRefresh = vi.fn(); const saleorAuthClient = new SaleorAuthClient({ saleorApiUrl: otherApiUrl, - storage, + refreshTokenStorage: mockStorage, onAuthRefresh, }); @@ -131,7 +132,7 @@ describe("SaleorAuthClient", () => { const onAuthRefresh = vi.fn(); const saleorAuthClient = new SaleorAuthClient({ saleorApiUrl: masterStagingUrl, - storage, + refreshTokenStorage: mockStorage, onAuthRefresh, }); @@ -140,7 +141,7 @@ describe("SaleorAuthClient", () => { if (key === getRefreshTokenKey(masterStagingUrl)) { return refreshToken; } - return undefined; + return null; }); fetchMock.mockResponse(async (req) => {