@shopsbuilder/auth-sdk 1.2.8 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/SaleorAuthClient.d.mts +4 -3
- package/dist/SaleorAuthClient.d.ts +4 -3
- package/dist/SaleorAuthClient.js +45 -60
- package/dist/SaleorAuthClient.mjs +1 -1
- package/dist/{chunk-X3MKOESH.mjs → chunk-3UGMWVQN.mjs} +42 -60
- package/dist/{chunk-WJVMUY3P.mjs → chunk-FY2QDW7J.mjs} +5 -4
- package/dist/index.js +45 -60
- package/dist/index.mjs +1 -1
- package/dist/react/SaleorAuthProvider.mjs +1 -1
- package/dist/react/context.mjs +1 -1
- package/dist/react/index.js +5 -4
- package/dist/react/index.mjs +2 -2
- package/dist/react/useAuthChange.d.mts +2 -1
- package/dist/react/useAuthChange.d.ts +2 -1
- package/dist/react/useAuthChange.js +5 -4
- package/dist/react/useAuthChange.mjs +1 -1
- package/package.json +1 -1
|
@@ -4,9 +4,10 @@ interface SaleorAuthClientProps {
|
|
|
4
4
|
onAuthRefresh?: (isAuthenticating: boolean) => void;
|
|
5
5
|
saleorApiUrl: string;
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* Override the key prefix used for storage/cookies.
|
|
8
|
+
* Defaults to `saleorApiUrl`. Useful when the server uses a different
|
|
9
|
+
* internal URL (e.g. `http://api:8000`) while the client uses the public
|
|
10
|
+
* URL — pass the client URL here so cookie names match.
|
|
10
11
|
*/
|
|
11
12
|
storageKeyPrefix?: string;
|
|
12
13
|
refreshTokenStorage?: StorageRepository;
|
|
@@ -4,9 +4,10 @@ interface SaleorAuthClientProps {
|
|
|
4
4
|
onAuthRefresh?: (isAuthenticating: boolean) => void;
|
|
5
5
|
saleorApiUrl: string;
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* Override the key prefix used for storage/cookies.
|
|
8
|
+
* Defaults to `saleorApiUrl`. Useful when the server uses a different
|
|
9
|
+
* internal URL (e.g. `http://api:8000`) while the client uses the public
|
|
10
|
+
* URL — pass the client URL here so cookie names match.
|
|
10
11
|
*/
|
|
11
12
|
storageKeyPrefix?: string;
|
|
12
13
|
refreshTokenStorage?: StorageRepository;
|
package/dist/SaleorAuthClient.js
CHANGED
|
@@ -110,6 +110,10 @@ var getTokenExpiry = (token) => {
|
|
|
110
110
|
const parsedTokenData = decodeToken(token);
|
|
111
111
|
return parsedTokenData.exp * MILLI_MULTIPLYER || 0;
|
|
112
112
|
};
|
|
113
|
+
var getTokenIss = (token) => {
|
|
114
|
+
const parsedTokenData = decodeToken(token);
|
|
115
|
+
return parsedTokenData.iss;
|
|
116
|
+
};
|
|
113
117
|
var isExpiredToken = (token, tokenGracePeriod) => {
|
|
114
118
|
return getTokenExpiry(token) - tokenGracePeriod <= Date.now();
|
|
115
119
|
};
|
|
@@ -312,14 +316,39 @@ var SaleorAuthClient = class {
|
|
|
312
316
|
cleanup = () => {
|
|
313
317
|
this.refreshTokenStorage?.cleanup();
|
|
314
318
|
};
|
|
315
|
-
runAuthorizedRequest = (input, init) => {
|
|
319
|
+
runAuthorizedRequest = (input, init, additionalParams) => {
|
|
316
320
|
const token = this.accessTokenStorage.getAccessToken();
|
|
317
321
|
if (!token) {
|
|
318
322
|
return fetch(input, init);
|
|
319
323
|
}
|
|
320
|
-
const headers =
|
|
321
|
-
|
|
322
|
-
|
|
324
|
+
const headers = init?.headers || {};
|
|
325
|
+
const getURL = (input2) => {
|
|
326
|
+
if (typeof input2 === "string") {
|
|
327
|
+
return input2;
|
|
328
|
+
} else if ("url" in input2) {
|
|
329
|
+
return input2.url;
|
|
330
|
+
} else {
|
|
331
|
+
return input2.href;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const iss = getTokenIss(token);
|
|
335
|
+
const issuerAndDomainMatch = getURL(input) === iss;
|
|
336
|
+
const shouldAddAuthorizationHeader = issuerAndDomainMatch || additionalParams?.allowPassingTokenToThirdPartyDomains;
|
|
337
|
+
if (!issuerAndDomainMatch) {
|
|
338
|
+
if (shouldAddAuthorizationHeader) {
|
|
339
|
+
console.warn(
|
|
340
|
+
"Token's `iss` and request URL do not match but `allowPassingTokenToThirdPartyDomains` was specified."
|
|
341
|
+
);
|
|
342
|
+
} else {
|
|
343
|
+
console.warn(
|
|
344
|
+
"Token's `iss` and request URL do not match. Not adding `Authorization` header to the request."
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return fetch(input, {
|
|
349
|
+
...init,
|
|
350
|
+
headers: shouldAddAuthorizationHeader ? { ...headers, Authorization: `Bearer ${token}` } : headers
|
|
351
|
+
});
|
|
323
352
|
};
|
|
324
353
|
handleRequestWithTokenRefresh = async (input, requestInit, additionalParams) => {
|
|
325
354
|
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
@@ -331,37 +360,15 @@ var SaleorAuthClient = class {
|
|
|
331
360
|
this.onAuthRefresh?.(true);
|
|
332
361
|
if (this.tokenRefreshPromise) {
|
|
333
362
|
const response = await this.tokenRefreshPromise;
|
|
334
|
-
const
|
|
335
|
-
const
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
body: rawText.slice(0, 500)
|
|
342
|
-
}));
|
|
343
|
-
let res;
|
|
344
|
-
try {
|
|
345
|
-
res = await responseClone.json();
|
|
346
|
-
} catch {
|
|
347
|
-
console.error("[auth-sdk] Token refresh response is not valid JSON, status:", response.status);
|
|
348
|
-
this.onAuthRefresh?.(false);
|
|
349
|
-
this.tokenRefreshPromise = null;
|
|
350
|
-
this.refreshTokenStorage?.clearAuthStorage();
|
|
351
|
-
return fetch(input, requestInit);
|
|
352
|
-
}
|
|
353
|
-
const graphqlErrors = res.errors;
|
|
354
|
-
const token = res.data?.tokenRefresh?.token;
|
|
355
|
-
const refreshErrors = res.data?.tokenRefresh?.errors;
|
|
363
|
+
const res = await response.clone().json();
|
|
364
|
+
const {
|
|
365
|
+
errors: graphqlErrors,
|
|
366
|
+
data: {
|
|
367
|
+
tokenRefresh: { errors, token }
|
|
368
|
+
}
|
|
369
|
+
} = res;
|
|
356
370
|
this.onAuthRefresh?.(false);
|
|
357
|
-
if (
|
|
358
|
-
console.warn("[auth-sdk] Token refresh failed:", JSON.stringify({
|
|
359
|
-
graphqlErrors: graphqlErrors ?? [],
|
|
360
|
-
refreshErrors: refreshErrors ?? [],
|
|
361
|
-
hasToken: Boolean(token),
|
|
362
|
-
httpStatus: response.status,
|
|
363
|
-
rawData: res.data ?? null
|
|
364
|
-
}));
|
|
371
|
+
if (errors?.length || graphqlErrors?.length || !token) {
|
|
365
372
|
this.tokenRefreshPromise = null;
|
|
366
373
|
this.refreshTokenStorage?.clearAuthStorage();
|
|
367
374
|
return fetch(input, requestInit);
|
|
@@ -371,32 +378,10 @@ var SaleorAuthClient = class {
|
|
|
371
378
|
this.tokenRefreshPromise = null;
|
|
372
379
|
return this.runAuthorizedRequest(input, requestInit, additionalParams);
|
|
373
380
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
const origHeaders = new Headers(requestInit.headers);
|
|
379
|
-
for (const [key, value] of origHeaders.entries()) {
|
|
380
|
-
if (key !== "host" && key !== "content-type") {
|
|
381
|
-
refreshHeaders[key] = value;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
const refreshBody = JSON.stringify({
|
|
386
|
-
query: TOKEN_REFRESH.toString(),
|
|
387
|
-
variables: { refreshToken }
|
|
388
|
-
});
|
|
389
|
-
console.log("[auth-sdk] Token refresh request:", JSON.stringify({
|
|
390
|
-
url: this.saleorApiUrl,
|
|
391
|
-
headers: refreshHeaders,
|
|
392
|
-
bodyLength: refreshBody.length,
|
|
393
|
-
bodyPreview: refreshBody.slice(0, 200)
|
|
394
|
-
}));
|
|
395
|
-
this.tokenRefreshPromise = fetch(this.saleorApiUrl, {
|
|
396
|
-
method: "POST",
|
|
397
|
-
headers: refreshHeaders,
|
|
398
|
-
body: refreshBody
|
|
399
|
-
});
|
|
381
|
+
this.tokenRefreshPromise = fetch(
|
|
382
|
+
this.saleorApiUrl,
|
|
383
|
+
getRequestData(TOKEN_REFRESH, { refreshToken }, { ...this.defaultRequestInit, ...requestInit })
|
|
384
|
+
);
|
|
400
385
|
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
401
386
|
};
|
|
402
387
|
handleSignIn = async (response) => {
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from "./chunk-263DHBMK.mjs";
|
|
7
7
|
import {
|
|
8
8
|
getRequestData,
|
|
9
|
+
getTokenIss,
|
|
9
10
|
invariant,
|
|
10
11
|
isExpiredToken
|
|
11
12
|
} from "./chunk-UDLCOX6B.mjs";
|
|
@@ -68,14 +69,39 @@ var SaleorAuthClient = class {
|
|
|
68
69
|
cleanup = () => {
|
|
69
70
|
this.refreshTokenStorage?.cleanup();
|
|
70
71
|
};
|
|
71
|
-
runAuthorizedRequest = (input, init) => {
|
|
72
|
+
runAuthorizedRequest = (input, init, additionalParams) => {
|
|
72
73
|
const token = this.accessTokenStorage.getAccessToken();
|
|
73
74
|
if (!token) {
|
|
74
75
|
return fetch(input, init);
|
|
75
76
|
}
|
|
76
|
-
const headers =
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
const headers = init?.headers || {};
|
|
78
|
+
const getURL = (input2) => {
|
|
79
|
+
if (typeof input2 === "string") {
|
|
80
|
+
return input2;
|
|
81
|
+
} else if ("url" in input2) {
|
|
82
|
+
return input2.url;
|
|
83
|
+
} else {
|
|
84
|
+
return input2.href;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const iss = getTokenIss(token);
|
|
88
|
+
const issuerAndDomainMatch = getURL(input) === iss;
|
|
89
|
+
const shouldAddAuthorizationHeader = issuerAndDomainMatch || additionalParams?.allowPassingTokenToThirdPartyDomains;
|
|
90
|
+
if (!issuerAndDomainMatch) {
|
|
91
|
+
if (shouldAddAuthorizationHeader) {
|
|
92
|
+
console.warn(
|
|
93
|
+
"Token's `iss` and request URL do not match but `allowPassingTokenToThirdPartyDomains` was specified."
|
|
94
|
+
);
|
|
95
|
+
} else {
|
|
96
|
+
console.warn(
|
|
97
|
+
"Token's `iss` and request URL do not match. Not adding `Authorization` header to the request."
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return fetch(input, {
|
|
102
|
+
...init,
|
|
103
|
+
headers: shouldAddAuthorizationHeader ? { ...headers, Authorization: `Bearer ${token}` } : headers
|
|
104
|
+
});
|
|
79
105
|
};
|
|
80
106
|
handleRequestWithTokenRefresh = async (input, requestInit, additionalParams) => {
|
|
81
107
|
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
@@ -87,37 +113,15 @@ var SaleorAuthClient = class {
|
|
|
87
113
|
this.onAuthRefresh?.(true);
|
|
88
114
|
if (this.tokenRefreshPromise) {
|
|
89
115
|
const response = await this.tokenRefreshPromise;
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
body: rawText.slice(0, 500)
|
|
98
|
-
}));
|
|
99
|
-
let res;
|
|
100
|
-
try {
|
|
101
|
-
res = await responseClone.json();
|
|
102
|
-
} catch {
|
|
103
|
-
console.error("[auth-sdk] Token refresh response is not valid JSON, status:", response.status);
|
|
104
|
-
this.onAuthRefresh?.(false);
|
|
105
|
-
this.tokenRefreshPromise = null;
|
|
106
|
-
this.refreshTokenStorage?.clearAuthStorage();
|
|
107
|
-
return fetch(input, requestInit);
|
|
108
|
-
}
|
|
109
|
-
const graphqlErrors = res.errors;
|
|
110
|
-
const token = res.data?.tokenRefresh?.token;
|
|
111
|
-
const refreshErrors = res.data?.tokenRefresh?.errors;
|
|
116
|
+
const res = await response.clone().json();
|
|
117
|
+
const {
|
|
118
|
+
errors: graphqlErrors,
|
|
119
|
+
data: {
|
|
120
|
+
tokenRefresh: { errors, token }
|
|
121
|
+
}
|
|
122
|
+
} = res;
|
|
112
123
|
this.onAuthRefresh?.(false);
|
|
113
|
-
if (
|
|
114
|
-
console.warn("[auth-sdk] Token refresh failed:", JSON.stringify({
|
|
115
|
-
graphqlErrors: graphqlErrors ?? [],
|
|
116
|
-
refreshErrors: refreshErrors ?? [],
|
|
117
|
-
hasToken: Boolean(token),
|
|
118
|
-
httpStatus: response.status,
|
|
119
|
-
rawData: res.data ?? null
|
|
120
|
-
}));
|
|
124
|
+
if (errors?.length || graphqlErrors?.length || !token) {
|
|
121
125
|
this.tokenRefreshPromise = null;
|
|
122
126
|
this.refreshTokenStorage?.clearAuthStorage();
|
|
123
127
|
return fetch(input, requestInit);
|
|
@@ -127,32 +131,10 @@ var SaleorAuthClient = class {
|
|
|
127
131
|
this.tokenRefreshPromise = null;
|
|
128
132
|
return this.runAuthorizedRequest(input, requestInit, additionalParams);
|
|
129
133
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const origHeaders = new Headers(requestInit.headers);
|
|
135
|
-
for (const [key, value] of origHeaders.entries()) {
|
|
136
|
-
if (key !== "host" && key !== "content-type") {
|
|
137
|
-
refreshHeaders[key] = value;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
const refreshBody = JSON.stringify({
|
|
142
|
-
query: TOKEN_REFRESH.toString(),
|
|
143
|
-
variables: { refreshToken }
|
|
144
|
-
});
|
|
145
|
-
console.log("[auth-sdk] Token refresh request:", JSON.stringify({
|
|
146
|
-
url: this.saleorApiUrl,
|
|
147
|
-
headers: refreshHeaders,
|
|
148
|
-
bodyLength: refreshBody.length,
|
|
149
|
-
bodyPreview: refreshBody.slice(0, 200)
|
|
150
|
-
}));
|
|
151
|
-
this.tokenRefreshPromise = fetch(this.saleorApiUrl, {
|
|
152
|
-
method: "POST",
|
|
153
|
-
headers: refreshHeaders,
|
|
154
|
-
body: refreshBody
|
|
155
|
-
});
|
|
134
|
+
this.tokenRefreshPromise = fetch(
|
|
135
|
+
this.saleorApiUrl,
|
|
136
|
+
getRequestData(TOKEN_REFRESH, { refreshToken }, { ...this.defaultRequestInit, ...requestInit })
|
|
137
|
+
);
|
|
156
138
|
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
157
139
|
};
|
|
158
140
|
handleSignIn = async (response) => {
|
|
@@ -4,9 +4,10 @@ import {
|
|
|
4
4
|
|
|
5
5
|
// src/react/useAuthChange.ts
|
|
6
6
|
import { useEffect } from "react";
|
|
7
|
-
var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
7
|
+
var useAuthChange = ({ saleorApiUrl, storageKeyPrefix, onSignedOut, onSignedIn }) => {
|
|
8
|
+
const keyPrefix = storageKeyPrefix ?? saleorApiUrl;
|
|
8
9
|
const handleAuthChange = (event) => {
|
|
9
|
-
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(
|
|
10
|
+
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(keyPrefix);
|
|
10
11
|
if (!isCustomAuthEvent) {
|
|
11
12
|
return;
|
|
12
13
|
}
|
|
@@ -21,9 +22,9 @@ var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
|
21
22
|
if (typeof window === "undefined") {
|
|
22
23
|
return;
|
|
23
24
|
}
|
|
24
|
-
window.addEventListener(getStorageAuthEventKey(
|
|
25
|
+
window.addEventListener(getStorageAuthEventKey(keyPrefix), handleAuthChange);
|
|
25
26
|
return () => {
|
|
26
|
-
window.removeEventListener(getStorageAuthEventKey(
|
|
27
|
+
window.removeEventListener(getStorageAuthEventKey(keyPrefix), handleAuthChange);
|
|
27
28
|
};
|
|
28
29
|
}, []);
|
|
29
30
|
};
|
package/dist/index.js
CHANGED
|
@@ -113,6 +113,10 @@ var getTokenExpiry = (token) => {
|
|
|
113
113
|
const parsedTokenData = decodeToken(token);
|
|
114
114
|
return parsedTokenData.exp * MILLI_MULTIPLYER || 0;
|
|
115
115
|
};
|
|
116
|
+
var getTokenIss = (token) => {
|
|
117
|
+
const parsedTokenData = decodeToken(token);
|
|
118
|
+
return parsedTokenData.iss;
|
|
119
|
+
};
|
|
116
120
|
var isExpiredToken = (token, tokenGracePeriod) => {
|
|
117
121
|
return getTokenExpiry(token) - tokenGracePeriod <= Date.now();
|
|
118
122
|
};
|
|
@@ -315,14 +319,39 @@ var SaleorAuthClient = class {
|
|
|
315
319
|
cleanup = () => {
|
|
316
320
|
this.refreshTokenStorage?.cleanup();
|
|
317
321
|
};
|
|
318
|
-
runAuthorizedRequest = (input, init) => {
|
|
322
|
+
runAuthorizedRequest = (input, init, additionalParams) => {
|
|
319
323
|
const token = this.accessTokenStorage.getAccessToken();
|
|
320
324
|
if (!token) {
|
|
321
325
|
return fetch(input, init);
|
|
322
326
|
}
|
|
323
|
-
const headers =
|
|
324
|
-
|
|
325
|
-
|
|
327
|
+
const headers = init?.headers || {};
|
|
328
|
+
const getURL = (input2) => {
|
|
329
|
+
if (typeof input2 === "string") {
|
|
330
|
+
return input2;
|
|
331
|
+
} else if ("url" in input2) {
|
|
332
|
+
return input2.url;
|
|
333
|
+
} else {
|
|
334
|
+
return input2.href;
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const iss = getTokenIss(token);
|
|
338
|
+
const issuerAndDomainMatch = getURL(input) === iss;
|
|
339
|
+
const shouldAddAuthorizationHeader = issuerAndDomainMatch || additionalParams?.allowPassingTokenToThirdPartyDomains;
|
|
340
|
+
if (!issuerAndDomainMatch) {
|
|
341
|
+
if (shouldAddAuthorizationHeader) {
|
|
342
|
+
console.warn(
|
|
343
|
+
"Token's `iss` and request URL do not match but `allowPassingTokenToThirdPartyDomains` was specified."
|
|
344
|
+
);
|
|
345
|
+
} else {
|
|
346
|
+
console.warn(
|
|
347
|
+
"Token's `iss` and request URL do not match. Not adding `Authorization` header to the request."
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return fetch(input, {
|
|
352
|
+
...init,
|
|
353
|
+
headers: shouldAddAuthorizationHeader ? { ...headers, Authorization: `Bearer ${token}` } : headers
|
|
354
|
+
});
|
|
326
355
|
};
|
|
327
356
|
handleRequestWithTokenRefresh = async (input, requestInit, additionalParams) => {
|
|
328
357
|
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
@@ -334,37 +363,15 @@ var SaleorAuthClient = class {
|
|
|
334
363
|
this.onAuthRefresh?.(true);
|
|
335
364
|
if (this.tokenRefreshPromise) {
|
|
336
365
|
const response = await this.tokenRefreshPromise;
|
|
337
|
-
const
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
body: rawText.slice(0, 500)
|
|
345
|
-
}));
|
|
346
|
-
let res;
|
|
347
|
-
try {
|
|
348
|
-
res = await responseClone.json();
|
|
349
|
-
} catch {
|
|
350
|
-
console.error("[auth-sdk] Token refresh response is not valid JSON, status:", response.status);
|
|
351
|
-
this.onAuthRefresh?.(false);
|
|
352
|
-
this.tokenRefreshPromise = null;
|
|
353
|
-
this.refreshTokenStorage?.clearAuthStorage();
|
|
354
|
-
return fetch(input, requestInit);
|
|
355
|
-
}
|
|
356
|
-
const graphqlErrors = res.errors;
|
|
357
|
-
const token = res.data?.tokenRefresh?.token;
|
|
358
|
-
const refreshErrors = res.data?.tokenRefresh?.errors;
|
|
366
|
+
const res = await response.clone().json();
|
|
367
|
+
const {
|
|
368
|
+
errors: graphqlErrors,
|
|
369
|
+
data: {
|
|
370
|
+
tokenRefresh: { errors, token }
|
|
371
|
+
}
|
|
372
|
+
} = res;
|
|
359
373
|
this.onAuthRefresh?.(false);
|
|
360
|
-
if (
|
|
361
|
-
console.warn("[auth-sdk] Token refresh failed:", JSON.stringify({
|
|
362
|
-
graphqlErrors: graphqlErrors ?? [],
|
|
363
|
-
refreshErrors: refreshErrors ?? [],
|
|
364
|
-
hasToken: Boolean(token),
|
|
365
|
-
httpStatus: response.status,
|
|
366
|
-
rawData: res.data ?? null
|
|
367
|
-
}));
|
|
374
|
+
if (errors?.length || graphqlErrors?.length || !token) {
|
|
368
375
|
this.tokenRefreshPromise = null;
|
|
369
376
|
this.refreshTokenStorage?.clearAuthStorage();
|
|
370
377
|
return fetch(input, requestInit);
|
|
@@ -374,32 +381,10 @@ var SaleorAuthClient = class {
|
|
|
374
381
|
this.tokenRefreshPromise = null;
|
|
375
382
|
return this.runAuthorizedRequest(input, requestInit, additionalParams);
|
|
376
383
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const origHeaders = new Headers(requestInit.headers);
|
|
382
|
-
for (const [key, value] of origHeaders.entries()) {
|
|
383
|
-
if (key !== "host" && key !== "content-type") {
|
|
384
|
-
refreshHeaders[key] = value;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
const refreshBody = JSON.stringify({
|
|
389
|
-
query: TOKEN_REFRESH.toString(),
|
|
390
|
-
variables: { refreshToken }
|
|
391
|
-
});
|
|
392
|
-
console.log("[auth-sdk] Token refresh request:", JSON.stringify({
|
|
393
|
-
url: this.saleorApiUrl,
|
|
394
|
-
headers: refreshHeaders,
|
|
395
|
-
bodyLength: refreshBody.length,
|
|
396
|
-
bodyPreview: refreshBody.slice(0, 200)
|
|
397
|
-
}));
|
|
398
|
-
this.tokenRefreshPromise = fetch(this.saleorApiUrl, {
|
|
399
|
-
method: "POST",
|
|
400
|
-
headers: refreshHeaders,
|
|
401
|
-
body: refreshBody
|
|
402
|
-
});
|
|
384
|
+
this.tokenRefreshPromise = fetch(
|
|
385
|
+
this.saleorApiUrl,
|
|
386
|
+
getRequestData(TOKEN_REFRESH, { refreshToken }, { ...this.defaultRequestInit, ...requestInit })
|
|
387
|
+
);
|
|
403
388
|
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
404
389
|
};
|
|
405
390
|
handleSignIn = async (response) => {
|
package/dist/index.mjs
CHANGED
package/dist/react/context.mjs
CHANGED
package/dist/react/index.js
CHANGED
|
@@ -177,9 +177,10 @@ var SaleorAuthProvider = ({ children, client }) => {
|
|
|
177
177
|
|
|
178
178
|
// src/react/useAuthChange.ts
|
|
179
179
|
var import_react2 = require("react");
|
|
180
|
-
var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
180
|
+
var useAuthChange = ({ saleorApiUrl, storageKeyPrefix, onSignedOut, onSignedIn }) => {
|
|
181
|
+
const keyPrefix = storageKeyPrefix ?? saleorApiUrl;
|
|
181
182
|
const handleAuthChange = (event) => {
|
|
182
|
-
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(
|
|
183
|
+
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(keyPrefix);
|
|
183
184
|
if (!isCustomAuthEvent) {
|
|
184
185
|
return;
|
|
185
186
|
}
|
|
@@ -194,9 +195,9 @@ var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
|
194
195
|
if (typeof window === "undefined") {
|
|
195
196
|
return;
|
|
196
197
|
}
|
|
197
|
-
window.addEventListener(getStorageAuthEventKey(
|
|
198
|
+
window.addEventListener(getStorageAuthEventKey(keyPrefix), handleAuthChange);
|
|
198
199
|
return () => {
|
|
199
|
-
window.removeEventListener(getStorageAuthEventKey(
|
|
200
|
+
window.removeEventListener(getStorageAuthEventKey(keyPrefix), handleAuthChange);
|
|
200
201
|
};
|
|
201
202
|
}, []);
|
|
202
203
|
};
|
package/dist/react/index.mjs
CHANGED
|
@@ -8,11 +8,11 @@ import {
|
|
|
8
8
|
} from "../chunk-NAQNA6DI.mjs";
|
|
9
9
|
import {
|
|
10
10
|
useAuthChange
|
|
11
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-FY2QDW7J.mjs";
|
|
12
12
|
import {
|
|
13
13
|
useSaleorExternalAuth
|
|
14
14
|
} from "../chunk-Q3UFWDCC.mjs";
|
|
15
|
-
import "../chunk-
|
|
15
|
+
import "../chunk-3UGMWVQN.mjs";
|
|
16
16
|
import "../chunk-B326YIV6.mjs";
|
|
17
17
|
import "../chunk-263DHBMK.mjs";
|
|
18
18
|
import "../chunk-T35JF4IS.mjs";
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
interface UseAuthChangeProps {
|
|
2
2
|
saleorApiUrl: string;
|
|
3
|
+
storageKeyPrefix?: string;
|
|
3
4
|
onSignedIn?: () => void;
|
|
4
5
|
onSignedOut?: () => void;
|
|
5
6
|
}
|
|
6
|
-
declare const useAuthChange: ({ saleorApiUrl, onSignedOut, onSignedIn }: UseAuthChangeProps) => void;
|
|
7
|
+
declare const useAuthChange: ({ saleorApiUrl, storageKeyPrefix, onSignedOut, onSignedIn }: UseAuthChangeProps) => void;
|
|
7
8
|
|
|
8
9
|
export { useAuthChange };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
interface UseAuthChangeProps {
|
|
2
2
|
saleorApiUrl: string;
|
|
3
|
+
storageKeyPrefix?: string;
|
|
3
4
|
onSignedIn?: () => void;
|
|
4
5
|
onSignedOut?: () => void;
|
|
5
6
|
}
|
|
6
|
-
declare const useAuthChange: ({ saleorApiUrl, onSignedOut, onSignedIn }: UseAuthChangeProps) => void;
|
|
7
|
+
declare const useAuthChange: ({ saleorApiUrl, storageKeyPrefix, onSignedOut, onSignedIn }: UseAuthChangeProps) => void;
|
|
7
8
|
|
|
8
9
|
export { useAuthChange };
|
|
@@ -29,9 +29,10 @@ var import_react = require("react");
|
|
|
29
29
|
var getStorageAuthEventKey = (prefix) => [prefix, "saleor_storage_auth_change"].filter(Boolean).join("+");
|
|
30
30
|
|
|
31
31
|
// src/react/useAuthChange.ts
|
|
32
|
-
var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
32
|
+
var useAuthChange = ({ saleorApiUrl, storageKeyPrefix, onSignedOut, onSignedIn }) => {
|
|
33
|
+
const keyPrefix = storageKeyPrefix ?? saleorApiUrl;
|
|
33
34
|
const handleAuthChange = (event) => {
|
|
34
|
-
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(
|
|
35
|
+
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(keyPrefix);
|
|
35
36
|
if (!isCustomAuthEvent) {
|
|
36
37
|
return;
|
|
37
38
|
}
|
|
@@ -46,9 +47,9 @@ var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
|
46
47
|
if (typeof window === "undefined") {
|
|
47
48
|
return;
|
|
48
49
|
}
|
|
49
|
-
window.addEventListener(getStorageAuthEventKey(
|
|
50
|
+
window.addEventListener(getStorageAuthEventKey(keyPrefix), handleAuthChange);
|
|
50
51
|
return () => {
|
|
51
|
-
window.removeEventListener(getStorageAuthEventKey(
|
|
52
|
+
window.removeEventListener(getStorageAuthEventKey(keyPrefix), handleAuthChange);
|
|
52
53
|
};
|
|
53
54
|
}, []);
|
|
54
55
|
};
|