@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,224 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SaleorAccessTokenStorageHandler
|
|
3
|
+
} from "./chunk-BZFBMGPG.mjs";
|
|
4
|
+
import {
|
|
5
|
+
SaleorRefreshTokenStorageHandler
|
|
6
|
+
} from "./chunk-263DHBMK.mjs";
|
|
7
|
+
import {
|
|
8
|
+
getRequestData,
|
|
9
|
+
getTokenIss,
|
|
10
|
+
invariant,
|
|
11
|
+
isExpiredToken
|
|
12
|
+
} from "./chunk-UDLCOX6B.mjs";
|
|
13
|
+
import {
|
|
14
|
+
PASSWORD_RESET,
|
|
15
|
+
TOKEN_CREATE,
|
|
16
|
+
TOKEN_REFRESH
|
|
17
|
+
} from "./chunk-7JTFMRQS.mjs";
|
|
18
|
+
|
|
19
|
+
// src/SaleorAuthClient.ts
|
|
20
|
+
import cookie from "cookie";
|
|
21
|
+
var SaleorAuthClient = class {
|
|
22
|
+
// we'll assume a generous time of 2 seconds for api to
|
|
23
|
+
// process our request
|
|
24
|
+
tokenGracePeriod = 2e3;
|
|
25
|
+
tokenRefreshPromise = null;
|
|
26
|
+
onAuthRefresh;
|
|
27
|
+
saleorApiUrl;
|
|
28
|
+
/**
|
|
29
|
+
* Persistent storage (for refresh token)
|
|
30
|
+
*/
|
|
31
|
+
refreshTokenStorage;
|
|
32
|
+
/**
|
|
33
|
+
* Non-persistent storage for access token
|
|
34
|
+
*/
|
|
35
|
+
accessTokenStorage;
|
|
36
|
+
defaultRequestInit;
|
|
37
|
+
/**
|
|
38
|
+
* Use ths method to clear event listeners from storageHandler
|
|
39
|
+
* @example
|
|
40
|
+
* ```jsx
|
|
41
|
+
* useEffect(() => {
|
|
42
|
+
* return () => {
|
|
43
|
+
* SaleorAuthClient.cleanup();
|
|
44
|
+
* }
|
|
45
|
+
* }, [])
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
constructor({
|
|
49
|
+
saleorApiUrl,
|
|
50
|
+
refreshTokenStorage,
|
|
51
|
+
accessTokenStorage,
|
|
52
|
+
onAuthRefresh,
|
|
53
|
+
tokenGracePeriod,
|
|
54
|
+
defaultRequestInit
|
|
55
|
+
}) {
|
|
56
|
+
this.defaultRequestInit = defaultRequestInit;
|
|
57
|
+
if (tokenGracePeriod) {
|
|
58
|
+
this.tokenGracePeriod = tokenGracePeriod;
|
|
59
|
+
}
|
|
60
|
+
this.onAuthRefresh = onAuthRefresh;
|
|
61
|
+
this.saleorApiUrl = saleorApiUrl;
|
|
62
|
+
const refreshTokenRepo = refreshTokenStorage ?? (typeof window !== "undefined" ? window.localStorage : void 0);
|
|
63
|
+
this.refreshTokenStorage = refreshTokenRepo ? new SaleorRefreshTokenStorageHandler(refreshTokenRepo, saleorApiUrl) : null;
|
|
64
|
+
const accessTokenRepo = accessTokenStorage ?? getInMemoryAccessTokenStorage();
|
|
65
|
+
this.accessTokenStorage = new SaleorAccessTokenStorageHandler(accessTokenRepo, saleorApiUrl);
|
|
66
|
+
}
|
|
67
|
+
cleanup = () => {
|
|
68
|
+
this.refreshTokenStorage?.cleanup();
|
|
69
|
+
};
|
|
70
|
+
runAuthorizedRequest = (input, init, additionalParams) => {
|
|
71
|
+
const token = this.accessTokenStorage.getAccessToken();
|
|
72
|
+
if (!token) {
|
|
73
|
+
return fetch(input, init);
|
|
74
|
+
}
|
|
75
|
+
const headers = new Headers(init?.headers);
|
|
76
|
+
const getURL = (input2) => {
|
|
77
|
+
if (typeof input2 === "string") {
|
|
78
|
+
return input2;
|
|
79
|
+
} else if ("url" in input2) {
|
|
80
|
+
return input2.url;
|
|
81
|
+
} else {
|
|
82
|
+
return input2.href;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const iss = getTokenIss(token);
|
|
86
|
+
const issuerAndDomainMatch = getURL(input) === iss;
|
|
87
|
+
const shouldAddAuthorizationHeader = issuerAndDomainMatch || additionalParams?.allowPassingTokenToThirdPartyDomains;
|
|
88
|
+
if (!issuerAndDomainMatch) {
|
|
89
|
+
if (shouldAddAuthorizationHeader) {
|
|
90
|
+
console.warn(
|
|
91
|
+
"Token's `iss` and request URL do not match but `allowPassingTokenToThirdPartyDomains` was specified."
|
|
92
|
+
);
|
|
93
|
+
} else {
|
|
94
|
+
console.warn(
|
|
95
|
+
"Token's `iss` and request URL do not match. Not adding `Authorization` header to the request."
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (shouldAddAuthorizationHeader) {
|
|
100
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
101
|
+
}
|
|
102
|
+
return fetch(input, { ...init, headers });
|
|
103
|
+
};
|
|
104
|
+
handleRequestWithTokenRefresh = async (input, requestInit, additionalParams) => {
|
|
105
|
+
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
106
|
+
invariant(refreshToken, "Missing refresh token in token refresh handler");
|
|
107
|
+
const accessToken = this.accessTokenStorage.getAccessToken();
|
|
108
|
+
if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) {
|
|
109
|
+
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
110
|
+
}
|
|
111
|
+
this.onAuthRefresh?.(true);
|
|
112
|
+
if (this.tokenRefreshPromise) {
|
|
113
|
+
const response = await this.tokenRefreshPromise;
|
|
114
|
+
const res = await response.clone().json();
|
|
115
|
+
const {
|
|
116
|
+
errors: graphqlErrors,
|
|
117
|
+
data: {
|
|
118
|
+
tokenRefresh: { errors, token }
|
|
119
|
+
}
|
|
120
|
+
} = res;
|
|
121
|
+
this.onAuthRefresh?.(false);
|
|
122
|
+
if (errors?.length || graphqlErrors?.length || !token) {
|
|
123
|
+
this.tokenRefreshPromise = null;
|
|
124
|
+
this.refreshTokenStorage?.clearAuthStorage();
|
|
125
|
+
return fetch(input, requestInit);
|
|
126
|
+
}
|
|
127
|
+
this.refreshTokenStorage?.setAuthState("signedIn");
|
|
128
|
+
this.accessTokenStorage.setAccessToken(token);
|
|
129
|
+
this.tokenRefreshPromise = null;
|
|
130
|
+
return this.runAuthorizedRequest(input, requestInit, additionalParams);
|
|
131
|
+
}
|
|
132
|
+
this.tokenRefreshPromise = fetch(
|
|
133
|
+
this.saleorApiUrl,
|
|
134
|
+
getRequestData(TOKEN_REFRESH, { refreshToken }, { ...this.defaultRequestInit, ...requestInit })
|
|
135
|
+
);
|
|
136
|
+
return this.fetchWithAuth(input, requestInit, additionalParams);
|
|
137
|
+
};
|
|
138
|
+
handleSignIn = async (response) => {
|
|
139
|
+
const readResponse = await response.json();
|
|
140
|
+
const responseData = "tokenCreate" in readResponse.data ? readResponse.data.tokenCreate : readResponse.data.setPassword;
|
|
141
|
+
if (!responseData) {
|
|
142
|
+
return readResponse;
|
|
143
|
+
}
|
|
144
|
+
const { errors, token, refreshToken } = responseData;
|
|
145
|
+
if (!token || errors.length) {
|
|
146
|
+
this.refreshTokenStorage?.setAuthState("signedOut");
|
|
147
|
+
return readResponse;
|
|
148
|
+
}
|
|
149
|
+
if (token) {
|
|
150
|
+
this.accessTokenStorage.setAccessToken(token);
|
|
151
|
+
}
|
|
152
|
+
if (refreshToken) {
|
|
153
|
+
this.refreshTokenStorage?.setRefreshToken(refreshToken);
|
|
154
|
+
}
|
|
155
|
+
this.refreshTokenStorage?.setAuthState("signedIn");
|
|
156
|
+
return readResponse;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* @param additionalParams
|
|
160
|
+
* @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
|
|
161
|
+
*/
|
|
162
|
+
fetchWithAuth = async (input, init, additionalParams) => {
|
|
163
|
+
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
|
|
164
|
+
if (!this.accessTokenStorage.getAccessToken() && typeof document !== "undefined") {
|
|
165
|
+
const tokenFromCookie = cookie.parse(document.cookie).token ?? null;
|
|
166
|
+
if (tokenFromCookie) {
|
|
167
|
+
this.accessTokenStorage.setAccessToken(tokenFromCookie);
|
|
168
|
+
}
|
|
169
|
+
document.cookie = cookie.serialize("token", "", { expires: /* @__PURE__ */ new Date(0), path: "/" });
|
|
170
|
+
}
|
|
171
|
+
const accessToken = this.accessTokenStorage.getAccessToken();
|
|
172
|
+
if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) {
|
|
173
|
+
return this.runAuthorizedRequest(input, init, additionalParams);
|
|
174
|
+
}
|
|
175
|
+
if (refreshToken) {
|
|
176
|
+
return this.handleRequestWithTokenRefresh(input, init, additionalParams);
|
|
177
|
+
}
|
|
178
|
+
return fetch(input, init);
|
|
179
|
+
};
|
|
180
|
+
resetPassword = async (variables, requestInit) => {
|
|
181
|
+
const response = await fetch(
|
|
182
|
+
this.saleorApiUrl,
|
|
183
|
+
getRequestData(PASSWORD_RESET, variables, { ...this.defaultRequestInit, ...requestInit })
|
|
184
|
+
);
|
|
185
|
+
return this.handleSignIn(response);
|
|
186
|
+
};
|
|
187
|
+
signIn = async (variables, requestInit) => {
|
|
188
|
+
const response = await fetch(
|
|
189
|
+
this.saleorApiUrl,
|
|
190
|
+
getRequestData(TOKEN_CREATE, variables, { ...this.defaultRequestInit, ...requestInit })
|
|
191
|
+
);
|
|
192
|
+
return this.handleSignIn(response);
|
|
193
|
+
};
|
|
194
|
+
signOut = () => {
|
|
195
|
+
this.accessTokenStorage.clearAuthStorage();
|
|
196
|
+
this.refreshTokenStorage?.clearAuthStorage();
|
|
197
|
+
if (typeof document !== "undefined") {
|
|
198
|
+
document.cookie = cookie.serialize("token", "", {
|
|
199
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
200
|
+
path: "/"
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
var createSaleorAuthClient = (props) => new SaleorAuthClient(props);
|
|
206
|
+
function getInMemoryAccessTokenStorage() {
|
|
207
|
+
let accessToken = null;
|
|
208
|
+
return {
|
|
209
|
+
getItem() {
|
|
210
|
+
return accessToken;
|
|
211
|
+
},
|
|
212
|
+
removeItem() {
|
|
213
|
+
return accessToken = null;
|
|
214
|
+
},
|
|
215
|
+
setItem(_key, value) {
|
|
216
|
+
return accessToken = value;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export {
|
|
222
|
+
SaleorAuthClient,
|
|
223
|
+
createSaleorAuthClient
|
|
224
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/SaleorAccessTokenStorageHandler.ts
|
|
2
|
+
var getAccessTokenKey = (prefix) => [prefix, "saleor_auth_access_token"].filter(Boolean).join("+");
|
|
3
|
+
var SaleorAccessTokenStorageHandler = class {
|
|
4
|
+
constructor(storage, prefix) {
|
|
5
|
+
this.storage = storage;
|
|
6
|
+
this.prefix = prefix;
|
|
7
|
+
}
|
|
8
|
+
getAccessToken = () => {
|
|
9
|
+
const key = getAccessTokenKey(this.prefix);
|
|
10
|
+
return this.storage.getItem(key);
|
|
11
|
+
};
|
|
12
|
+
setAccessToken = (token) => {
|
|
13
|
+
const key = getAccessTokenKey(this.prefix);
|
|
14
|
+
return this.storage.setItem(key, token);
|
|
15
|
+
};
|
|
16
|
+
clearAuthStorage = () => {
|
|
17
|
+
const key = getAccessTokenKey(this.prefix);
|
|
18
|
+
return this.storage.removeItem(key);
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
getAccessTokenKey,
|
|
24
|
+
SaleorAccessTokenStorageHandler
|
|
25
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/graphql.ts
|
|
2
|
+
var TypedDocumentString = class extends String {
|
|
3
|
+
constructor(value, __meta__) {
|
|
4
|
+
super(value);
|
|
5
|
+
this.value = value;
|
|
6
|
+
this.__meta__ = __meta__;
|
|
7
|
+
}
|
|
8
|
+
__apiType;
|
|
9
|
+
toString() {
|
|
10
|
+
return this.value;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
TypedDocumentString
|
|
16
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var ExternalProvider = /* @__PURE__ */ ((ExternalProvider2) => {
|
|
3
|
+
ExternalProvider2["OpenIDConnect"] = "mirumee.authentication.openidconnect";
|
|
4
|
+
ExternalProvider2["SaleorCloud"] = "cloud_auth.CloudAuthorizationPlugin";
|
|
5
|
+
return ExternalProvider2;
|
|
6
|
+
})(ExternalProvider || {});
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
ExternalProvider
|
|
10
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/react/context.ts
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
var createSafeContext = () => {
|
|
4
|
+
const context = createContext(void 0);
|
|
5
|
+
function useSafeContext() {
|
|
6
|
+
const value = useContext(context);
|
|
7
|
+
if (value === void 0) {
|
|
8
|
+
throw new Error("useContext must be inside a Provider with a value");
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
return [useSafeContext, context.Provider];
|
|
13
|
+
};
|
|
14
|
+
var [useSaleorAuthContext, Provider] = createSafeContext();
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
createSafeContext,
|
|
18
|
+
useSaleorAuthContext,
|
|
19
|
+
Provider
|
|
20
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SaleorExternalAuth
|
|
3
|
+
} from "./chunk-T35JF4IS.mjs";
|
|
4
|
+
|
|
5
|
+
// src/react/useSaleorExternalAuth.ts
|
|
6
|
+
import { useState, useEffect } from "react";
|
|
7
|
+
var useSaleorExternalAuth = ({
|
|
8
|
+
saleorURL,
|
|
9
|
+
provider,
|
|
10
|
+
redirectURL
|
|
11
|
+
}) => {
|
|
12
|
+
const [state, setState] = useState({
|
|
13
|
+
authURL: void 0,
|
|
14
|
+
error: void 0,
|
|
15
|
+
loading: true
|
|
16
|
+
});
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const triggerExternalAuth = async () => {
|
|
19
|
+
try {
|
|
20
|
+
const auth = new SaleorExternalAuth(saleorURL, provider);
|
|
21
|
+
const result = await auth.initiate({ redirectURL });
|
|
22
|
+
setState({ authURL: result, loading: false });
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (error instanceof Error) {
|
|
25
|
+
setState({ loading: false, error: error.message });
|
|
26
|
+
} else {
|
|
27
|
+
setState({ loading: false, error: "Unknown error" });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
void triggerExternalAuth();
|
|
32
|
+
}, [saleorURL]);
|
|
33
|
+
return state;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
useSaleorExternalAuth
|
|
38
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getRequestData
|
|
3
|
+
} from "./chunk-UDLCOX6B.mjs";
|
|
4
|
+
import {
|
|
5
|
+
ExternalAuthenticationURL,
|
|
6
|
+
ExternalObtainAccessTokens
|
|
7
|
+
} from "./chunk-7JTFMRQS.mjs";
|
|
8
|
+
|
|
9
|
+
// src/SaleorExternalAuth.ts
|
|
10
|
+
var GraphQLError = class extends Error {
|
|
11
|
+
constructor(errorResponse) {
|
|
12
|
+
const message = errorResponse.errors.map((error) => error.message).join("\n");
|
|
13
|
+
super(message);
|
|
14
|
+
this.errorResponse = errorResponse;
|
|
15
|
+
this.name = this.constructor.name;
|
|
16
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var SaleorExternalAuth = class {
|
|
20
|
+
constructor(saleorURL, provider) {
|
|
21
|
+
this.saleorURL = saleorURL;
|
|
22
|
+
this.provider = provider;
|
|
23
|
+
}
|
|
24
|
+
async makePOSTRequest(query, variables) {
|
|
25
|
+
const response = await fetch(this.saleorURL, getRequestData(query, variables));
|
|
26
|
+
const result = await response.json();
|
|
27
|
+
if ("errors" in result) {
|
|
28
|
+
console.error(result.errors);
|
|
29
|
+
throw new GraphQLError(result);
|
|
30
|
+
}
|
|
31
|
+
return result.data;
|
|
32
|
+
}
|
|
33
|
+
async initiate({ redirectURL }) {
|
|
34
|
+
const {
|
|
35
|
+
externalAuthenticationUrl: { authenticationData: data, errors }
|
|
36
|
+
} = await this.makePOSTRequest(ExternalAuthenticationURL, {
|
|
37
|
+
pluginId: this.provider,
|
|
38
|
+
input: JSON.stringify({ redirectUri: redirectURL })
|
|
39
|
+
});
|
|
40
|
+
if (errors.length > 0) {
|
|
41
|
+
console.error({ errors });
|
|
42
|
+
throw new GraphQLError({ errors });
|
|
43
|
+
}
|
|
44
|
+
const { authorizationUrl } = JSON.parse(data);
|
|
45
|
+
return authorizationUrl;
|
|
46
|
+
}
|
|
47
|
+
async obtainAccessToken({ code, state }) {
|
|
48
|
+
const { externalObtainAccessTokens: data } = await this.makePOSTRequest(ExternalObtainAccessTokens, {
|
|
49
|
+
pluginId: this.provider,
|
|
50
|
+
input: JSON.stringify({ code, state })
|
|
51
|
+
});
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
GraphQLError,
|
|
58
|
+
SaleorExternalAuth
|
|
59
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
var MILLI_MULTIPLYER = 1e3;
|
|
3
|
+
var decodeToken = (token) => {
|
|
4
|
+
const tokenParts = token.split(".");
|
|
5
|
+
const decodedTokenData = Buffer.from(tokenParts[1] || "", "base64").toString();
|
|
6
|
+
const parsedTokenData = JSON.parse(decodedTokenData);
|
|
7
|
+
return parsedTokenData;
|
|
8
|
+
};
|
|
9
|
+
var getTokenExpiry = (token) => {
|
|
10
|
+
const parsedTokenData = decodeToken(token);
|
|
11
|
+
return parsedTokenData.exp * MILLI_MULTIPLYER || 0;
|
|
12
|
+
};
|
|
13
|
+
var getTokenIss = (token) => {
|
|
14
|
+
const parsedTokenData = decodeToken(token);
|
|
15
|
+
return parsedTokenData.iss;
|
|
16
|
+
};
|
|
17
|
+
var isExpiredToken = (token, tokenGracePeriod) => {
|
|
18
|
+
return getTokenExpiry(token) - tokenGracePeriod <= Date.now();
|
|
19
|
+
};
|
|
20
|
+
var getRequestData = (query, variables, requestInit) => ({
|
|
21
|
+
...requestInit,
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: {
|
|
24
|
+
...Object.fromEntries(new Headers(requestInit?.headers).entries()),
|
|
25
|
+
"Content-Type": "application/json"
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify({
|
|
28
|
+
query,
|
|
29
|
+
variables
|
|
30
|
+
})
|
|
31
|
+
});
|
|
32
|
+
var InvariantError = class extends Error {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super(message);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
function invariant(condition, message) {
|
|
38
|
+
if (!condition) {
|
|
39
|
+
throw new InvariantError(`Invariant Violation: ${message || ""}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
getTokenIss,
|
|
45
|
+
isExpiredToken,
|
|
46
|
+
getRequestData,
|
|
47
|
+
InvariantError,
|
|
48
|
+
invariant
|
|
49
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getStorageAuthEventKey
|
|
3
|
+
} from "./chunk-263DHBMK.mjs";
|
|
4
|
+
|
|
5
|
+
// src/react/useAuthChange.ts
|
|
6
|
+
import { useEffect } from "react";
|
|
7
|
+
var useAuthChange = ({ saleorApiUrl, onSignedOut, onSignedIn }) => {
|
|
8
|
+
const handleAuthChange = (event) => {
|
|
9
|
+
const isCustomAuthEvent = event?.type === getStorageAuthEventKey(saleorApiUrl);
|
|
10
|
+
if (!isCustomAuthEvent) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const { authState } = event.detail;
|
|
14
|
+
if (authState === "signedIn") {
|
|
15
|
+
onSignedIn?.();
|
|
16
|
+
} else if (authState === "signedOut") {
|
|
17
|
+
onSignedOut?.();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (typeof window === "undefined") {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
window.addEventListener(getStorageAuthEventKey(saleorApiUrl), handleAuthChange);
|
|
25
|
+
return () => {
|
|
26
|
+
window.removeEventListener(getStorageAuthEventKey(saleorApiUrl), handleAuthChange);
|
|
27
|
+
};
|
|
28
|
+
}, []);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
useAuthChange
|
|
33
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DocumentTypeDecoration } from '@graphql-typed-document-node/core';
|
|
2
|
+
|
|
3
|
+
declare class TypedDocumentString<TResult, TVariables> extends String implements DocumentTypeDecoration<TResult, TVariables> {
|
|
4
|
+
private value;
|
|
5
|
+
__meta__?: Record<string, any> | undefined;
|
|
6
|
+
__apiType?: DocumentTypeDecoration<TResult, TVariables>["__apiType"];
|
|
7
|
+
constructor(value: string, __meta__?: Record<string, any> | undefined);
|
|
8
|
+
toString(): string & DocumentTypeDecoration<TResult, TVariables>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { TypedDocumentString };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DocumentTypeDecoration } from '@graphql-typed-document-node/core';
|
|
2
|
+
|
|
3
|
+
declare class TypedDocumentString<TResult, TVariables> extends String implements DocumentTypeDecoration<TResult, TVariables> {
|
|
4
|
+
private value;
|
|
5
|
+
__meta__?: Record<string, any> | undefined;
|
|
6
|
+
__apiType?: DocumentTypeDecoration<TResult, TVariables>["__apiType"];
|
|
7
|
+
constructor(value: string, __meta__?: Record<string, any> | undefined);
|
|
8
|
+
toString(): string & DocumentTypeDecoration<TResult, TVariables>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { TypedDocumentString };
|
package/dist/graphql.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/graphql.ts
|
|
21
|
+
var graphql_exports = {};
|
|
22
|
+
__export(graphql_exports, {
|
|
23
|
+
TypedDocumentString: () => TypedDocumentString
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(graphql_exports);
|
|
26
|
+
var TypedDocumentString = class extends String {
|
|
27
|
+
constructor(value, __meta__) {
|
|
28
|
+
super(value);
|
|
29
|
+
this.value = value;
|
|
30
|
+
this.__meta__ = __meta__;
|
|
31
|
+
}
|
|
32
|
+
__apiType;
|
|
33
|
+
toString() {
|
|
34
|
+
return this.value;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
TypedDocumentString
|
|
40
|
+
});
|
package/dist/graphql.mjs
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SaleorAuthClient, SaleorAuthClientProps, createSaleorAuthClient } from './SaleorAuthClient.mjs';
|
|
2
|
+
export { GraphQLError, SaleorExternalAuth } from './SaleorExternalAuth.mjs';
|
|
3
|
+
export { ExternalAuthenticationURLResponse, ExternalAuthenticationURLVariables, ExternalObtainAccessToken, ExternalObtainAccessTokenResponse, ExternalObtainAccessTokenVariables, ExternalProvider, Fetch, FetchRequestInfo, FetchRequestInit, FetchWithAdditionalParams, PasswordResetResponse, PasswordResetVariables, StorageRepository, TokenCreateResponse, TokenCreateVariables, TokenRefreshResponse, TokenRefreshVariables } from './types.mjs';
|
|
4
|
+
import './graphql.mjs';
|
|
5
|
+
import '@graphql-typed-document-node/core';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SaleorAuthClient, SaleorAuthClientProps, createSaleorAuthClient } from './SaleorAuthClient.js';
|
|
2
|
+
export { GraphQLError, SaleorExternalAuth } from './SaleorExternalAuth.js';
|
|
3
|
+
export { ExternalAuthenticationURLResponse, ExternalAuthenticationURLVariables, ExternalObtainAccessToken, ExternalObtainAccessTokenResponse, ExternalObtainAccessTokenVariables, ExternalProvider, Fetch, FetchRequestInfo, FetchRequestInit, FetchWithAdditionalParams, PasswordResetResponse, PasswordResetVariables, StorageRepository, TokenCreateResponse, TokenCreateVariables, TokenRefreshResponse, TokenRefreshVariables } from './types.js';
|
|
4
|
+
import './graphql.js';
|
|
5
|
+
import '@graphql-typed-document-node/core';
|