@newgameplusinc/odyssey-sso 1.0.2 → 2.0.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/dist/config.d.ts +27 -0
- package/dist/config.js +31 -0
- package/dist/{lib/database.d.ts → database.d.ts} +1 -1
- package/dist/{lib/database.js → database.js} +4 -2
- package/dist/main.d.ts +249 -0
- package/dist/main.js +948 -0
- package/dist/types.d.ts +466 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +1 -0
- package/package.json +7 -10
- package/README.md +0 -3
- package/dist/config/index.d.ts +0 -7
- package/dist/config/index.js +0 -7
- package/dist/index.d.ts +0 -27
- package/dist/index.js +0 -158
- package/dist/lib/axios.d.ts +0 -6
- package/dist/lib/axios.js +0 -20
package/dist/main.js
ADDED
|
@@ -0,0 +1,948 @@
|
|
|
1
|
+
import * as c from "./config";
|
|
2
|
+
import { isBrowser } from "./utils";
|
|
3
|
+
export default class SSO {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
this.config = config;
|
|
6
|
+
this.db = c.db;
|
|
7
|
+
this.ssoClientUrl = c.ssoClientUrl;
|
|
8
|
+
this.ssoServerUrl = c.ssoServerUrl;
|
|
9
|
+
this.dbId = "local-user";
|
|
10
|
+
this.cb = () => { };
|
|
11
|
+
this.login = async (redirectUrl) => {
|
|
12
|
+
if (!isBrowser()) {
|
|
13
|
+
if (this.config.debug)
|
|
14
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const r = `${this.ssoClientUrl}/sso?sdkKey=${this.config.sdkKey}&redirectUri=${redirectUrl}`;
|
|
18
|
+
window.location.href = r;
|
|
19
|
+
};
|
|
20
|
+
this.exchange = async () => {
|
|
21
|
+
if (!isBrowser()) {
|
|
22
|
+
if (this.config.debug)
|
|
23
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const url = new URL(window.location.href);
|
|
27
|
+
const token = url.searchParams.get("token");
|
|
28
|
+
if (!token) {
|
|
29
|
+
if (this.config.debug)
|
|
30
|
+
console.log(`SSO-SDK: No token found in URL.`);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
url.searchParams.delete("token");
|
|
34
|
+
window.history.pushState({}, "", url.href);
|
|
35
|
+
const res = await fetch(`${this.ssoServerUrl}/sso/exchange`, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
token,
|
|
39
|
+
sdkKey: this.config.sdkKey,
|
|
40
|
+
}),
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
if (this.config.debug)
|
|
47
|
+
console.log(`SSO-SDK: Exchange failed.`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const data = await res.json();
|
|
51
|
+
const profileRes = await fetch(`${this.ssoServerUrl}/users/profile`, {
|
|
52
|
+
headers: {
|
|
53
|
+
"Content-Type": "application/json",
|
|
54
|
+
Authorization: `Bearer ${data.data.tokens.accessToken}`,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
if (!profileRes.ok) {
|
|
58
|
+
if (this.config.debug)
|
|
59
|
+
console.log(`SSO-SDK: Profile fetch failed.`);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const profileData = await profileRes.json();
|
|
63
|
+
await this.db.open();
|
|
64
|
+
await this.db.delete(this.dbId);
|
|
65
|
+
await this.db.create({
|
|
66
|
+
...profileData.data,
|
|
67
|
+
userId: profileData.data.id,
|
|
68
|
+
tokens: data.data.tokens,
|
|
69
|
+
id: this.dbId,
|
|
70
|
+
});
|
|
71
|
+
this.cb({
|
|
72
|
+
type: c.actions.login,
|
|
73
|
+
payload: profileData.data,
|
|
74
|
+
});
|
|
75
|
+
return { ...profileData.data, tokens: data.data.tokens };
|
|
76
|
+
};
|
|
77
|
+
this.fetchProfile = async () => {
|
|
78
|
+
if (!isBrowser()) {
|
|
79
|
+
if (this.config.debug)
|
|
80
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
await this.db.open();
|
|
84
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
85
|
+
if (!dbUser) {
|
|
86
|
+
if (this.config.debug)
|
|
87
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const user = await this.withRefresh(async (aT) => {
|
|
91
|
+
return await fetch(`${this.ssoServerUrl}/users/profile`, {
|
|
92
|
+
headers: {
|
|
93
|
+
"Content-Type": "application/json",
|
|
94
|
+
Authorization: `Bearer ${aT || dbUser?.tokens.accessToken}`,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
if (!user || !user.ok) {
|
|
99
|
+
if (this.config.debug)
|
|
100
|
+
console.log(`SSO-SDK: Profile fetch failed.`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const { data } = (await user.json());
|
|
104
|
+
this.cb({
|
|
105
|
+
type: c.actions.profile_fetch,
|
|
106
|
+
payload: data,
|
|
107
|
+
});
|
|
108
|
+
return data;
|
|
109
|
+
};
|
|
110
|
+
this.refresh = async () => {
|
|
111
|
+
if (!isBrowser()) {
|
|
112
|
+
if (this.config.debug)
|
|
113
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
await this.db.open();
|
|
117
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
118
|
+
if (!dbUser) {
|
|
119
|
+
if (this.config.debug)
|
|
120
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const user = await fetch(`${this.ssoServerUrl}/users/refresh`, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
body: JSON.stringify({
|
|
126
|
+
token: dbUser.tokens.refreshToken,
|
|
127
|
+
}),
|
|
128
|
+
headers: {
|
|
129
|
+
"Content-Type": "application/json",
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
if (!user.ok) {
|
|
133
|
+
if (this.config.debug)
|
|
134
|
+
console.log(`SSO-SDK: Refresh failed.`);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const { data } = (await user.json());
|
|
138
|
+
await this.db.update(this.dbId, {
|
|
139
|
+
...dbUser,
|
|
140
|
+
tokens: {
|
|
141
|
+
accessToken: data.accessToken,
|
|
142
|
+
refreshToken: data.refreshToken,
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
this.cb({
|
|
146
|
+
type: c.actions.refresh,
|
|
147
|
+
payload: null,
|
|
148
|
+
});
|
|
149
|
+
return data;
|
|
150
|
+
};
|
|
151
|
+
this.logout = async () => {
|
|
152
|
+
if (!isBrowser()) {
|
|
153
|
+
if (this.config.debug)
|
|
154
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
await this.db.open();
|
|
158
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
159
|
+
if (!dbUser) {
|
|
160
|
+
if (this.config.debug)
|
|
161
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const user = await this.withRefresh(async (aT) => {
|
|
165
|
+
return await fetch(`${this.ssoServerUrl}/users/logout`, {
|
|
166
|
+
method: "DELETE",
|
|
167
|
+
headers: {
|
|
168
|
+
"Content-Type": "application/json",
|
|
169
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
if (!user || !user.ok) {
|
|
174
|
+
if (this.config.debug)
|
|
175
|
+
console.log(`SSO-SDK: Logout failed.`);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
await this.db.delete(this.dbId);
|
|
179
|
+
this.cb({
|
|
180
|
+
type: c.actions.logout,
|
|
181
|
+
payload: null,
|
|
182
|
+
});
|
|
183
|
+
return true;
|
|
184
|
+
};
|
|
185
|
+
this.onEvents = (cb) => {
|
|
186
|
+
this.cb = cb;
|
|
187
|
+
};
|
|
188
|
+
this.withRefresh = async (api) => {
|
|
189
|
+
const r = await api();
|
|
190
|
+
if (r.status !== 401)
|
|
191
|
+
return r;
|
|
192
|
+
// refresh token
|
|
193
|
+
if (!isBrowser()) {
|
|
194
|
+
if (this.config.debug)
|
|
195
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
await this.db.open();
|
|
199
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
200
|
+
if (!dbUser) {
|
|
201
|
+
if (this.config.debug)
|
|
202
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const user = await fetch(`${this.ssoServerUrl}/users/refresh`, {
|
|
206
|
+
method: "POST",
|
|
207
|
+
body: JSON.stringify({
|
|
208
|
+
token: dbUser.tokens.refreshToken,
|
|
209
|
+
}),
|
|
210
|
+
headers: {
|
|
211
|
+
"Content-Type": "application/json",
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
if (!user.ok) {
|
|
215
|
+
if (this.config.debug)
|
|
216
|
+
console.log(`SSO-SDK: Refresh failed.`);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const { data } = (await user.json());
|
|
220
|
+
await this.db.update(this.dbId, {
|
|
221
|
+
...dbUser,
|
|
222
|
+
tokens: {
|
|
223
|
+
accessToken: data.accessToken,
|
|
224
|
+
refreshToken: data.refreshToken,
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
this.cb({
|
|
228
|
+
type: c.actions.refresh,
|
|
229
|
+
payload: null,
|
|
230
|
+
});
|
|
231
|
+
const nR = await api(data.accessToken);
|
|
232
|
+
return nR;
|
|
233
|
+
};
|
|
234
|
+
this.profileUpdate = async (payload) => {
|
|
235
|
+
if (!isBrowser()) {
|
|
236
|
+
if (this.config.debug)
|
|
237
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
await this.db.open();
|
|
241
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
242
|
+
if (!dbUser) {
|
|
243
|
+
if (this.config.debug)
|
|
244
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const user = await this.withRefresh(async (aT) => {
|
|
248
|
+
return await fetch(`${this.ssoServerUrl}/users/profile`, {
|
|
249
|
+
method: "PATCH",
|
|
250
|
+
body: JSON.stringify(payload),
|
|
251
|
+
headers: {
|
|
252
|
+
"Content-Type": "application/json",
|
|
253
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
254
|
+
"x-sdk-key": this.config.sdkKey,
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
if (!user || !user.ok) {
|
|
259
|
+
if (this.config.debug)
|
|
260
|
+
console.log(`SSO-SDK: Profile update failed.`);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const { data } = (await user.json());
|
|
264
|
+
const { id, ...rest } = data;
|
|
265
|
+
await this.db.update(this.dbId, {
|
|
266
|
+
...rest,
|
|
267
|
+
userId: id,
|
|
268
|
+
});
|
|
269
|
+
this.cb({
|
|
270
|
+
type: c.actions.profile_update,
|
|
271
|
+
payload: data,
|
|
272
|
+
});
|
|
273
|
+
return data;
|
|
274
|
+
};
|
|
275
|
+
this.avatarUpdate = async (file) => {
|
|
276
|
+
if (!isBrowser()) {
|
|
277
|
+
if (this.config.debug)
|
|
278
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const photoURL = await this.fileUpload(file);
|
|
282
|
+
if (!photoURL) {
|
|
283
|
+
if (this.config.debug)
|
|
284
|
+
console.log(`SSO-SDK: File upload failed.`);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
await this.db.open();
|
|
288
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
289
|
+
if (!dbUser) {
|
|
290
|
+
if (this.config.debug)
|
|
291
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const user = await this.withRefresh(async (aT) => {
|
|
295
|
+
return await fetch(`${this.ssoServerUrl}/users/profile/avatar`, {
|
|
296
|
+
method: "PUT",
|
|
297
|
+
body: JSON.stringify({
|
|
298
|
+
photoURL,
|
|
299
|
+
}),
|
|
300
|
+
headers: {
|
|
301
|
+
"Content-Type": "application/json",
|
|
302
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
303
|
+
"x-sdk-key": this.config.sdkKey,
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
if (!user || !user.ok) {
|
|
308
|
+
if (this.config.debug)
|
|
309
|
+
console.log(`SSO-SDK: Profile update failed.`);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
const { data } = (await user.json());
|
|
313
|
+
const { id, ...rest } = data;
|
|
314
|
+
await this.db.update(this.dbId, {
|
|
315
|
+
...rest,
|
|
316
|
+
userId: id,
|
|
317
|
+
});
|
|
318
|
+
this.cb({
|
|
319
|
+
type: c.actions.profile_photo_update,
|
|
320
|
+
payload: data,
|
|
321
|
+
});
|
|
322
|
+
return data;
|
|
323
|
+
};
|
|
324
|
+
this.fileUpload = async (file) => {
|
|
325
|
+
if (!isBrowser()) {
|
|
326
|
+
if (this.config.debug)
|
|
327
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
await this.db.open();
|
|
331
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
332
|
+
if (!dbUser) {
|
|
333
|
+
if (this.config.debug)
|
|
334
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const { type } = file;
|
|
338
|
+
const url = await this.withRefresh(async (aT) => {
|
|
339
|
+
return await fetch(`${this.ssoServerUrl}/uploads/url?fileType=${type}`, {
|
|
340
|
+
headers: {
|
|
341
|
+
"Content-Type": "application/json",
|
|
342
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
343
|
+
"x-sdk-key": this.config.sdkKey,
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
if (!url || !url.ok) {
|
|
348
|
+
if (this.config.debug)
|
|
349
|
+
console.log(`SSO-SDK: could not get upload url.`);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const { data } = (await url.json());
|
|
353
|
+
const upload = await fetch(data.url, {
|
|
354
|
+
method: data.method,
|
|
355
|
+
body: file,
|
|
356
|
+
headers: data.headers,
|
|
357
|
+
});
|
|
358
|
+
if (!upload || !upload.ok) {
|
|
359
|
+
if (this.config.debug)
|
|
360
|
+
console.log(`SSO-SDK: could not upload file.`);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (!data.url) {
|
|
364
|
+
if (this.config.debug)
|
|
365
|
+
console.log(`SSO-SDK: could not get upload url.`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
return data.url;
|
|
369
|
+
};
|
|
370
|
+
this.clothAdd = async (payload) => {
|
|
371
|
+
if (!isBrowser()) {
|
|
372
|
+
if (this.config.debug)
|
|
373
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const thumb = await this.fileUpload(payload.thumb);
|
|
377
|
+
if (!thumb) {
|
|
378
|
+
if (this.config.debug)
|
|
379
|
+
console.log(`SSO-SDK: File upload failed.`);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const jsonPayload = {
|
|
383
|
+
...payload,
|
|
384
|
+
thumb,
|
|
385
|
+
};
|
|
386
|
+
await this.db.open();
|
|
387
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
388
|
+
if (!dbUser) {
|
|
389
|
+
if (this.config.debug)
|
|
390
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const user = await this.withRefresh(async (aT) => {
|
|
394
|
+
return await fetch(`${this.ssoServerUrl}/clothings`, {
|
|
395
|
+
method: "POST",
|
|
396
|
+
body: JSON.stringify(jsonPayload),
|
|
397
|
+
headers: {
|
|
398
|
+
"Content-Type": "application/json",
|
|
399
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
400
|
+
"x-sdk-key": this.config.sdkKey,
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
if (!user || !user.ok) {
|
|
405
|
+
if (this.config.debug)
|
|
406
|
+
console.log(`SSO-SDK: Profile update failed.`);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const { data } = (await user.json());
|
|
410
|
+
this.cb({
|
|
411
|
+
type: c.actions.cloth_add,
|
|
412
|
+
payload: data,
|
|
413
|
+
});
|
|
414
|
+
return data;
|
|
415
|
+
};
|
|
416
|
+
this.clothGetAll = async (options) => {
|
|
417
|
+
if (!isBrowser()) {
|
|
418
|
+
if (this.config.debug)
|
|
419
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
await this.db.open();
|
|
423
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
424
|
+
if (!dbUser) {
|
|
425
|
+
if (this.config.debug)
|
|
426
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const queryParams = new URLSearchParams({
|
|
430
|
+
skip: Math.ceil(options?.skip ?? 0).toString(),
|
|
431
|
+
limit: Math.ceil(options?.limit ?? 10).toString(),
|
|
432
|
+
sort: options?.sort ?? "asc",
|
|
433
|
+
});
|
|
434
|
+
const user = await this.withRefresh(async (aT) => {
|
|
435
|
+
return await fetch(`${this.ssoServerUrl}/clothings?${queryParams.toString()}`, {
|
|
436
|
+
headers: {
|
|
437
|
+
"Content-Type": "application/json",
|
|
438
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
439
|
+
"x-sdk-key": this.config.sdkKey,
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
if (!user || !user.ok) {
|
|
444
|
+
if (this.config.debug)
|
|
445
|
+
console.log(`SSO-SDK: Get all cloths failed.`);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const { data } = (await user.json());
|
|
449
|
+
this.cb({
|
|
450
|
+
type: c.actions.cloth_fetch_all,
|
|
451
|
+
payload: data,
|
|
452
|
+
});
|
|
453
|
+
return data;
|
|
454
|
+
};
|
|
455
|
+
this.clothGetById = async (clothId) => {
|
|
456
|
+
if (!isBrowser()) {
|
|
457
|
+
if (this.config.debug)
|
|
458
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
await this.db.open();
|
|
462
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
463
|
+
if (!dbUser) {
|
|
464
|
+
if (this.config.debug)
|
|
465
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
const user = await this.withRefresh(async (aT) => {
|
|
469
|
+
return await fetch(`${this.ssoServerUrl}/clothings/${clothId}`, {
|
|
470
|
+
headers: {
|
|
471
|
+
"Content-Type": "application/json",
|
|
472
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
473
|
+
"x-sdk-key": this.config.sdkKey,
|
|
474
|
+
},
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
if (!user || !user.ok) {
|
|
478
|
+
if (this.config.debug)
|
|
479
|
+
console.log(`SSO-SDK: Get cloth by id failed.`);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const { data } = (await user.json());
|
|
483
|
+
this.cb({
|
|
484
|
+
type: c.actions.cloth_fetch,
|
|
485
|
+
payload: data,
|
|
486
|
+
});
|
|
487
|
+
return data;
|
|
488
|
+
};
|
|
489
|
+
this.clothUpdate = async (clothId, payload) => {
|
|
490
|
+
if (!isBrowser()) {
|
|
491
|
+
if (this.config.debug)
|
|
492
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
await this.db.open();
|
|
496
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
497
|
+
if (!dbUser) {
|
|
498
|
+
if (this.config.debug)
|
|
499
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const user = await this.withRefresh(async (aT) => {
|
|
503
|
+
return await fetch(`${this.ssoServerUrl}/clothings/${clothId}`, {
|
|
504
|
+
method: "PATCH",
|
|
505
|
+
body: JSON.stringify(payload),
|
|
506
|
+
headers: {
|
|
507
|
+
"Content-Type": "application/json",
|
|
508
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
509
|
+
"x-sdk-key": this.config.sdkKey,
|
|
510
|
+
},
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
if (!user || !user.ok) {
|
|
514
|
+
if (this.config.debug)
|
|
515
|
+
console.log(`SSO-SDK: Cloth update failed.`);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const { data } = (await user.json());
|
|
519
|
+
this.cb({
|
|
520
|
+
type: c.actions.cloth_update,
|
|
521
|
+
payload: data,
|
|
522
|
+
});
|
|
523
|
+
return data;
|
|
524
|
+
};
|
|
525
|
+
this.clothDelete = async (clothId) => {
|
|
526
|
+
if (!isBrowser()) {
|
|
527
|
+
if (this.config.debug)
|
|
528
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
await this.db.open();
|
|
532
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
533
|
+
if (!dbUser) {
|
|
534
|
+
if (this.config.debug)
|
|
535
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const user = await this.withRefresh(async (aT) => {
|
|
539
|
+
return await fetch(`${this.ssoServerUrl}/clothings/${clothId}`, {
|
|
540
|
+
method: "DELETE",
|
|
541
|
+
headers: {
|
|
542
|
+
"Content-Type": "application/json",
|
|
543
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
544
|
+
"x-sdk-key": this.config.sdkKey,
|
|
545
|
+
},
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
if (!user || !user.ok) {
|
|
549
|
+
if (this.config.debug)
|
|
550
|
+
console.log(`SSO-SDK: Cloth delete failed.`);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
await user.json();
|
|
554
|
+
this.cb({
|
|
555
|
+
type: c.actions.cloth_delete,
|
|
556
|
+
payload: {
|
|
557
|
+
id: clothId,
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
return clothId;
|
|
561
|
+
};
|
|
562
|
+
// materials
|
|
563
|
+
this.materialAdd = async (payload) => {
|
|
564
|
+
if (!isBrowser()) {
|
|
565
|
+
if (this.config.debug)
|
|
566
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
const thumb = await this.fileUpload(payload.thumb);
|
|
570
|
+
if (!thumb) {
|
|
571
|
+
if (this.config.debug)
|
|
572
|
+
console.log(`SSO-SDK: Material add failed.`);
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
const jsonPayload = {
|
|
576
|
+
...payload,
|
|
577
|
+
thumb: thumb,
|
|
578
|
+
};
|
|
579
|
+
await this.db.open();
|
|
580
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
581
|
+
if (!dbUser) {
|
|
582
|
+
if (this.config.debug)
|
|
583
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const user = await this.withRefresh(async (aT) => {
|
|
587
|
+
return await fetch(`${this.ssoServerUrl}/clothings/materials`, {
|
|
588
|
+
method: "POST",
|
|
589
|
+
body: JSON.stringify(jsonPayload),
|
|
590
|
+
headers: {
|
|
591
|
+
"Content-Type": "application/json",
|
|
592
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
593
|
+
"x-sdk-key": this.config.sdkKey,
|
|
594
|
+
},
|
|
595
|
+
});
|
|
596
|
+
});
|
|
597
|
+
if (!user || !user.ok) {
|
|
598
|
+
if (this.config.debug)
|
|
599
|
+
console.log(`SSO-SDK: Material add failed.`);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
const { data } = (await user.json());
|
|
603
|
+
this.cb({
|
|
604
|
+
type: c.actions.material_add,
|
|
605
|
+
payload: data,
|
|
606
|
+
});
|
|
607
|
+
return data;
|
|
608
|
+
};
|
|
609
|
+
this.materialGetAll = async (options) => {
|
|
610
|
+
if (!isBrowser()) {
|
|
611
|
+
if (this.config.debug)
|
|
612
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
await this.db.open();
|
|
616
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
617
|
+
if (!dbUser) {
|
|
618
|
+
if (this.config.debug)
|
|
619
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const params = new URLSearchParams({
|
|
623
|
+
skip: options?.skip?.toString() || "0",
|
|
624
|
+
limit: options?.limit?.toString() || "10",
|
|
625
|
+
sort: options?.sort || "asc",
|
|
626
|
+
});
|
|
627
|
+
const user = await this.withRefresh(async (aT) => {
|
|
628
|
+
return await fetch(`${this.ssoServerUrl}/clothings/materials?${params}`, {
|
|
629
|
+
method: "GET",
|
|
630
|
+
headers: {
|
|
631
|
+
"Content-Type": "application/json",
|
|
632
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
633
|
+
"x-sdk-key": this.config.sdkKey,
|
|
634
|
+
},
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
if (!user || !user.ok) {
|
|
638
|
+
if (this.config.debug)
|
|
639
|
+
console.log(`SSO-SDK: Materials fetch failed.`);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
const { data } = (await user.json());
|
|
643
|
+
this.cb({
|
|
644
|
+
type: c.actions.material_fetch_all,
|
|
645
|
+
payload: data,
|
|
646
|
+
});
|
|
647
|
+
return data;
|
|
648
|
+
};
|
|
649
|
+
this.materialGetById = async (materialId) => {
|
|
650
|
+
if (!isBrowser()) {
|
|
651
|
+
if (this.config.debug)
|
|
652
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
await this.db.open();
|
|
656
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
657
|
+
if (!dbUser) {
|
|
658
|
+
if (this.config.debug)
|
|
659
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
const user = await this.withRefresh(async (aT) => {
|
|
663
|
+
return await fetch(`${this.ssoServerUrl}/clothings/materials/${materialId}`, {
|
|
664
|
+
method: "GET",
|
|
665
|
+
headers: {
|
|
666
|
+
"Content-Type": "application/json",
|
|
667
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
668
|
+
"x-sdk-key": this.config.sdkKey,
|
|
669
|
+
},
|
|
670
|
+
});
|
|
671
|
+
});
|
|
672
|
+
if (!user || !user.ok) {
|
|
673
|
+
if (this.config.debug)
|
|
674
|
+
console.log(`SSO-SDK: Material fetch failed.`);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const { data } = (await user.json());
|
|
678
|
+
this.cb({
|
|
679
|
+
type: c.actions.material_fetch,
|
|
680
|
+
payload: data,
|
|
681
|
+
});
|
|
682
|
+
return data;
|
|
683
|
+
};
|
|
684
|
+
this.materialUpdate = async (materialId, payload) => {
|
|
685
|
+
if (!isBrowser()) {
|
|
686
|
+
if (this.config.debug)
|
|
687
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
await this.db.open();
|
|
691
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
692
|
+
if (!dbUser) {
|
|
693
|
+
if (this.config.debug)
|
|
694
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
const user = await this.withRefresh(async (aT) => {
|
|
698
|
+
return await fetch(`${this.ssoServerUrl}/clothings/materials/${materialId}`, {
|
|
699
|
+
method: "PATCH",
|
|
700
|
+
body: JSON.stringify(payload),
|
|
701
|
+
headers: {
|
|
702
|
+
"Content-Type": "application/json",
|
|
703
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
704
|
+
"x-sdk-key": this.config.sdkKey,
|
|
705
|
+
},
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
if (!user || !user.ok) {
|
|
709
|
+
if (this.config.debug)
|
|
710
|
+
console.log(`SSO-SDK: Material update failed.`);
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
const { data } = (await user.json());
|
|
714
|
+
this.cb({
|
|
715
|
+
type: c.actions.material_update,
|
|
716
|
+
payload: data,
|
|
717
|
+
});
|
|
718
|
+
return data;
|
|
719
|
+
};
|
|
720
|
+
this.materialDelete = async (materialId) => {
|
|
721
|
+
if (!isBrowser()) {
|
|
722
|
+
if (this.config.debug)
|
|
723
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
await this.db.open();
|
|
727
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
728
|
+
if (!dbUser) {
|
|
729
|
+
if (this.config.debug)
|
|
730
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
const user = await this.withRefresh(async (aT) => {
|
|
734
|
+
return await fetch(`${this.ssoServerUrl}/clothings/materials/${materialId}`, {
|
|
735
|
+
method: "DELETE",
|
|
736
|
+
headers: {
|
|
737
|
+
"Content-Type": "application/json",
|
|
738
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
739
|
+
"x-sdk-key": this.config.sdkKey,
|
|
740
|
+
},
|
|
741
|
+
});
|
|
742
|
+
});
|
|
743
|
+
if (!user || !user.ok) {
|
|
744
|
+
if (this.config.debug)
|
|
745
|
+
console.log(`SSO-SDK: Material delete failed.`);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
this.cb({
|
|
749
|
+
type: c.actions.material_delete,
|
|
750
|
+
payload: { id: materialId },
|
|
751
|
+
});
|
|
752
|
+
return materialId;
|
|
753
|
+
};
|
|
754
|
+
// skulls
|
|
755
|
+
this.skullAdd = async (payload) => {
|
|
756
|
+
if (!isBrowser()) {
|
|
757
|
+
if (this.config.debug)
|
|
758
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const preview = await this.fileUpload(payload.filePreview);
|
|
762
|
+
const model = await this.fileUpload(payload.file3d);
|
|
763
|
+
if (!preview || !model) {
|
|
764
|
+
if (this.config.debug)
|
|
765
|
+
console.log(`SSO-SDK: File upload failed.`);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
const jsonPayload = {
|
|
769
|
+
...payload,
|
|
770
|
+
previewUrl: preview,
|
|
771
|
+
url3d: model,
|
|
772
|
+
};
|
|
773
|
+
await this.db.open();
|
|
774
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
775
|
+
if (!dbUser) {
|
|
776
|
+
if (this.config.debug)
|
|
777
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
const user = await this.withRefresh(async (aT) => {
|
|
781
|
+
return await fetch(`${this.ssoServerUrl}/skulls`, {
|
|
782
|
+
method: "POST",
|
|
783
|
+
body: JSON.stringify(jsonPayload),
|
|
784
|
+
headers: {
|
|
785
|
+
"Content-Type": "application/json",
|
|
786
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
787
|
+
"x-sdk-key": this.config.sdkKey,
|
|
788
|
+
},
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
if (!user || !user.ok) {
|
|
792
|
+
if (this.config.debug)
|
|
793
|
+
console.log(`SSO-SDK: Skull add failed.`);
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
const { data } = (await user.json());
|
|
797
|
+
this.cb({
|
|
798
|
+
type: c.actions.skull_add,
|
|
799
|
+
payload: data,
|
|
800
|
+
});
|
|
801
|
+
return data;
|
|
802
|
+
};
|
|
803
|
+
this.skullUpdate = async (skullId, payload) => {
|
|
804
|
+
if (!isBrowser()) {
|
|
805
|
+
if (this.config.debug)
|
|
806
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
await this.db.open();
|
|
810
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
811
|
+
if (!dbUser) {
|
|
812
|
+
if (this.config.debug)
|
|
813
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
const user = await this.withRefresh(async (aT) => {
|
|
817
|
+
return await fetch(`${this.ssoServerUrl}/skulls/${skullId}`, {
|
|
818
|
+
method: "PATCH",
|
|
819
|
+
body: JSON.stringify(payload),
|
|
820
|
+
headers: {
|
|
821
|
+
"Content-Type": "application/json",
|
|
822
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
823
|
+
"x-sdk-key": this.config.sdkKey,
|
|
824
|
+
},
|
|
825
|
+
});
|
|
826
|
+
});
|
|
827
|
+
if (!user || !user.ok) {
|
|
828
|
+
if (this.config.debug)
|
|
829
|
+
console.log(`SSO-SDK: Skull update failed.`);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
const { data } = (await user.json());
|
|
833
|
+
this.cb({
|
|
834
|
+
type: c.actions.skull_update,
|
|
835
|
+
payload: data,
|
|
836
|
+
});
|
|
837
|
+
return data;
|
|
838
|
+
};
|
|
839
|
+
this.skullGetAll = async () => {
|
|
840
|
+
if (!isBrowser()) {
|
|
841
|
+
if (this.config.debug)
|
|
842
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
await this.db.open();
|
|
846
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
847
|
+
if (!dbUser) {
|
|
848
|
+
if (this.config.debug)
|
|
849
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
const user = await this.withRefresh(async (aT) => {
|
|
853
|
+
return await fetch(`${this.ssoServerUrl}/skulls`, {
|
|
854
|
+
method: "GET",
|
|
855
|
+
headers: {
|
|
856
|
+
"Content-Type": "application/json",
|
|
857
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
858
|
+
"x-sdk-key": this.config.sdkKey,
|
|
859
|
+
},
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
if (!user || !user.ok) {
|
|
863
|
+
if (this.config.debug)
|
|
864
|
+
console.log(`SSO-SDK: Skull get all failed.`);
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const { data } = (await user.json());
|
|
868
|
+
this.cb({
|
|
869
|
+
type: c.actions.skull_fetch_all,
|
|
870
|
+
payload: data,
|
|
871
|
+
});
|
|
872
|
+
return data;
|
|
873
|
+
};
|
|
874
|
+
this.skullGetById = async (skullId) => {
|
|
875
|
+
if (!isBrowser()) {
|
|
876
|
+
if (this.config.debug)
|
|
877
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
await this.db.open();
|
|
881
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
882
|
+
if (!dbUser) {
|
|
883
|
+
if (this.config.debug)
|
|
884
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
const user = await this.withRefresh(async (aT) => {
|
|
888
|
+
return await fetch(`${this.ssoServerUrl}/skulls/${skullId}`, {
|
|
889
|
+
method: "GET",
|
|
890
|
+
headers: {
|
|
891
|
+
"Content-Type": "application/json",
|
|
892
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
893
|
+
"x-sdk-key": this.config.sdkKey,
|
|
894
|
+
},
|
|
895
|
+
});
|
|
896
|
+
});
|
|
897
|
+
if (!user || !user.ok) {
|
|
898
|
+
if (this.config.debug)
|
|
899
|
+
console.log(`SSO-SDK: Skull get by id failed.`);
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
const { data } = (await user.json());
|
|
903
|
+
this.cb({
|
|
904
|
+
type: c.actions.skull_fetch,
|
|
905
|
+
payload: data,
|
|
906
|
+
});
|
|
907
|
+
return data;
|
|
908
|
+
};
|
|
909
|
+
this.skullDelete = async (skullId) => {
|
|
910
|
+
if (!isBrowser()) {
|
|
911
|
+
if (this.config.debug)
|
|
912
|
+
console.log(`SSO-SDK: Not in browser.`);
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
await this.db.open();
|
|
916
|
+
const dbUser = (await this.db.getById(this.dbId));
|
|
917
|
+
if (!dbUser) {
|
|
918
|
+
if (this.config.debug)
|
|
919
|
+
console.log(`SSO-SDK: No user found in DB.`);
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
const user = await this.withRefresh(async (aT) => {
|
|
923
|
+
return await fetch(`${this.ssoServerUrl}/skulls/${skullId}`, {
|
|
924
|
+
method: "DELETE",
|
|
925
|
+
headers: {
|
|
926
|
+
"Content-Type": "application/json",
|
|
927
|
+
Authorization: `Bearer ${aT || dbUser.tokens.accessToken}`,
|
|
928
|
+
"x-sdk-key": this.config.sdkKey,
|
|
929
|
+
},
|
|
930
|
+
});
|
|
931
|
+
});
|
|
932
|
+
if (!user || !user.ok) {
|
|
933
|
+
if (this.config.debug)
|
|
934
|
+
console.log(`SSO-SDK: Skull delete failed.`);
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
this.cb({
|
|
938
|
+
type: c.actions.skull_delete,
|
|
939
|
+
payload: {
|
|
940
|
+
id: skullId,
|
|
941
|
+
},
|
|
942
|
+
});
|
|
943
|
+
return skullId;
|
|
944
|
+
};
|
|
945
|
+
if (config.debug)
|
|
946
|
+
console.log(`SSO-SDK: Debug mode enabled.`);
|
|
947
|
+
}
|
|
948
|
+
}
|