@shopsbuilder/auth-sdk 1.1.0
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/README.md +469 -0
- package/dist/README.md +469 -0
- package/dist/SaleorAccessTokenStorageHandler.d.mts +13 -0
- package/dist/SaleorAccessTokenStorageHandler.d.ts +13 -0
- package/dist/SaleorAccessTokenStorageHandler.js +50 -0
- package/dist/SaleorAccessTokenStorageHandler.mjs +8 -0
- package/dist/SaleorAuthClient.d.mts +52 -0
- package/dist/SaleorAuthClient.d.ts +52 -0
- package/dist/SaleorAuthClient.js +464 -0
- package/dist/SaleorAuthClient.mjs +13 -0
- package/dist/SaleorExternalAuth.d.mts +29 -0
- package/dist/SaleorExternalAuth.d.ts +29 -0
- package/dist/SaleorExternalAuth.js +198 -0
- package/dist/SaleorExternalAuth.mjs +12 -0
- package/dist/SaleorRefreshTokenStorageHandler.d.mts +24 -0
- package/dist/SaleorRefreshTokenStorageHandler.d.ts +24 -0
- package/dist/SaleorRefreshTokenStorageHandler.js +83 -0
- package/dist/SaleorRefreshTokenStorageHandler.mjs +12 -0
- package/dist/chunk-263DHBMK.mjs +56 -0
- package/dist/chunk-74GMXOK4.mjs +13 -0
- package/dist/chunk-77CRMKFX.mjs +12 -0
- package/dist/chunk-7JTFMRQS.mjs +105 -0
- package/dist/chunk-BRRF6LN3.mjs +224 -0
- package/dist/chunk-BZFBMGPG.mjs +25 -0
- package/dist/chunk-K5MTKW5C.mjs +16 -0
- package/dist/chunk-KLIEZ4V4.mjs +10 -0
- package/dist/chunk-NAQNA6DI.mjs +20 -0
- package/dist/chunk-Q3UFWDCC.mjs +38 -0
- package/dist/chunk-T35JF4IS.mjs +59 -0
- package/dist/chunk-UDLCOX6B.mjs +49 -0
- package/dist/chunk-WJVMUY3P.mjs +33 -0
- package/dist/graphql.d.mts +11 -0
- package/dist/graphql.d.ts +11 -0
- package/dist/graphql.js +40 -0
- package/dist/graphql.mjs +6 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +524 -0
- package/dist/index.mjs +23 -0
- package/dist/mutations.d.mts +12 -0
- package/dist/mutations.d.ts +12 -0
- package/dist/mutations.js +145 -0
- package/dist/mutations.mjs +17 -0
- package/dist/next/handler.d.mts +9 -0
- package/dist/next/handler.d.ts +9 -0
- package/dist/next/handler.js +36 -0
- package/dist/next/handler.mjs +11 -0
- package/dist/next/index.d.mts +6 -0
- package/dist/next/index.d.ts +6 -0
- package/dist/next/index.js +38 -0
- package/dist/next/index.mjs +11 -0
- package/dist/next/server.d.mts +20 -0
- package/dist/next/server.d.ts +20 -0
- package/dist/next/server.js +79 -0
- package/dist/next/server.mjs +53 -0
- package/dist/react/SaleorAuthProvider.d.mts +10 -0
- package/dist/react/SaleorAuthProvider.d.ts +10 -0
- package/dist/react/SaleorAuthProvider.js +50 -0
- package/dist/react/SaleorAuthProvider.mjs +13 -0
- package/dist/react/context.d.mts +9 -0
- package/dist/react/context.d.ts +9 -0
- package/dist/react/context.js +46 -0
- package/dist/react/context.mjs +16 -0
- package/dist/react/index.d.mts +8 -0
- package/dist/react/index.d.ts +8 -0
- package/dist/react/index.js +291 -0
- package/dist/react/index.mjs +30 -0
- package/dist/react/useAuthChange.d.mts +8 -0
- package/dist/react/useAuthChange.d.ts +8 -0
- package/dist/react/useAuthChange.js +58 -0
- package/dist/react/useAuthChange.mjs +7 -0
- package/dist/react/useSaleorExternalAuth.d.mts +22 -0
- package/dist/react/useSaleorExternalAuth.d.ts +22 -0
- package/dist/react/useSaleorExternalAuth.js +227 -0
- package/dist/react/useSaleorExternalAuth.mjs +11 -0
- package/dist/types.d.mts +82 -0
- package/dist/types.d.ts +82 -0
- package/dist/types.js +34 -0
- package/dist/types.mjs +6 -0
- package/dist/utils.d.mts +28 -0
- package/dist/utils.d.ts +28 -0
- package/dist/utils.js +77 -0
- package/dist/utils.mjs +15 -0
- package/package.json +129 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/SaleorAuthClient.ts
|
|
31
|
+
var SaleorAuthClient_exports = {};
|
|
32
|
+
__export(SaleorAuthClient_exports, {
|
|
33
|
+
SaleorAuthClient: () => SaleorAuthClient,
|
|
34
|
+
createSaleorAuthClient: () => createSaleorAuthClient
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(SaleorAuthClient_exports);
|
|
37
|
+
|
|
38
|
+
// src/SaleorRefreshTokenStorageHandler.ts
|
|
39
|
+
var getStorageAuthEventKey = (prefix) => [prefix, "saleor_storage_auth_change"].filter(Boolean).join("+");
|
|
40
|
+
var getStorageAuthStateKey = (prefix) => [prefix, "saleor_auth_module_auth_state"].filter(Boolean).join("+");
|
|
41
|
+
var getRefreshTokenKey = (prefix) => [prefix, "saleor_auth_module_refresh_token"].filter(Boolean).join("+");
|
|
42
|
+
var SaleorRefreshTokenStorageHandler = class {
|
|
43
|
+
constructor(storage, prefix) {
|
|
44
|
+
this.storage = storage;
|
|
45
|
+
this.prefix = prefix;
|
|
46
|
+
if (typeof window !== "undefined") {
|
|
47
|
+
window.addEventListener("storage", this.handleStorageChange);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
handleStorageChange = (event) => {
|
|
51
|
+
const { oldValue, newValue, type, key } = event;
|
|
52
|
+
if (oldValue === newValue || type !== "storage" || key !== getStorageAuthStateKey(this.prefix)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this.sendAuthStateEvent(newValue);
|
|
56
|
+
};
|
|
57
|
+
cleanup = () => {
|
|
58
|
+
if (typeof window !== "undefined") {
|
|
59
|
+
window.removeEventListener("storage", this.handleStorageChange);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/* auth state */
|
|
63
|
+
sendAuthStateEvent = (authState) => {
|
|
64
|
+
if (typeof window !== "undefined") {
|
|
65
|
+
const event = new CustomEvent(getStorageAuthEventKey(this.prefix), {
|
|
66
|
+
detail: { authState }
|
|
67
|
+
});
|
|
68
|
+
window.dispatchEvent(event);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
getAuthState = () => this.storage.getItem(getStorageAuthStateKey(this.prefix)) || "signedOut";
|
|
72
|
+
setAuthState = (authState) => {
|
|
73
|
+
this.storage.setItem(getStorageAuthStateKey(this.prefix), authState);
|
|
74
|
+
this.sendAuthStateEvent(authState);
|
|
75
|
+
};
|
|
76
|
+
/* refresh token */
|
|
77
|
+
getRefreshToken = () => this.storage.getItem(getRefreshTokenKey(this.prefix)) || null;
|
|
78
|
+
setRefreshToken = (token) => {
|
|
79
|
+
this.storage.setItem(getRefreshTokenKey(this.prefix), token);
|
|
80
|
+
};
|
|
81
|
+
/* performed on logout */
|
|
82
|
+
clearAuthStorage = () => {
|
|
83
|
+
this.setAuthState("signedOut");
|
|
84
|
+
this.storage.removeItem(getRefreshTokenKey(this.prefix));
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/graphql.ts
|
|
89
|
+
var TypedDocumentString = class extends String {
|
|
90
|
+
constructor(value, __meta__) {
|
|
91
|
+
super(value);
|
|
92
|
+
this.value = value;
|
|
93
|
+
this.__meta__ = __meta__;
|
|
94
|
+
}
|
|
95
|
+
__apiType;
|
|
96
|
+
toString() {
|
|
97
|
+
return this.value;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// src/utils.ts
|
|
102
|
+
var MILLI_MULTIPLYER = 1e3;
|
|
103
|
+
var decodeToken = (token) => {
|
|
104
|
+
const tokenParts = token.split(".");
|
|
105
|
+
const decodedTokenData = Buffer.from(tokenParts[1] || "", "base64").toString();
|
|
106
|
+
const parsedTokenData = JSON.parse(decodedTokenData);
|
|
107
|
+
return parsedTokenData;
|
|
108
|
+
};
|
|
109
|
+
var getTokenExpiry = (token) => {
|
|
110
|
+
const parsedTokenData = decodeToken(token);
|
|
111
|
+
return parsedTokenData.exp * MILLI_MULTIPLYER || 0;
|
|
112
|
+
};
|
|
113
|
+
var getTokenIss = (token) => {
|
|
114
|
+
const parsedTokenData = decodeToken(token);
|
|
115
|
+
return parsedTokenData.iss;
|
|
116
|
+
};
|
|
117
|
+
var isExpiredToken = (token, tokenGracePeriod) => {
|
|
118
|
+
return getTokenExpiry(token) - tokenGracePeriod <= Date.now();
|
|
119
|
+
};
|
|
120
|
+
var getRequestData = (query, variables, requestInit) => ({
|
|
121
|
+
...requestInit,
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: {
|
|
124
|
+
...Object.fromEntries(new Headers(requestInit?.headers).entries()),
|
|
125
|
+
"Content-Type": "application/json"
|
|
126
|
+
},
|
|
127
|
+
body: JSON.stringify({
|
|
128
|
+
query,
|
|
129
|
+
variables
|
|
130
|
+
})
|
|
131
|
+
});
|
|
132
|
+
var InvariantError = class extends Error {
|
|
133
|
+
constructor(message) {
|
|
134
|
+
super(message);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
function invariant(condition, message) {
|
|
138
|
+
if (!condition) {
|
|
139
|
+
throw new InvariantError(`Invariant Violation: ${message || ""}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/mutations.ts
|
|
144
|
+
var accountErrorFragment = (
|
|
145
|
+
/* graphql */
|
|
146
|
+
`
|
|
147
|
+
fragment AccountErrorFragment on AccountError {
|
|
148
|
+
code
|
|
149
|
+
field
|
|
150
|
+
message
|
|
151
|
+
}
|
|
152
|
+
`
|
|
153
|
+
);
|
|
154
|
+
var TOKEN_REFRESH = new TypedDocumentString(
|
|
155
|
+
/* graphql */
|
|
156
|
+
`
|
|
157
|
+
${accountErrorFragment}
|
|
158
|
+
mutation refreshToken($refreshToken: String!) {
|
|
159
|
+
tokenRefresh(refreshToken: $refreshToken) {
|
|
160
|
+
token
|
|
161
|
+
errors {
|
|
162
|
+
...AccountErrorFragment
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
`
|
|
167
|
+
);
|
|
168
|
+
var TOKEN_CREATE = new TypedDocumentString(
|
|
169
|
+
/* graphql */
|
|
170
|
+
`
|
|
171
|
+
mutation tokenCreate($email: String!, $password: String!) {
|
|
172
|
+
tokenCreate(email: $email, password: $password) {
|
|
173
|
+
token
|
|
174
|
+
refreshToken
|
|
175
|
+
errors {
|
|
176
|
+
message
|
|
177
|
+
field
|
|
178
|
+
code
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
`
|
|
183
|
+
);
|
|
184
|
+
var PASSWORD_RESET = new TypedDocumentString(
|
|
185
|
+
/* graphql */
|
|
186
|
+
`
|
|
187
|
+
mutation passwordReset($email: String!, $password: String!, $token: String!) {
|
|
188
|
+
setPassword(email: $email, password: $password, token: $token) {
|
|
189
|
+
token
|
|
190
|
+
refreshToken
|
|
191
|
+
errors {
|
|
192
|
+
message
|
|
193
|
+
field
|
|
194
|
+
code
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
`
|
|
199
|
+
);
|
|
200
|
+
var ExternalAuthenticationURL = new TypedDocumentString(
|
|
201
|
+
/* graphql */
|
|
202
|
+
`
|
|
203
|
+
mutation externalAuthenticationUrl($pluginId: String!, $input: JSONString!) {
|
|
204
|
+
externalAuthenticationUrl(pluginId: $pluginId, input: $input) {
|
|
205
|
+
authenticationData
|
|
206
|
+
errors {
|
|
207
|
+
code
|
|
208
|
+
field
|
|
209
|
+
message
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
`
|
|
214
|
+
);
|
|
215
|
+
var ExternalObtainAccessTokens = new TypedDocumentString(
|
|
216
|
+
/* graphql */
|
|
217
|
+
`
|
|
218
|
+
mutation AuthObtainAccessToken($pluginId: String!, $input: JSONString!) {
|
|
219
|
+
externalObtainAccessTokens(pluginId: $pluginId, input: $input) {
|
|
220
|
+
token
|
|
221
|
+
refreshToken
|
|
222
|
+
user {
|
|
223
|
+
id
|
|
224
|
+
email
|
|
225
|
+
}
|
|
226
|
+
errors {
|
|
227
|
+
field
|
|
228
|
+
code
|
|
229
|
+
message
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
`
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// src/SaleorAuthClient.ts
|
|
237
|
+
var import_cookie = __toESM(require("cookie"));
|
|
238
|
+
|
|
239
|
+
// src/SaleorAccessTokenStorageHandler.ts
|
|
240
|
+
var getAccessTokenKey = (prefix) => [prefix, "saleor_auth_access_token"].filter(Boolean).join("+");
|
|
241
|
+
var SaleorAccessTokenStorageHandler = class {
|
|
242
|
+
constructor(storage, prefix) {
|
|
243
|
+
this.storage = storage;
|
|
244
|
+
this.prefix = prefix;
|
|
245
|
+
}
|
|
246
|
+
getAccessToken = () => {
|
|
247
|
+
const key = getAccessTokenKey(this.prefix);
|
|
248
|
+
return this.storage.getItem(key);
|
|
249
|
+
};
|
|
250
|
+
setAccessToken = (token) => {
|
|
251
|
+
const key = getAccessTokenKey(this.prefix);
|
|
252
|
+
return this.storage.setItem(key, token);
|
|
253
|
+
};
|
|
254
|
+
clearAuthStorage = () => {
|
|
255
|
+
const key = getAccessTokenKey(this.prefix);
|
|
256
|
+
return this.storage.removeItem(key);
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
// src/SaleorAuthClient.ts
|
|
261
|
+
var SaleorAuthClient = class {
|
|
262
|
+
// we'll assume a generous time of 2 seconds for api to
|
|
263
|
+
// process our request
|
|
264
|
+
tokenGracePeriod = 2e3;
|
|
265
|
+
tokenRefreshPromise = null;
|
|
266
|
+
onAuthRefresh;
|
|
267
|
+
saleorApiUrl;
|
|
268
|
+
/**
|
|
269
|
+
* Persistent storage (for refresh token)
|
|
270
|
+
*/
|
|
271
|
+
refreshTokenStorage;
|
|
272
|
+
/**
|
|
273
|
+
* Non-persistent storage for access token
|
|
274
|
+
*/
|
|
275
|
+
accessTokenStorage;
|
|
276
|
+
defaultRequestInit;
|
|
277
|
+
/**
|
|
278
|
+
* Use ths method to clear event listeners from storageHandler
|
|
279
|
+
* @example
|
|
280
|
+
* ```jsx
|
|
281
|
+
* useEffect(() => {
|
|
282
|
+
* return () => {
|
|
283
|
+
* SaleorAuthClient.cleanup();
|
|
284
|
+
* }
|
|
285
|
+
* }, [])
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
constructor({
|
|
289
|
+
saleorApiUrl,
|
|
290
|
+
refreshTokenStorage,
|
|
291
|
+
accessTokenStorage,
|
|
292
|
+
onAuthRefresh,
|
|
293
|
+
tokenGracePeriod,
|
|
294
|
+
defaultRequestInit
|
|
295
|
+
}) {
|
|
296
|
+
this.defaultRequestInit = defaultRequestInit;
|
|
297
|
+
if (tokenGracePeriod) {
|
|
298
|
+
this.tokenGracePeriod = tokenGracePeriod;
|
|
299
|
+
}
|
|
300
|
+
this.onAuthRefresh = onAuthRefresh;
|
|
301
|
+
this.saleorApiUrl = saleorApiUrl;
|
|
302
|
+
const refreshTokenRepo = refreshTokenStorage ?? (typeof window !== "undefined" ? window.localStorage : void 0);
|
|
303
|
+
this.refreshTokenStorage = refreshTokenRepo ? new SaleorRefreshTokenStorageHandler(refreshTokenRepo, saleorApiUrl) : null;
|
|
304
|
+
const accessTokenRepo = accessTokenStorage ?? getInMemoryAccessTokenStorage();
|
|
305
|
+
this.accessTokenStorage = new SaleorAccessTokenStorageHandler(accessTokenRepo, saleorApiUrl);
|
|
306
|
+
}
|
|
307
|
+
cleanup = () => {
|
|
308
|
+
this.refreshTokenStorage?.cleanup();
|
|
309
|
+
};
|
|
310
|
+
runAuthorizedRequest = (input, init, additionalParams) => {
|
|
311
|
+
const token = this.accessTokenStorage.getAccessToken();
|
|
312
|
+
if (!token) {
|
|
313
|
+
return fetch(input, init);
|
|
314
|
+
}
|
|
315
|
+
const headers = new Headers(init?.headers);
|
|
316
|
+
const getURL = (input2) => {
|
|
317
|
+
if (typeof input2 === "string") {
|
|
318
|
+
return input2;
|
|
319
|
+
} else if ("url" in input2) {
|
|
320
|
+
return input2.url;
|
|
321
|
+
} else {
|
|
322
|
+
return input2.href;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const iss = getTokenIss(token);
|
|
326
|
+
const issuerAndDomainMatch = getURL(input) === iss;
|
|
327
|
+
const shouldAddAuthorizationHeader = issuerAndDomainMatch || additionalParams?.allowPassingTokenToThirdPartyDomains;
|
|
328
|
+
if (!issuerAndDomainMatch) {
|
|
329
|
+
if (shouldAddAuthorizationHeader) {
|
|
330
|
+
console.warn(
|
|
331
|
+
"Token's `iss` and request URL do not match but `allowPassingTokenToThirdPartyDomains` was specified."
|
|
332
|
+
);
|
|
333
|
+
} else {
|
|
334
|
+
console.warn(
|
|
335
|
+
"Token's `iss` and request URL do not match. Not adding `Authorization` header to the request."
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (shouldAddAuthorizationHeader) {
|
|
340
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
341
|
+
}
|
|
342
|
+
return fetch(input, { ...init, headers });
|
|
343
|
+
};
|
|
344
|
+
handleRequestWithTokenRefresh = async (input, requestInit, additionalParams) => {
|
|
345
|
+
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
346
|
+
invariant(refreshToken, "Missing refresh token in token refresh handler");
|
|
347
|
+
const accessToken = this.accessTokenStorage.getAccessToken();
|
|
348
|
+
if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) {
|
|
349
|
+
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
350
|
+
}
|
|
351
|
+
this.onAuthRefresh?.(true);
|
|
352
|
+
if (this.tokenRefreshPromise) {
|
|
353
|
+
const response = await this.tokenRefreshPromise;
|
|
354
|
+
const res = await response.clone().json();
|
|
355
|
+
const {
|
|
356
|
+
errors: graphqlErrors,
|
|
357
|
+
data: {
|
|
358
|
+
tokenRefresh: { errors, token }
|
|
359
|
+
}
|
|
360
|
+
} = res;
|
|
361
|
+
this.onAuthRefresh?.(false);
|
|
362
|
+
if (errors?.length || graphqlErrors?.length || !token) {
|
|
363
|
+
this.tokenRefreshPromise = null;
|
|
364
|
+
this.refreshTokenStorage?.clearAuthStorage();
|
|
365
|
+
return fetch(input, requestInit);
|
|
366
|
+
}
|
|
367
|
+
this.refreshTokenStorage?.setAuthState("signedIn");
|
|
368
|
+
this.accessTokenStorage.setAccessToken(token);
|
|
369
|
+
this.tokenRefreshPromise = null;
|
|
370
|
+
return this.runAuthorizedRequest(input, requestInit, additionalParams);
|
|
371
|
+
}
|
|
372
|
+
this.tokenRefreshPromise = fetch(
|
|
373
|
+
this.saleorApiUrl,
|
|
374
|
+
getRequestData(TOKEN_REFRESH, { refreshToken }, { ...this.defaultRequestInit, ...requestInit })
|
|
375
|
+
);
|
|
376
|
+
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
377
|
+
};
|
|
378
|
+
handleSignIn = async (response) => {
|
|
379
|
+
const readResponse = await response.json();
|
|
380
|
+
const responseData = "tokenCreate" in readResponse.data ? readResponse.data.tokenCreate : readResponse.data.setPassword;
|
|
381
|
+
if (!responseData) {
|
|
382
|
+
return readResponse;
|
|
383
|
+
}
|
|
384
|
+
const { errors, token, refreshToken } = responseData;
|
|
385
|
+
if (!token || errors.length) {
|
|
386
|
+
this.refreshTokenStorage?.setAuthState("signedOut");
|
|
387
|
+
return readResponse;
|
|
388
|
+
}
|
|
389
|
+
if (token) {
|
|
390
|
+
this.accessTokenStorage.setAccessToken(token);
|
|
391
|
+
}
|
|
392
|
+
if (refreshToken) {
|
|
393
|
+
this.refreshTokenStorage?.setRefreshToken(refreshToken);
|
|
394
|
+
}
|
|
395
|
+
this.refreshTokenStorage?.setAuthState("signedIn");
|
|
396
|
+
return readResponse;
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* @param additionalParams
|
|
400
|
+
* @param additionalParams.allowPassingTokenToThirdPartyDomains if set to true, the `Authorization` header will be added to the request even if the token's `iss` and request URL do not match
|
|
401
|
+
*/
|
|
402
|
+
fetchWithAuth = async (input, init, additionalParams) => {
|
|
403
|
+
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
404
|
+
if (!this.accessTokenStorage.getAccessToken() && typeof document !== "undefined") {
|
|
405
|
+
const tokenFromCookie = import_cookie.default.parse(document.cookie).token ?? null;
|
|
406
|
+
if (tokenFromCookie) {
|
|
407
|
+
this.accessTokenStorage.setAccessToken(tokenFromCookie);
|
|
408
|
+
}
|
|
409
|
+
document.cookie = import_cookie.default.serialize("token", "", { expires: /* @__PURE__ */ new Date(0), path: "/" });
|
|
410
|
+
}
|
|
411
|
+
const accessToken = this.accessTokenStorage.getAccessToken();
|
|
412
|
+
if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) {
|
|
413
|
+
return this.runAuthorizedRequest(input, init, additionalParams);
|
|
414
|
+
}
|
|
415
|
+
if (refreshToken) {
|
|
416
|
+
return this.handleRequestWithTokenRefresh(input, init, additionalParams);
|
|
417
|
+
}
|
|
418
|
+
return fetch(input, init);
|
|
419
|
+
};
|
|
420
|
+
resetPassword = async (variables, requestInit) => {
|
|
421
|
+
const response = await fetch(
|
|
422
|
+
this.saleorApiUrl,
|
|
423
|
+
getRequestData(PASSWORD_RESET, variables, { ...this.defaultRequestInit, ...requestInit })
|
|
424
|
+
);
|
|
425
|
+
return this.handleSignIn(response);
|
|
426
|
+
};
|
|
427
|
+
signIn = async (variables, requestInit) => {
|
|
428
|
+
const response = await fetch(
|
|
429
|
+
this.saleorApiUrl,
|
|
430
|
+
getRequestData(TOKEN_CREATE, variables, { ...this.defaultRequestInit, ...requestInit })
|
|
431
|
+
);
|
|
432
|
+
return this.handleSignIn(response);
|
|
433
|
+
};
|
|
434
|
+
signOut = () => {
|
|
435
|
+
this.accessTokenStorage.clearAuthStorage();
|
|
436
|
+
this.refreshTokenStorage?.clearAuthStorage();
|
|
437
|
+
if (typeof document !== "undefined") {
|
|
438
|
+
document.cookie = import_cookie.default.serialize("token", "", {
|
|
439
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
440
|
+
path: "/"
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
};
|
|
445
|
+
var createSaleorAuthClient = (props) => new SaleorAuthClient(props);
|
|
446
|
+
function getInMemoryAccessTokenStorage() {
|
|
447
|
+
let accessToken = null;
|
|
448
|
+
return {
|
|
449
|
+
getItem() {
|
|
450
|
+
return accessToken;
|
|
451
|
+
},
|
|
452
|
+
removeItem() {
|
|
453
|
+
return accessToken = null;
|
|
454
|
+
},
|
|
455
|
+
setItem(_key, value) {
|
|
456
|
+
return accessToken = value;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
461
|
+
0 && (module.exports = {
|
|
462
|
+
SaleorAuthClient,
|
|
463
|
+
createSaleorAuthClient
|
|
464
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SaleorAuthClient,
|
|
3
|
+
createSaleorAuthClient
|
|
4
|
+
} from "./chunk-BRRF6LN3.mjs";
|
|
5
|
+
import "./chunk-BZFBMGPG.mjs";
|
|
6
|
+
import "./chunk-263DHBMK.mjs";
|
|
7
|
+
import "./chunk-UDLCOX6B.mjs";
|
|
8
|
+
import "./chunk-7JTFMRQS.mjs";
|
|
9
|
+
import "./chunk-K5MTKW5C.mjs";
|
|
10
|
+
export {
|
|
11
|
+
SaleorAuthClient,
|
|
12
|
+
createSaleorAuthClient
|
|
13
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ExternalProvider, ExternalObtainAccessToken } from './types.mjs';
|
|
2
|
+
import { TypedDocumentString } from './graphql.mjs';
|
|
3
|
+
import '@graphql-typed-document-node/core';
|
|
4
|
+
|
|
5
|
+
interface RedirectData {
|
|
6
|
+
code: string;
|
|
7
|
+
state: string;
|
|
8
|
+
}
|
|
9
|
+
interface GraphQLErrorResponse {
|
|
10
|
+
errors: readonly {
|
|
11
|
+
message: string;
|
|
12
|
+
}[];
|
|
13
|
+
}
|
|
14
|
+
declare class GraphQLError extends Error {
|
|
15
|
+
errorResponse: GraphQLErrorResponse;
|
|
16
|
+
constructor(errorResponse: GraphQLErrorResponse);
|
|
17
|
+
}
|
|
18
|
+
declare class SaleorExternalAuth {
|
|
19
|
+
private saleorURL;
|
|
20
|
+
private provider;
|
|
21
|
+
constructor(saleorURL: string, provider: ExternalProvider);
|
|
22
|
+
makePOSTRequest<TResult, TVariables>(query: TypedDocumentString<TResult, TVariables>, variables: TVariables): Promise<TResult>;
|
|
23
|
+
initiate({ redirectURL }: {
|
|
24
|
+
redirectURL: string;
|
|
25
|
+
}): Promise<string>;
|
|
26
|
+
obtainAccessToken({ code, state }: RedirectData): Promise<ExternalObtainAccessToken>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { GraphQLError, SaleorExternalAuth };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ExternalProvider, ExternalObtainAccessToken } from './types.js';
|
|
2
|
+
import { TypedDocumentString } from './graphql.js';
|
|
3
|
+
import '@graphql-typed-document-node/core';
|
|
4
|
+
|
|
5
|
+
interface RedirectData {
|
|
6
|
+
code: string;
|
|
7
|
+
state: string;
|
|
8
|
+
}
|
|
9
|
+
interface GraphQLErrorResponse {
|
|
10
|
+
errors: readonly {
|
|
11
|
+
message: string;
|
|
12
|
+
}[];
|
|
13
|
+
}
|
|
14
|
+
declare class GraphQLError extends Error {
|
|
15
|
+
errorResponse: GraphQLErrorResponse;
|
|
16
|
+
constructor(errorResponse: GraphQLErrorResponse);
|
|
17
|
+
}
|
|
18
|
+
declare class SaleorExternalAuth {
|
|
19
|
+
private saleorURL;
|
|
20
|
+
private provider;
|
|
21
|
+
constructor(saleorURL: string, provider: ExternalProvider);
|
|
22
|
+
makePOSTRequest<TResult, TVariables>(query: TypedDocumentString<TResult, TVariables>, variables: TVariables): Promise<TResult>;
|
|
23
|
+
initiate({ redirectURL }: {
|
|
24
|
+
redirectURL: string;
|
|
25
|
+
}): Promise<string>;
|
|
26
|
+
obtainAccessToken({ code, state }: RedirectData): Promise<ExternalObtainAccessToken>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { GraphQLError, SaleorExternalAuth };
|