@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,198 @@
|
|
|
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/SaleorExternalAuth.ts
|
|
21
|
+
var SaleorExternalAuth_exports = {};
|
|
22
|
+
__export(SaleorExternalAuth_exports, {
|
|
23
|
+
GraphQLError: () => GraphQLError,
|
|
24
|
+
SaleorExternalAuth: () => SaleorExternalAuth
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(SaleorExternalAuth_exports);
|
|
27
|
+
|
|
28
|
+
// src/graphql.ts
|
|
29
|
+
var TypedDocumentString = class extends String {
|
|
30
|
+
constructor(value, __meta__) {
|
|
31
|
+
super(value);
|
|
32
|
+
this.value = value;
|
|
33
|
+
this.__meta__ = __meta__;
|
|
34
|
+
}
|
|
35
|
+
__apiType;
|
|
36
|
+
toString() {
|
|
37
|
+
return this.value;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/mutations.ts
|
|
42
|
+
var accountErrorFragment = (
|
|
43
|
+
/* graphql */
|
|
44
|
+
`
|
|
45
|
+
fragment AccountErrorFragment on AccountError {
|
|
46
|
+
code
|
|
47
|
+
field
|
|
48
|
+
message
|
|
49
|
+
}
|
|
50
|
+
`
|
|
51
|
+
);
|
|
52
|
+
var TOKEN_REFRESH = new TypedDocumentString(
|
|
53
|
+
/* graphql */
|
|
54
|
+
`
|
|
55
|
+
${accountErrorFragment}
|
|
56
|
+
mutation refreshToken($refreshToken: String!) {
|
|
57
|
+
tokenRefresh(refreshToken: $refreshToken) {
|
|
58
|
+
token
|
|
59
|
+
errors {
|
|
60
|
+
...AccountErrorFragment
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
`
|
|
65
|
+
);
|
|
66
|
+
var TOKEN_CREATE = new TypedDocumentString(
|
|
67
|
+
/* graphql */
|
|
68
|
+
`
|
|
69
|
+
mutation tokenCreate($email: String!, $password: String!) {
|
|
70
|
+
tokenCreate(email: $email, password: $password) {
|
|
71
|
+
token
|
|
72
|
+
refreshToken
|
|
73
|
+
errors {
|
|
74
|
+
message
|
|
75
|
+
field
|
|
76
|
+
code
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
`
|
|
81
|
+
);
|
|
82
|
+
var PASSWORD_RESET = new TypedDocumentString(
|
|
83
|
+
/* graphql */
|
|
84
|
+
`
|
|
85
|
+
mutation passwordReset($email: String!, $password: String!, $token: String!) {
|
|
86
|
+
setPassword(email: $email, password: $password, token: $token) {
|
|
87
|
+
token
|
|
88
|
+
refreshToken
|
|
89
|
+
errors {
|
|
90
|
+
message
|
|
91
|
+
field
|
|
92
|
+
code
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
`
|
|
97
|
+
);
|
|
98
|
+
var ExternalAuthenticationURL = new TypedDocumentString(
|
|
99
|
+
/* graphql */
|
|
100
|
+
`
|
|
101
|
+
mutation externalAuthenticationUrl($pluginId: String!, $input: JSONString!) {
|
|
102
|
+
externalAuthenticationUrl(pluginId: $pluginId, input: $input) {
|
|
103
|
+
authenticationData
|
|
104
|
+
errors {
|
|
105
|
+
code
|
|
106
|
+
field
|
|
107
|
+
message
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
`
|
|
112
|
+
);
|
|
113
|
+
var ExternalObtainAccessTokens = new TypedDocumentString(
|
|
114
|
+
/* graphql */
|
|
115
|
+
`
|
|
116
|
+
mutation AuthObtainAccessToken($pluginId: String!, $input: JSONString!) {
|
|
117
|
+
externalObtainAccessTokens(pluginId: $pluginId, input: $input) {
|
|
118
|
+
token
|
|
119
|
+
refreshToken
|
|
120
|
+
user {
|
|
121
|
+
id
|
|
122
|
+
email
|
|
123
|
+
}
|
|
124
|
+
errors {
|
|
125
|
+
field
|
|
126
|
+
code
|
|
127
|
+
message
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
`
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
// src/utils.ts
|
|
135
|
+
var getRequestData = (query, variables, requestInit) => ({
|
|
136
|
+
...requestInit,
|
|
137
|
+
method: "POST",
|
|
138
|
+
headers: {
|
|
139
|
+
...Object.fromEntries(new Headers(requestInit?.headers).entries()),
|
|
140
|
+
"Content-Type": "application/json"
|
|
141
|
+
},
|
|
142
|
+
body: JSON.stringify({
|
|
143
|
+
query,
|
|
144
|
+
variables
|
|
145
|
+
})
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// src/SaleorExternalAuth.ts
|
|
149
|
+
var GraphQLError = class extends Error {
|
|
150
|
+
constructor(errorResponse) {
|
|
151
|
+
const message = errorResponse.errors.map((error) => error.message).join("\n");
|
|
152
|
+
super(message);
|
|
153
|
+
this.errorResponse = errorResponse;
|
|
154
|
+
this.name = this.constructor.name;
|
|
155
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
var SaleorExternalAuth = class {
|
|
159
|
+
constructor(saleorURL, provider) {
|
|
160
|
+
this.saleorURL = saleorURL;
|
|
161
|
+
this.provider = provider;
|
|
162
|
+
}
|
|
163
|
+
async makePOSTRequest(query, variables) {
|
|
164
|
+
const response = await fetch(this.saleorURL, getRequestData(query, variables));
|
|
165
|
+
const result = await response.json();
|
|
166
|
+
if ("errors" in result) {
|
|
167
|
+
console.error(result.errors);
|
|
168
|
+
throw new GraphQLError(result);
|
|
169
|
+
}
|
|
170
|
+
return result.data;
|
|
171
|
+
}
|
|
172
|
+
async initiate({ redirectURL }) {
|
|
173
|
+
const {
|
|
174
|
+
externalAuthenticationUrl: { authenticationData: data, errors }
|
|
175
|
+
} = await this.makePOSTRequest(ExternalAuthenticationURL, {
|
|
176
|
+
pluginId: this.provider,
|
|
177
|
+
input: JSON.stringify({ redirectUri: redirectURL })
|
|
178
|
+
});
|
|
179
|
+
if (errors.length > 0) {
|
|
180
|
+
console.error({ errors });
|
|
181
|
+
throw new GraphQLError({ errors });
|
|
182
|
+
}
|
|
183
|
+
const { authorizationUrl } = JSON.parse(data);
|
|
184
|
+
return authorizationUrl;
|
|
185
|
+
}
|
|
186
|
+
async obtainAccessToken({ code, state }) {
|
|
187
|
+
const { externalObtainAccessTokens: data } = await this.makePOSTRequest(ExternalObtainAccessTokens, {
|
|
188
|
+
pluginId: this.provider,
|
|
189
|
+
input: JSON.stringify({ code, state })
|
|
190
|
+
});
|
|
191
|
+
return data;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
195
|
+
0 && (module.exports = {
|
|
196
|
+
GraphQLError,
|
|
197
|
+
SaleorExternalAuth
|
|
198
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GraphQLError,
|
|
3
|
+
SaleorExternalAuth
|
|
4
|
+
} from "./chunk-T35JF4IS.mjs";
|
|
5
|
+
import "./chunk-KLIEZ4V4.mjs";
|
|
6
|
+
import "./chunk-UDLCOX6B.mjs";
|
|
7
|
+
import "./chunk-7JTFMRQS.mjs";
|
|
8
|
+
import "./chunk-K5MTKW5C.mjs";
|
|
9
|
+
export {
|
|
10
|
+
GraphQLError,
|
|
11
|
+
SaleorExternalAuth
|
|
12
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StorageRepository } from './types.mjs';
|
|
2
|
+
|
|
3
|
+
declare const getStorageAuthEventKey: (prefix?: string) => string;
|
|
4
|
+
declare const getStorageAuthStateKey: (prefix?: string) => string;
|
|
5
|
+
declare const getRefreshTokenKey: (prefix?: string) => string;
|
|
6
|
+
type AuthState = "signedIn" | "signedOut";
|
|
7
|
+
type SaleorAuthEvent = CustomEvent<{
|
|
8
|
+
authState: AuthState;
|
|
9
|
+
}>;
|
|
10
|
+
declare class SaleorRefreshTokenStorageHandler {
|
|
11
|
+
private storage;
|
|
12
|
+
private prefix?;
|
|
13
|
+
constructor(storage: StorageRepository, prefix?: string | undefined);
|
|
14
|
+
private handleStorageChange;
|
|
15
|
+
cleanup: () => void;
|
|
16
|
+
sendAuthStateEvent: (authState: AuthState) => void;
|
|
17
|
+
getAuthState: () => AuthState;
|
|
18
|
+
setAuthState: (authState: AuthState) => void;
|
|
19
|
+
getRefreshToken: () => string | null;
|
|
20
|
+
setRefreshToken: (token: string) => void;
|
|
21
|
+
clearAuthStorage: () => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { type AuthState, type SaleorAuthEvent, SaleorRefreshTokenStorageHandler, getRefreshTokenKey, getStorageAuthEventKey, getStorageAuthStateKey };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StorageRepository } from './types.js';
|
|
2
|
+
|
|
3
|
+
declare const getStorageAuthEventKey: (prefix?: string) => string;
|
|
4
|
+
declare const getStorageAuthStateKey: (prefix?: string) => string;
|
|
5
|
+
declare const getRefreshTokenKey: (prefix?: string) => string;
|
|
6
|
+
type AuthState = "signedIn" | "signedOut";
|
|
7
|
+
type SaleorAuthEvent = CustomEvent<{
|
|
8
|
+
authState: AuthState;
|
|
9
|
+
}>;
|
|
10
|
+
declare class SaleorRefreshTokenStorageHandler {
|
|
11
|
+
private storage;
|
|
12
|
+
private prefix?;
|
|
13
|
+
constructor(storage: StorageRepository, prefix?: string | undefined);
|
|
14
|
+
private handleStorageChange;
|
|
15
|
+
cleanup: () => void;
|
|
16
|
+
sendAuthStateEvent: (authState: AuthState) => void;
|
|
17
|
+
getAuthState: () => AuthState;
|
|
18
|
+
setAuthState: (authState: AuthState) => void;
|
|
19
|
+
getRefreshToken: () => string | null;
|
|
20
|
+
setRefreshToken: (token: string) => void;
|
|
21
|
+
clearAuthStorage: () => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { type AuthState, type SaleorAuthEvent, SaleorRefreshTokenStorageHandler, getRefreshTokenKey, getStorageAuthEventKey, getStorageAuthStateKey };
|
|
@@ -0,0 +1,83 @@
|
|
|
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/SaleorRefreshTokenStorageHandler.ts
|
|
21
|
+
var SaleorRefreshTokenStorageHandler_exports = {};
|
|
22
|
+
__export(SaleorRefreshTokenStorageHandler_exports, {
|
|
23
|
+
SaleorRefreshTokenStorageHandler: () => SaleorRefreshTokenStorageHandler,
|
|
24
|
+
getRefreshTokenKey: () => getRefreshTokenKey,
|
|
25
|
+
getStorageAuthEventKey: () => getStorageAuthEventKey,
|
|
26
|
+
getStorageAuthStateKey: () => getStorageAuthStateKey
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(SaleorRefreshTokenStorageHandler_exports);
|
|
29
|
+
var getStorageAuthEventKey = (prefix) => [prefix, "saleor_storage_auth_change"].filter(Boolean).join("+");
|
|
30
|
+
var getStorageAuthStateKey = (prefix) => [prefix, "saleor_auth_module_auth_state"].filter(Boolean).join("+");
|
|
31
|
+
var getRefreshTokenKey = (prefix) => [prefix, "saleor_auth_module_refresh_token"].filter(Boolean).join("+");
|
|
32
|
+
var SaleorRefreshTokenStorageHandler = class {
|
|
33
|
+
constructor(storage, prefix) {
|
|
34
|
+
this.storage = storage;
|
|
35
|
+
this.prefix = prefix;
|
|
36
|
+
if (typeof window !== "undefined") {
|
|
37
|
+
window.addEventListener("storage", this.handleStorageChange);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
handleStorageChange = (event) => {
|
|
41
|
+
const { oldValue, newValue, type, key } = event;
|
|
42
|
+
if (oldValue === newValue || type !== "storage" || key !== getStorageAuthStateKey(this.prefix)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.sendAuthStateEvent(newValue);
|
|
46
|
+
};
|
|
47
|
+
cleanup = () => {
|
|
48
|
+
if (typeof window !== "undefined") {
|
|
49
|
+
window.removeEventListener("storage", this.handleStorageChange);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
/* auth state */
|
|
53
|
+
sendAuthStateEvent = (authState) => {
|
|
54
|
+
if (typeof window !== "undefined") {
|
|
55
|
+
const event = new CustomEvent(getStorageAuthEventKey(this.prefix), {
|
|
56
|
+
detail: { authState }
|
|
57
|
+
});
|
|
58
|
+
window.dispatchEvent(event);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
getAuthState = () => this.storage.getItem(getStorageAuthStateKey(this.prefix)) || "signedOut";
|
|
62
|
+
setAuthState = (authState) => {
|
|
63
|
+
this.storage.setItem(getStorageAuthStateKey(this.prefix), authState);
|
|
64
|
+
this.sendAuthStateEvent(authState);
|
|
65
|
+
};
|
|
66
|
+
/* refresh token */
|
|
67
|
+
getRefreshToken = () => this.storage.getItem(getRefreshTokenKey(this.prefix)) || null;
|
|
68
|
+
setRefreshToken = (token) => {
|
|
69
|
+
this.storage.setItem(getRefreshTokenKey(this.prefix), token);
|
|
70
|
+
};
|
|
71
|
+
/* performed on logout */
|
|
72
|
+
clearAuthStorage = () => {
|
|
73
|
+
this.setAuthState("signedOut");
|
|
74
|
+
this.storage.removeItem(getRefreshTokenKey(this.prefix));
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
78
|
+
0 && (module.exports = {
|
|
79
|
+
SaleorRefreshTokenStorageHandler,
|
|
80
|
+
getRefreshTokenKey,
|
|
81
|
+
getStorageAuthEventKey,
|
|
82
|
+
getStorageAuthStateKey
|
|
83
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SaleorRefreshTokenStorageHandler,
|
|
3
|
+
getRefreshTokenKey,
|
|
4
|
+
getStorageAuthEventKey,
|
|
5
|
+
getStorageAuthStateKey
|
|
6
|
+
} from "./chunk-263DHBMK.mjs";
|
|
7
|
+
export {
|
|
8
|
+
SaleorRefreshTokenStorageHandler,
|
|
9
|
+
getRefreshTokenKey,
|
|
10
|
+
getStorageAuthEventKey,
|
|
11
|
+
getStorageAuthStateKey
|
|
12
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/SaleorRefreshTokenStorageHandler.ts
|
|
2
|
+
var getStorageAuthEventKey = (prefix) => [prefix, "saleor_storage_auth_change"].filter(Boolean).join("+");
|
|
3
|
+
var getStorageAuthStateKey = (prefix) => [prefix, "saleor_auth_module_auth_state"].filter(Boolean).join("+");
|
|
4
|
+
var getRefreshTokenKey = (prefix) => [prefix, "saleor_auth_module_refresh_token"].filter(Boolean).join("+");
|
|
5
|
+
var SaleorRefreshTokenStorageHandler = class {
|
|
6
|
+
constructor(storage, prefix) {
|
|
7
|
+
this.storage = storage;
|
|
8
|
+
this.prefix = prefix;
|
|
9
|
+
if (typeof window !== "undefined") {
|
|
10
|
+
window.addEventListener("storage", this.handleStorageChange);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
handleStorageChange = (event) => {
|
|
14
|
+
const { oldValue, newValue, type, key } = event;
|
|
15
|
+
if (oldValue === newValue || type !== "storage" || key !== getStorageAuthStateKey(this.prefix)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.sendAuthStateEvent(newValue);
|
|
19
|
+
};
|
|
20
|
+
cleanup = () => {
|
|
21
|
+
if (typeof window !== "undefined") {
|
|
22
|
+
window.removeEventListener("storage", this.handleStorageChange);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/* auth state */
|
|
26
|
+
sendAuthStateEvent = (authState) => {
|
|
27
|
+
if (typeof window !== "undefined") {
|
|
28
|
+
const event = new CustomEvent(getStorageAuthEventKey(this.prefix), {
|
|
29
|
+
detail: { authState }
|
|
30
|
+
});
|
|
31
|
+
window.dispatchEvent(event);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
getAuthState = () => this.storage.getItem(getStorageAuthStateKey(this.prefix)) || "signedOut";
|
|
35
|
+
setAuthState = (authState) => {
|
|
36
|
+
this.storage.setItem(getStorageAuthStateKey(this.prefix), authState);
|
|
37
|
+
this.sendAuthStateEvent(authState);
|
|
38
|
+
};
|
|
39
|
+
/* refresh token */
|
|
40
|
+
getRefreshToken = () => this.storage.getItem(getRefreshTokenKey(this.prefix)) || null;
|
|
41
|
+
setRefreshToken = (token) => {
|
|
42
|
+
this.storage.setItem(getRefreshTokenKey(this.prefix), token);
|
|
43
|
+
};
|
|
44
|
+
/* performed on logout */
|
|
45
|
+
clearAuthStorage = () => {
|
|
46
|
+
this.setAuthState("signedOut");
|
|
47
|
+
this.storage.removeItem(getRefreshTokenKey(this.prefix));
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export {
|
|
52
|
+
getStorageAuthEventKey,
|
|
53
|
+
getStorageAuthStateKey,
|
|
54
|
+
getRefreshTokenKey,
|
|
55
|
+
SaleorRefreshTokenStorageHandler
|
|
56
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Provider
|
|
3
|
+
} from "./chunk-NAQNA6DI.mjs";
|
|
4
|
+
|
|
5
|
+
// src/react/SaleorAuthProvider.tsx
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
var SaleorAuthProvider = ({ children, client }) => {
|
|
8
|
+
return /* @__PURE__ */ jsx(Provider, { value: client, children });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
SaleorAuthProvider
|
|
13
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/next/handler.ts
|
|
2
|
+
import { serialize } from "cookie";
|
|
3
|
+
var createSaleorExternalAuthHandler = (auth) => async (req, res) => {
|
|
4
|
+
const { state, code } = req.query;
|
|
5
|
+
const { token } = await auth.obtainAccessToken({ state, code });
|
|
6
|
+
res.setHeader("Set-Cookie", serialize("token", token, { path: "/" }));
|
|
7
|
+
res.redirect("/");
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
createSaleorExternalAuthHandler
|
|
12
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TypedDocumentString
|
|
3
|
+
} from "./chunk-K5MTKW5C.mjs";
|
|
4
|
+
|
|
5
|
+
// src/mutations.ts
|
|
6
|
+
var accountErrorFragment = (
|
|
7
|
+
/* graphql */
|
|
8
|
+
`
|
|
9
|
+
fragment AccountErrorFragment on AccountError {
|
|
10
|
+
code
|
|
11
|
+
field
|
|
12
|
+
message
|
|
13
|
+
}
|
|
14
|
+
`
|
|
15
|
+
);
|
|
16
|
+
var TOKEN_REFRESH = new TypedDocumentString(
|
|
17
|
+
/* graphql */
|
|
18
|
+
`
|
|
19
|
+
${accountErrorFragment}
|
|
20
|
+
mutation refreshToken($refreshToken: String!) {
|
|
21
|
+
tokenRefresh(refreshToken: $refreshToken) {
|
|
22
|
+
token
|
|
23
|
+
errors {
|
|
24
|
+
...AccountErrorFragment
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
`
|
|
29
|
+
);
|
|
30
|
+
var TOKEN_CREATE = new TypedDocumentString(
|
|
31
|
+
/* graphql */
|
|
32
|
+
`
|
|
33
|
+
mutation tokenCreate($email: String!, $password: String!) {
|
|
34
|
+
tokenCreate(email: $email, password: $password) {
|
|
35
|
+
token
|
|
36
|
+
refreshToken
|
|
37
|
+
errors {
|
|
38
|
+
message
|
|
39
|
+
field
|
|
40
|
+
code
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
`
|
|
45
|
+
);
|
|
46
|
+
var PASSWORD_RESET = new TypedDocumentString(
|
|
47
|
+
/* graphql */
|
|
48
|
+
`
|
|
49
|
+
mutation passwordReset($email: String!, $password: String!, $token: String!) {
|
|
50
|
+
setPassword(email: $email, password: $password, token: $token) {
|
|
51
|
+
token
|
|
52
|
+
refreshToken
|
|
53
|
+
errors {
|
|
54
|
+
message
|
|
55
|
+
field
|
|
56
|
+
code
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
`
|
|
61
|
+
);
|
|
62
|
+
var ExternalAuthenticationURL = new TypedDocumentString(
|
|
63
|
+
/* graphql */
|
|
64
|
+
`
|
|
65
|
+
mutation externalAuthenticationUrl($pluginId: String!, $input: JSONString!) {
|
|
66
|
+
externalAuthenticationUrl(pluginId: $pluginId, input: $input) {
|
|
67
|
+
authenticationData
|
|
68
|
+
errors {
|
|
69
|
+
code
|
|
70
|
+
field
|
|
71
|
+
message
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
`
|
|
76
|
+
);
|
|
77
|
+
var ExternalObtainAccessTokens = new TypedDocumentString(
|
|
78
|
+
/* graphql */
|
|
79
|
+
`
|
|
80
|
+
mutation AuthObtainAccessToken($pluginId: String!, $input: JSONString!) {
|
|
81
|
+
externalObtainAccessTokens(pluginId: $pluginId, input: $input) {
|
|
82
|
+
token
|
|
83
|
+
refreshToken
|
|
84
|
+
user {
|
|
85
|
+
id
|
|
86
|
+
email
|
|
87
|
+
}
|
|
88
|
+
errors {
|
|
89
|
+
field
|
|
90
|
+
code
|
|
91
|
+
message
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
`
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
export {
|
|
99
|
+
accountErrorFragment,
|
|
100
|
+
TOKEN_REFRESH,
|
|
101
|
+
TOKEN_CREATE,
|
|
102
|
+
PASSWORD_RESET,
|
|
103
|
+
ExternalAuthenticationURL,
|
|
104
|
+
ExternalObtainAccessTokens
|
|
105
|
+
};
|