Skip to content

Commit

Permalink
fix: refresh proper token based on owner (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzuraw authored Apr 18, 2023
1 parent 27dd252 commit ad9b1a0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
node_modules
dist
.env
.envrc
.envrc
*.tgz
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@
"size-limit": [
{
"path": "dist/sdk.cjs.production.min.js",
"limit": "10 KB"
"limit": "11 KB"
},
{
"path": "dist/sdk.esm.js",
"limit": "10 KB"
"limit": "11 KB"
}
],
"devDependencies": {
Expand Down
30 changes: 16 additions & 14 deletions src/apollo/client.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {
ApolloClient,
createHttpLink,
FetchResult,
InMemoryCache,
NormalizedCacheObject,
Reference,
FetchResult,
createHttpLink,
} from "@apollo/client";
import fetch from "cross-fetch";
import jwtDecode from "jwt-decode";

import { TypedTypePolicies } from "./apollo-helpers";
import { JWTToken } from "../core";
import { AuthSDK, auth } from "../core/auth";
import { storage } from "../core/storage";
import { isInternalToken } from "../helpers";
import { TypedTypePolicies } from "./apollo-helpers";
import { ExternalRefreshMutation, RefreshTokenMutation } from "./types";

let client: ApolloClient<NormalizedCacheObject>;
Expand Down Expand Up @@ -57,7 +58,6 @@ export const createFetch = ({
}

let token = storage.getAccessToken();
const authPluginId = storage.getAuthPluginId();

try {
if (
Expand All @@ -73,18 +73,19 @@ export const createFetch = ({

if (autoTokenRefresh && token) {
// auto refresh token before provided time skew (in seconds) until it expires
const expirationTime =
(jwtDecode<JWTToken>(token).exp - tokenRefreshTimeSkew) * 1000;
const decodedToken = jwtDecode<JWTToken>(token);
const expirationTime = (decodedToken.exp - tokenRefreshTimeSkew) * 1000;
const owner = decodedToken.owner;

try {
if (refreshPromise) {
await refreshPromise;
} else if (Date.now() >= expirationTime) {
// refreshToken automatically updates token in storage
refreshPromise = authPluginId
? authClient.refreshExternalToken()
: authClient.refreshToken();
await refreshPromise;
if (isInternalToken(owner)) {
await authClient.refreshToken();
} else {
await authClient.refreshExternalToken();
}
}
} catch (e) {
} finally {
Expand All @@ -111,15 +112,16 @@ export const createFetch = ({
Record<string, unknown>,
Record<string, unknown>
> | null = null;
const owner = jwtDecode<JWTToken>(token).owner;

if (isUnauthenticated) {
try {
if (refreshPromise) {
refreshTokenResponse = await refreshPromise;
} else {
refreshPromise = authPluginId
? authClient.refreshExternalToken()
: authClient.refreshToken();
refreshPromise = isInternalToken(owner)
? authClient.refreshToken()
: authClient.refreshExternalToken();
refreshTokenResponse = await refreshPromise;
}

Expand Down
23 changes: 13 additions & 10 deletions src/core/createSaleorClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createApolloClient } from "../apollo";
import { auth } from "./auth";
import { user } from "./user";
import { getState, State } from "./state";
import { createApolloClient } from "../apollo";
import { SaleorClient, SaleorClientOpts } from "./types";
import { JWTToken, SaleorClient, SaleorClientOpts } from "./types";
import { user } from "./user";

import { createStorage, storage } from "./storage";
import jwtDecode from "jwt-decode";
import { DEVELOPMENT_MODE, WINDOW_EXISTS } from "../constants";
import { isInternalToken } from "../helpers";
import { createStorage, storage } from "./storage";

export const createSaleorClient = ({
apiUrl,
Expand All @@ -26,14 +28,15 @@ export const createSaleorClient = ({
const authSDK = auth(coreInternals);
const userSDK = user(coreInternals);

if (autologin) {
const refreshToken = storage.getRefreshToken();
const authPluginId = storage.getAuthPluginId();
const refreshToken = storage.getRefreshToken();

if (refreshToken && authPluginId) {
authSDK.refreshExternalToken(true);
} else if (refreshToken) {
if (autologin && refreshToken) {
const owner = jwtDecode<JWTToken>(refreshToken).owner;

if (isInternalToken(owner)) {
authSDK.refreshToken(true);
} else {
authSDK.refreshExternalToken(true);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isInternalToken = (owner: string): boolean => owner === "saleor";
1 change: 1 addition & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const createTestToken = (
{
data: Math.random(), // to prevent generating the same tokens within the same second - some tests may try to create tokens quickly
exp: Math.floor(Date.now() / 1000) + expirationPeriodInSeconds,
owner: "saleor",
},
testTokenSecret
);
Expand Down

0 comments on commit ad9b1a0

Please sign in to comment.