@commercengine/storefront-sdk 0.2.1 → 0.3.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/index.d.ts +26 -8
- package/dist/index.js +83 -13
- package/dist/lib/auth.d.ts +65 -91
- package/dist/lib/auth.js +290 -318
- package/dist/lib/cart.d.ts +4 -4
- package/dist/lib/cart.js +21 -14
- package/package.json +1 -1
package/dist/lib/auth.js
CHANGED
|
@@ -10,38 +10,35 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
10
10
|
constructor(config, tokenStorage) {
|
|
11
11
|
super(config);
|
|
12
12
|
this.autoRefreshTimer = null;
|
|
13
|
-
|
|
14
|
-
this.
|
|
15
|
-
// Try to initialize from storage
|
|
16
|
-
const storedToken = this.tokenStorage.getAccessToken();
|
|
17
|
-
if (storedToken) {
|
|
18
|
-
this.setToken(storedToken);
|
|
19
|
-
this.setupAutoRefresh();
|
|
20
|
-
}
|
|
13
|
+
this.tokenStorage = tokenStorage;
|
|
14
|
+
this.setupAutoRefresh();
|
|
21
15
|
}
|
|
22
16
|
// Override the setToken method to use storage
|
|
23
|
-
setToken(token) {
|
|
24
|
-
super.setToken(token); //
|
|
25
|
-
this.tokenStorage.
|
|
17
|
+
async setToken(token) {
|
|
18
|
+
super.setToken(token); // Set token in client
|
|
19
|
+
await this.tokenStorage.setItem("access_token", token);
|
|
26
20
|
// If we also have a refresh token, store it
|
|
27
|
-
|
|
21
|
+
const refreshToken = await this.tokenStorage.getItem("refresh_token");
|
|
22
|
+
if (refreshToken) {
|
|
28
23
|
this.setupAutoRefresh();
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
26
|
// Store refresh token separately
|
|
32
|
-
setRefreshToken(token) {
|
|
33
|
-
this.tokenStorage.
|
|
27
|
+
async setRefreshToken(token) {
|
|
28
|
+
await this.tokenStorage.setItem("refresh_token", token);
|
|
34
29
|
}
|
|
35
30
|
// Clear tokens from storage
|
|
36
|
-
clearToken() {
|
|
37
|
-
super.clearToken(); //
|
|
38
|
-
this.tokenStorage.
|
|
31
|
+
async clearToken() {
|
|
32
|
+
super.clearToken(); // Clear token in client
|
|
33
|
+
await this.tokenStorage.removeItem("access_token");
|
|
34
|
+
await this.tokenStorage.removeItem("refresh_token");
|
|
35
|
+
await this.tokenStorage.removeItem("user_data");
|
|
39
36
|
this.clearAutoRefresh();
|
|
40
37
|
}
|
|
41
38
|
// Setup auto refresh based on token expiry
|
|
42
|
-
setupAutoRefresh() {
|
|
39
|
+
async setupAutoRefresh() {
|
|
43
40
|
this.clearAutoRefresh();
|
|
44
|
-
const token = this.tokenStorage.
|
|
41
|
+
const token = await this.tokenStorage.getItem("access_token");
|
|
45
42
|
if (!token)
|
|
46
43
|
return;
|
|
47
44
|
const expiryTime = this.getTokenExpiry(token);
|
|
@@ -73,16 +70,16 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
73
70
|
}
|
|
74
71
|
async handleTokenRefresh() {
|
|
75
72
|
try {
|
|
76
|
-
const refreshToken = this.tokenStorage.
|
|
73
|
+
const refreshToken = await this.tokenStorage.getItem("refresh_token");
|
|
77
74
|
if (!refreshToken)
|
|
78
75
|
return;
|
|
79
76
|
const tokens = await this.refreshToken(refreshToken);
|
|
80
|
-
this.setToken(tokens.access_token);
|
|
81
|
-
this.setRefreshToken(tokens.refresh_token);
|
|
77
|
+
await this.setToken(tokens.access_token);
|
|
78
|
+
await this.setRefreshToken(tokens.refresh_token);
|
|
82
79
|
}
|
|
83
80
|
catch (error) {
|
|
84
81
|
// If refresh fails, clear tokens
|
|
85
|
-
this.clearToken();
|
|
82
|
+
await this.clearToken();
|
|
86
83
|
}
|
|
87
84
|
}
|
|
88
85
|
getTokenExpiry(token) {
|
|
@@ -117,47 +114,45 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
117
114
|
}
|
|
118
115
|
}
|
|
119
116
|
/**
|
|
120
|
-
*
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
* Store auth data including tokens and user info
|
|
118
|
+
*/
|
|
119
|
+
async storeAuthData(accessToken, refreshToken, user) {
|
|
120
|
+
await this.setToken(accessToken);
|
|
121
|
+
await this.setRefreshToken(refreshToken);
|
|
122
|
+
if (user) {
|
|
123
|
+
await this.tokenStorage.setItem("user_data", JSON.stringify(user));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get stored user data
|
|
128
|
+
* @returns Promise that resolves to the stored user data or null if none exists
|
|
129
|
+
*/
|
|
130
|
+
async getStoredUser() {
|
|
131
|
+
const userData = await this.tokenStorage.getItem("user_data");
|
|
132
|
+
if (!userData)
|
|
133
|
+
return null;
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(userData);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get anonymous token for guest users
|
|
124
143
|
*/
|
|
125
144
|
async getAnonymousToken(options) {
|
|
126
145
|
return this.executeWithTokenRefresh(async () => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
// Set temporary API key if provided
|
|
133
|
-
if (tempApiKey) {
|
|
134
|
-
this.setApiKey(tempApiKey);
|
|
135
|
-
}
|
|
136
|
-
// Check if we have an API key set (either from constructor or from options)
|
|
137
|
-
if (!this.config.apiKey) {
|
|
138
|
-
throw new Error("X-Api-Key is required for anonymous authentication");
|
|
139
|
-
}
|
|
140
|
-
const { data, error } = await this.client.POST("/auth/anonymous");
|
|
141
|
-
if (error) {
|
|
142
|
-
this.handleError(error);
|
|
143
|
-
}
|
|
144
|
-
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
145
|
-
this.setToken(data.content.access_token);
|
|
146
|
-
this.setRefreshToken(data.content.refresh_token);
|
|
147
|
-
}
|
|
148
|
-
return data?.content;
|
|
149
|
-
}
|
|
150
|
-
finally {
|
|
151
|
-
// Restore the original API key state if we used a temporary one
|
|
152
|
-
if (tempApiKey) {
|
|
153
|
-
if (hadExistingApiKey && originalApiKey) {
|
|
154
|
-
this.setApiKey(originalApiKey);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
this.clearApiKey();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
146
|
+
const { data, error } = await this.client.POST("/auth/anonymous", {
|
|
147
|
+
headers: options?.apiKey ? { "X-Api-Key": options.apiKey } : undefined,
|
|
148
|
+
});
|
|
149
|
+
if (error) {
|
|
150
|
+
this.handleError(error);
|
|
160
151
|
}
|
|
152
|
+
const response = data?.content;
|
|
153
|
+
// Store tokens and user data
|
|
154
|
+
await this.storeAuthData(response.access_token, response.refresh_token, response.user);
|
|
155
|
+
return response;
|
|
161
156
|
});
|
|
162
157
|
}
|
|
163
158
|
/**
|
|
@@ -207,22 +202,21 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
207
202
|
/**
|
|
208
203
|
* Login with password
|
|
209
204
|
*
|
|
210
|
-
* @param
|
|
205
|
+
* @param credentials - Login credentials
|
|
211
206
|
* @returns Promise with user info and tokens
|
|
212
207
|
*/
|
|
213
|
-
async loginWithPassword(
|
|
208
|
+
async loginWithPassword(credentials) {
|
|
214
209
|
return this.executeWithTokenRefresh(async () => {
|
|
215
210
|
const { data, error } = await this.client.POST("/auth/login/password", {
|
|
216
|
-
body:
|
|
211
|
+
body: credentials,
|
|
217
212
|
});
|
|
218
213
|
if (error) {
|
|
219
214
|
this.handleError(error);
|
|
220
215
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return data?.content;
|
|
216
|
+
const response = data?.content;
|
|
217
|
+
// Store tokens and user ID
|
|
218
|
+
await this.storeAuthData(response.access_token, response.refresh_token, response.user);
|
|
219
|
+
return response;
|
|
226
220
|
});
|
|
227
221
|
}
|
|
228
222
|
/**
|
|
@@ -246,8 +240,8 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
246
240
|
this.handleError(error);
|
|
247
241
|
}
|
|
248
242
|
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
249
|
-
this.setToken(data.content.access_token);
|
|
250
|
-
this.setRefreshToken(data.content.refresh_token);
|
|
243
|
+
await this.setToken(data.content.access_token);
|
|
244
|
+
await this.setRefreshToken(data.content.refresh_token);
|
|
251
245
|
}
|
|
252
246
|
return data?.content;
|
|
253
247
|
});
|
|
@@ -270,8 +264,8 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
270
264
|
this.handleError(error);
|
|
271
265
|
}
|
|
272
266
|
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
273
|
-
this.setToken(data.content.access_token);
|
|
274
|
-
this.setRefreshToken(data.content.refresh_token);
|
|
267
|
+
await this.setToken(data.content.access_token);
|
|
268
|
+
await this.setRefreshToken(data.content.refresh_token);
|
|
275
269
|
}
|
|
276
270
|
return data?.content;
|
|
277
271
|
});
|
|
@@ -291,33 +285,26 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
291
285
|
this.handleError(error);
|
|
292
286
|
}
|
|
293
287
|
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
294
|
-
this.setToken(data.content.access_token);
|
|
295
|
-
this.setRefreshToken(data.content.refresh_token);
|
|
288
|
+
await this.setToken(data.content.access_token);
|
|
289
|
+
await this.setRefreshToken(data.content.refresh_token);
|
|
296
290
|
}
|
|
297
291
|
return data?.content;
|
|
298
292
|
});
|
|
299
293
|
}
|
|
300
294
|
/**
|
|
301
|
-
* Refresh token
|
|
302
|
-
*
|
|
303
|
-
* @param refreshToken - Refresh token
|
|
304
|
-
* @returns Promise with new tokens
|
|
295
|
+
* Refresh the access token using a refresh token
|
|
305
296
|
*/
|
|
306
297
|
async refreshToken(refreshToken) {
|
|
307
|
-
// Don't use executeWithTokenRefresh here to avoid infinite loop
|
|
308
298
|
const { data, error } = await this.client.POST("/auth/refresh-token", {
|
|
309
|
-
body: {
|
|
310
|
-
refresh_token: refreshToken,
|
|
311
|
-
},
|
|
299
|
+
body: { refresh_token: refreshToken },
|
|
312
300
|
});
|
|
313
301
|
if (error) {
|
|
314
302
|
this.handleError(error);
|
|
315
303
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
return data?.content;
|
|
304
|
+
const response = data?.content;
|
|
305
|
+
// Store new tokens but don't update user ID as it hasn't changed
|
|
306
|
+
await this.storeAuthData(response.access_token, response.refresh_token);
|
|
307
|
+
return response;
|
|
321
308
|
}
|
|
322
309
|
/**
|
|
323
310
|
* Logout
|
|
@@ -330,7 +317,7 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
330
317
|
if (error) {
|
|
331
318
|
this.handleError(error);
|
|
332
319
|
}
|
|
333
|
-
this.clearToken();
|
|
320
|
+
await this.clearToken();
|
|
334
321
|
});
|
|
335
322
|
}
|
|
336
323
|
/**
|
|
@@ -340,22 +327,28 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
340
327
|
* @returns Promise that resolves to true if token was refreshed, false otherwise
|
|
341
328
|
*/
|
|
342
329
|
async attemptTokenRefresh() {
|
|
343
|
-
const refreshToken = this.tokenStorage.
|
|
330
|
+
const refreshToken = await this.tokenStorage.getItem("refresh_token");
|
|
344
331
|
if (!refreshToken)
|
|
345
332
|
return false;
|
|
346
333
|
try {
|
|
334
|
+
// Get current user data before refresh
|
|
335
|
+
const userData = await this.getStoredUser();
|
|
347
336
|
const tokens = await this.refreshToken(refreshToken);
|
|
348
337
|
if (tokens.access_token) {
|
|
349
|
-
this.setToken(tokens.access_token);
|
|
338
|
+
await this.setToken(tokens.access_token);
|
|
350
339
|
}
|
|
351
340
|
if (tokens.refresh_token) {
|
|
352
|
-
this.setRefreshToken(tokens.refresh_token);
|
|
341
|
+
await this.setRefreshToken(tokens.refresh_token);
|
|
342
|
+
}
|
|
343
|
+
// Restore user data after refresh since it hasn't changed
|
|
344
|
+
if (userData) {
|
|
345
|
+
await this.tokenStorage.setItem("user_data", JSON.stringify(userData));
|
|
353
346
|
}
|
|
354
347
|
return true;
|
|
355
348
|
}
|
|
356
349
|
catch (error) {
|
|
357
350
|
// If refresh fails, clear tokens
|
|
358
|
-
this.clearToken();
|
|
351
|
+
await this.clearToken();
|
|
359
352
|
return false;
|
|
360
353
|
}
|
|
361
354
|
}
|
|
@@ -392,31 +385,51 @@ class MemoryTokenStorage {
|
|
|
392
385
|
this.accessToken = null;
|
|
393
386
|
this.refreshToken = null;
|
|
394
387
|
this.cartId = null;
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
388
|
+
this.userData = null;
|
|
389
|
+
}
|
|
390
|
+
getItem(key) {
|
|
391
|
+
if (key === "access_token")
|
|
392
|
+
return Promise.resolve(this.accessToken);
|
|
393
|
+
if (key === "refresh_token")
|
|
394
|
+
return Promise.resolve(this.refreshToken);
|
|
395
|
+
if (key === "cart_id")
|
|
396
|
+
return Promise.resolve(this.cartId);
|
|
397
|
+
if (key === "user_data")
|
|
398
|
+
return Promise.resolve(this.userData);
|
|
399
|
+
return Promise.resolve(null);
|
|
400
|
+
}
|
|
401
|
+
setItem(key, value) {
|
|
402
|
+
if (key === "access_token")
|
|
403
|
+
this.accessToken = value;
|
|
404
|
+
if (key === "refresh_token")
|
|
405
|
+
this.refreshToken = value;
|
|
406
|
+
if (key === "cart_id")
|
|
407
|
+
this.cartId = value;
|
|
408
|
+
if (key === "user_data")
|
|
409
|
+
this.userData = value;
|
|
410
|
+
return Promise.resolve();
|
|
411
|
+
}
|
|
412
|
+
removeItem(key) {
|
|
413
|
+
if (key === "access_token")
|
|
414
|
+
this.accessToken = null;
|
|
415
|
+
if (key === "refresh_token")
|
|
416
|
+
this.refreshToken = null;
|
|
417
|
+
if (key === "cart_id")
|
|
418
|
+
this.cartId = null;
|
|
419
|
+
if (key === "user_data")
|
|
420
|
+
this.userData = null;
|
|
421
|
+
return Promise.resolve();
|
|
411
422
|
}
|
|
412
423
|
getCartId() {
|
|
413
|
-
return this.cartId;
|
|
424
|
+
return Promise.resolve(this.cartId);
|
|
414
425
|
}
|
|
415
426
|
setCartId(cartId) {
|
|
416
427
|
this.cartId = cartId;
|
|
428
|
+
return Promise.resolve();
|
|
417
429
|
}
|
|
418
430
|
clearCartId() {
|
|
419
431
|
this.cartId = null;
|
|
432
|
+
return Promise.resolve();
|
|
420
433
|
}
|
|
421
434
|
}
|
|
422
435
|
exports.MemoryTokenStorage = MemoryTokenStorage;
|
|
@@ -428,47 +441,51 @@ class BrowserTokenStorage {
|
|
|
428
441
|
this.accessTokenKey = `${prefix}access_token`;
|
|
429
442
|
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
430
443
|
this.cartIdKey = `${prefix}cart_id`;
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
if (
|
|
439
|
-
return;
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
444
|
+
this.userDataKey = `${prefix}user_data`;
|
|
445
|
+
}
|
|
446
|
+
getItem(key) {
|
|
447
|
+
if (key === "access_token")
|
|
448
|
+
return Promise.resolve(localStorage.getItem(this.accessTokenKey));
|
|
449
|
+
if (key === "refresh_token")
|
|
450
|
+
return Promise.resolve(localStorage.getItem(this.refreshTokenKey));
|
|
451
|
+
if (key === "cart_id")
|
|
452
|
+
return Promise.resolve(localStorage.getItem(this.cartIdKey));
|
|
453
|
+
if (key === "user_data")
|
|
454
|
+
return Promise.resolve(localStorage.getItem(this.userDataKey));
|
|
455
|
+
return Promise.resolve(null);
|
|
456
|
+
}
|
|
457
|
+
setItem(key, value) {
|
|
458
|
+
if (key === "access_token")
|
|
459
|
+
localStorage.setItem(this.accessTokenKey, value);
|
|
460
|
+
if (key === "refresh_token")
|
|
461
|
+
localStorage.setItem(this.refreshTokenKey, value);
|
|
462
|
+
if (key === "cart_id")
|
|
463
|
+
localStorage.setItem(this.cartIdKey, value);
|
|
464
|
+
if (key === "user_data")
|
|
465
|
+
localStorage.setItem(this.userDataKey, value);
|
|
466
|
+
return Promise.resolve();
|
|
467
|
+
}
|
|
468
|
+
removeItem(key) {
|
|
469
|
+
if (key === "access_token")
|
|
470
|
+
localStorage.removeItem(this.accessTokenKey);
|
|
471
|
+
if (key === "refresh_token")
|
|
472
|
+
localStorage.removeItem(this.refreshTokenKey);
|
|
473
|
+
if (key === "cart_id")
|
|
474
|
+
localStorage.removeItem(this.cartIdKey);
|
|
475
|
+
if (key === "user_data")
|
|
476
|
+
localStorage.removeItem(this.userDataKey);
|
|
477
|
+
return Promise.resolve();
|
|
457
478
|
}
|
|
458
479
|
getCartId() {
|
|
459
|
-
|
|
460
|
-
return null;
|
|
461
|
-
return localStorage.getItem(this.cartIdKey);
|
|
480
|
+
return Promise.resolve(localStorage.getItem(this.cartIdKey));
|
|
462
481
|
}
|
|
463
482
|
setCartId(cartId) {
|
|
464
|
-
if (typeof localStorage === "undefined")
|
|
465
|
-
return;
|
|
466
483
|
localStorage.setItem(this.cartIdKey, cartId);
|
|
484
|
+
return Promise.resolve();
|
|
467
485
|
}
|
|
468
486
|
clearCartId() {
|
|
469
|
-
if (typeof localStorage === "undefined")
|
|
470
|
-
return;
|
|
471
487
|
localStorage.removeItem(this.cartIdKey);
|
|
488
|
+
return Promise.resolve();
|
|
472
489
|
}
|
|
473
490
|
}
|
|
474
491
|
exports.BrowserTokenStorage = BrowserTokenStorage;
|
|
@@ -486,147 +503,85 @@ class CookieTokenStorage {
|
|
|
486
503
|
this.accessTokenKey = `${prefix}access_token`;
|
|
487
504
|
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
488
505
|
this.cartIdKey = `${prefix}cart_id`;
|
|
506
|
+
this.userDataKey = `${prefix}user_data`;
|
|
489
507
|
this.cookieOptions = cookieOptions;
|
|
490
508
|
}
|
|
491
509
|
/**
|
|
492
|
-
* Get
|
|
493
|
-
* Works in both browser and Next.js server components
|
|
494
|
-
*/
|
|
495
|
-
getAccessToken() {
|
|
496
|
-
// Browser environment
|
|
497
|
-
if (typeof document !== "undefined") {
|
|
498
|
-
return getCookieValue(this.accessTokenKey);
|
|
499
|
-
}
|
|
500
|
-
// Next.js server component - would need to be implemented by user
|
|
501
|
-
// with their specific cookie library
|
|
502
|
-
return null;
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* Set access token in cookies
|
|
506
|
-
* Works in browser environment
|
|
507
|
-
*/
|
|
508
|
-
setAccessToken(token) {
|
|
509
|
-
// Browser environment
|
|
510
|
-
if (typeof document !== "undefined") {
|
|
511
|
-
setCookie(this.accessTokenKey, token, this.cookieOptions);
|
|
512
|
-
}
|
|
513
|
-
// Next.js - would need to be implemented by user
|
|
514
|
-
// with their specific cookie API
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Get refresh token from cookies
|
|
510
|
+
* Get value from cookies
|
|
518
511
|
* Works in both browser and Next.js server components
|
|
519
512
|
*/
|
|
520
|
-
|
|
513
|
+
getItem(key) {
|
|
521
514
|
// Browser environment
|
|
522
515
|
if (typeof document !== "undefined") {
|
|
523
|
-
|
|
516
|
+
if (key === "access_token")
|
|
517
|
+
return Promise.resolve(getCookieValue(this.accessTokenKey));
|
|
518
|
+
if (key === "refresh_token")
|
|
519
|
+
return Promise.resolve(getCookieValue(this.refreshTokenKey));
|
|
520
|
+
if (key === "cart_id")
|
|
521
|
+
return Promise.resolve(getCookieValue(this.cartIdKey));
|
|
522
|
+
if (key === "user_data")
|
|
523
|
+
return Promise.resolve(getCookieValue(this.userDataKey));
|
|
524
524
|
}
|
|
525
525
|
// Next.js server component - would need to be implemented by user
|
|
526
|
-
return null;
|
|
526
|
+
return Promise.resolve(null);
|
|
527
527
|
}
|
|
528
528
|
/**
|
|
529
|
-
* Set
|
|
529
|
+
* Set value in cookies
|
|
530
530
|
* Works in browser environment
|
|
531
531
|
*/
|
|
532
|
-
|
|
532
|
+
setItem(key, value) {
|
|
533
533
|
// Browser environment
|
|
534
534
|
if (typeof document !== "undefined") {
|
|
535
|
-
|
|
535
|
+
if (key === "access_token")
|
|
536
|
+
setCookie(this.accessTokenKey, value, this.cookieOptions);
|
|
537
|
+
if (key === "refresh_token")
|
|
538
|
+
setCookie(this.refreshTokenKey, value, this.cookieOptions);
|
|
539
|
+
if (key === "cart_id")
|
|
540
|
+
setCookie(this.cartIdKey, value, this.cookieOptions);
|
|
541
|
+
if (key === "user_data")
|
|
542
|
+
setCookie(this.userDataKey, value, this.cookieOptions);
|
|
536
543
|
}
|
|
537
544
|
// Next.js - would need to be implemented by user
|
|
545
|
+
return Promise.resolve();
|
|
538
546
|
}
|
|
539
547
|
/**
|
|
540
|
-
*
|
|
548
|
+
* Remove value from cookies
|
|
541
549
|
*/
|
|
542
|
-
|
|
550
|
+
removeItem(key) {
|
|
543
551
|
// Browser environment
|
|
544
552
|
if (typeof document !== "undefined") {
|
|
545
|
-
|
|
546
|
-
|
|
553
|
+
if (key === "access_token")
|
|
554
|
+
deleteCookie(this.accessTokenKey);
|
|
555
|
+
if (key === "refresh_token")
|
|
556
|
+
deleteCookie(this.refreshTokenKey);
|
|
557
|
+
if (key === "cart_id")
|
|
558
|
+
deleteCookie(this.cartIdKey);
|
|
559
|
+
if (key === "user_data")
|
|
560
|
+
deleteCookie(this.userDataKey);
|
|
547
561
|
}
|
|
548
562
|
// Next.js - would need to be implemented by user
|
|
563
|
+
return Promise.resolve();
|
|
549
564
|
}
|
|
550
565
|
/**
|
|
551
566
|
* Get cart ID from cookies
|
|
552
|
-
* Works in both browser and Next.js server components
|
|
553
567
|
*/
|
|
554
568
|
getCartId() {
|
|
555
|
-
|
|
556
|
-
if (typeof document !== "undefined") {
|
|
557
|
-
return getCookieValue(this.cartIdKey);
|
|
558
|
-
}
|
|
559
|
-
// Next.js server component - would need to be implemented by user
|
|
560
|
-
return null;
|
|
569
|
+
return this.getItem(this.cartIdKey);
|
|
561
570
|
}
|
|
562
571
|
/**
|
|
563
572
|
* Set cart ID in cookies
|
|
564
|
-
* Works in browser environment
|
|
565
573
|
*/
|
|
566
574
|
setCartId(cartId) {
|
|
567
|
-
|
|
568
|
-
if (typeof document !== "undefined") {
|
|
569
|
-
setCookie(this.cartIdKey, cartId, this.cookieOptions);
|
|
570
|
-
}
|
|
571
|
-
// Next.js - would need to be implemented by user
|
|
575
|
+
return this.setItem(this.cartIdKey, cartId);
|
|
572
576
|
}
|
|
573
577
|
/**
|
|
574
578
|
* Clear cart ID from cookies
|
|
575
579
|
*/
|
|
576
580
|
clearCartId() {
|
|
577
|
-
|
|
578
|
-
if (typeof document !== "undefined") {
|
|
579
|
-
deleteCookie(this.cartIdKey);
|
|
580
|
-
}
|
|
581
|
-
// Next.js - would need to be implemented by user
|
|
581
|
+
return this.removeItem(this.cartIdKey);
|
|
582
582
|
}
|
|
583
583
|
}
|
|
584
584
|
exports.CookieTokenStorage = CookieTokenStorage;
|
|
585
|
-
/**
|
|
586
|
-
* Helper function to get cookie value
|
|
587
|
-
*/
|
|
588
|
-
function getCookieValue(name) {
|
|
589
|
-
if (typeof document === "undefined")
|
|
590
|
-
return null;
|
|
591
|
-
const match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
|
|
592
|
-
return match ? decodeURIComponent(match[2]) : null;
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* Helper function to set cookie
|
|
596
|
-
*/
|
|
597
|
-
function setCookie(name, value, options = {}) {
|
|
598
|
-
if (typeof document === "undefined")
|
|
599
|
-
return;
|
|
600
|
-
const cookieOptions = {
|
|
601
|
-
path: "/",
|
|
602
|
-
...options,
|
|
603
|
-
};
|
|
604
|
-
let cookie = `${name}=${encodeURIComponent(value)}`;
|
|
605
|
-
if (cookieOptions.maxAge) {
|
|
606
|
-
cookie += `; max-age=${cookieOptions.maxAge}`;
|
|
607
|
-
}
|
|
608
|
-
if (cookieOptions.domain) {
|
|
609
|
-
cookie += `; domain=${cookieOptions.domain}`;
|
|
610
|
-
}
|
|
611
|
-
if (cookieOptions.path) {
|
|
612
|
-
cookie += `; path=${cookieOptions.path}`;
|
|
613
|
-
}
|
|
614
|
-
if (cookieOptions.secure) {
|
|
615
|
-
cookie += "; secure";
|
|
616
|
-
}
|
|
617
|
-
if (cookieOptions.sameSite) {
|
|
618
|
-
cookie += `; samesite=${cookieOptions.sameSite}`;
|
|
619
|
-
}
|
|
620
|
-
document.cookie = cookie;
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Helper function to delete cookie
|
|
624
|
-
*/
|
|
625
|
-
function deleteCookie(name, path = "/") {
|
|
626
|
-
if (typeof document === "undefined")
|
|
627
|
-
return;
|
|
628
|
-
document.cookie = `${name}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT`;
|
|
629
|
-
}
|
|
630
585
|
/**
|
|
631
586
|
* Next.js specific cookie storage implementation
|
|
632
587
|
* Works with the Next.js cookies API
|
|
@@ -636,128 +591,145 @@ class NextCookieTokenStorage {
|
|
|
636
591
|
this.accessTokenKey = `${prefix}access_token`;
|
|
637
592
|
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
638
593
|
this.cartIdKey = `${prefix}cart_id`;
|
|
594
|
+
this.userDataKey = `${prefix}user_data`;
|
|
639
595
|
this.cookieStore = cookieStore;
|
|
640
596
|
}
|
|
641
|
-
|
|
642
|
-
* Get access token from Next.js cookies
|
|
643
|
-
*/
|
|
644
|
-
getAccessToken() {
|
|
597
|
+
getItem(key) {
|
|
645
598
|
try {
|
|
646
599
|
// Assuming cookieStore.get method exists (like in next/headers)
|
|
647
|
-
|
|
648
|
-
|
|
600
|
+
let cookieKey = "";
|
|
601
|
+
if (key === "access_token")
|
|
602
|
+
cookieKey = this.accessTokenKey;
|
|
603
|
+
if (key === "refresh_token")
|
|
604
|
+
cookieKey = this.refreshTokenKey;
|
|
605
|
+
if (key === "cart_id")
|
|
606
|
+
cookieKey = this.cartIdKey;
|
|
607
|
+
if (key === "user_data")
|
|
608
|
+
cookieKey = this.userDataKey;
|
|
609
|
+
if (!cookieKey)
|
|
610
|
+
return Promise.resolve(null);
|
|
611
|
+
const cookie = this.cookieStore.get(cookieKey);
|
|
612
|
+
return cookie ? Promise.resolve(cookie.value) : Promise.resolve(null);
|
|
649
613
|
}
|
|
650
614
|
catch (error) {
|
|
651
|
-
return null;
|
|
615
|
+
return Promise.resolve(null);
|
|
652
616
|
}
|
|
653
617
|
}
|
|
654
|
-
|
|
655
|
-
* Set access token in Next.js cookies
|
|
656
|
-
*/
|
|
657
|
-
setAccessToken(token) {
|
|
618
|
+
setItem(key, value) {
|
|
658
619
|
try {
|
|
659
620
|
// Assuming cookieStore.set method exists
|
|
660
|
-
|
|
621
|
+
let cookieKey = "";
|
|
622
|
+
if (key === "access_token")
|
|
623
|
+
cookieKey = this.accessTokenKey;
|
|
624
|
+
if (key === "refresh_token")
|
|
625
|
+
cookieKey = this.refreshTokenKey;
|
|
626
|
+
if (key === "cart_id")
|
|
627
|
+
cookieKey = this.cartIdKey;
|
|
628
|
+
if (key === "user_data")
|
|
629
|
+
cookieKey = this.userDataKey;
|
|
630
|
+
if (!cookieKey)
|
|
631
|
+
return Promise.resolve();
|
|
632
|
+
this.cookieStore.set(cookieKey, value, {
|
|
661
633
|
path: "/",
|
|
662
634
|
secure: process.env.NODE_ENV === "production",
|
|
663
635
|
httpOnly: true,
|
|
664
636
|
sameSite: "strict",
|
|
665
637
|
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
666
638
|
});
|
|
639
|
+
return Promise.resolve();
|
|
667
640
|
}
|
|
668
641
|
catch (error) {
|
|
669
642
|
// Silently fail - might be in an environment where cookies can't be set
|
|
643
|
+
return Promise.resolve();
|
|
670
644
|
}
|
|
671
645
|
}
|
|
672
|
-
|
|
673
|
-
* Get refresh token from Next.js cookies
|
|
674
|
-
*/
|
|
675
|
-
getRefreshToken() {
|
|
676
|
-
try {
|
|
677
|
-
// Assuming cookieStore.get method exists
|
|
678
|
-
const cookie = this.cookieStore.get(this.refreshTokenKey);
|
|
679
|
-
return cookie ? cookie.value : null;
|
|
680
|
-
}
|
|
681
|
-
catch (error) {
|
|
682
|
-
return null;
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Set refresh token in Next.js cookies
|
|
687
|
-
*/
|
|
688
|
-
setRefreshToken(token) {
|
|
689
|
-
try {
|
|
690
|
-
// Assuming cookieStore.set method exists
|
|
691
|
-
this.cookieStore.set(this.refreshTokenKey, token, {
|
|
692
|
-
path: "/",
|
|
693
|
-
secure: process.env.NODE_ENV === "production",
|
|
694
|
-
httpOnly: true,
|
|
695
|
-
sameSite: "strict",
|
|
696
|
-
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
catch (error) {
|
|
700
|
-
// Silently fail
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
/**
|
|
704
|
-
* Clear all tokens from Next.js cookies
|
|
705
|
-
*/
|
|
706
|
-
clearTokens() {
|
|
646
|
+
removeItem(key) {
|
|
707
647
|
try {
|
|
708
648
|
// Assuming cookieStore.delete method exists
|
|
709
|
-
|
|
710
|
-
|
|
649
|
+
let cookieKey = "";
|
|
650
|
+
if (key === "access_token")
|
|
651
|
+
cookieKey = this.accessTokenKey;
|
|
652
|
+
if (key === "refresh_token")
|
|
653
|
+
cookieKey = this.refreshTokenKey;
|
|
654
|
+
if (key === "cart_id")
|
|
655
|
+
cookieKey = this.cartIdKey;
|
|
656
|
+
if (key === "user_data")
|
|
657
|
+
cookieKey = this.userDataKey;
|
|
658
|
+
if (!cookieKey)
|
|
659
|
+
return Promise.resolve();
|
|
660
|
+
this.cookieStore.delete(cookieKey);
|
|
661
|
+
return Promise.resolve();
|
|
711
662
|
}
|
|
712
663
|
catch (error) {
|
|
713
664
|
// Silently fail
|
|
665
|
+
return Promise.resolve();
|
|
714
666
|
}
|
|
715
667
|
}
|
|
716
668
|
/**
|
|
717
669
|
* Get cart ID from Next.js cookies
|
|
718
670
|
*/
|
|
719
671
|
getCartId() {
|
|
720
|
-
|
|
721
|
-
// Assuming cookieStore.get method exists
|
|
722
|
-
const cookie = this.cookieStore.get(this.cartIdKey);
|
|
723
|
-
return cookie ? cookie.value : null;
|
|
724
|
-
}
|
|
725
|
-
catch (error) {
|
|
726
|
-
return null;
|
|
727
|
-
}
|
|
672
|
+
return this.getItem(this.cartIdKey);
|
|
728
673
|
}
|
|
729
674
|
/**
|
|
730
675
|
* Set cart ID in Next.js cookies
|
|
731
676
|
*/
|
|
732
677
|
setCartId(cartId) {
|
|
733
|
-
|
|
734
|
-
// Assuming cookieStore.set method exists
|
|
735
|
-
this.cookieStore.set(this.cartIdKey, cartId, {
|
|
736
|
-
path: "/",
|
|
737
|
-
secure: process.env.NODE_ENV === "production",
|
|
738
|
-
httpOnly: true,
|
|
739
|
-
sameSite: "strict",
|
|
740
|
-
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
catch (error) {
|
|
744
|
-
// Silently fail
|
|
745
|
-
}
|
|
678
|
+
return this.setItem(this.cartIdKey, cartId);
|
|
746
679
|
}
|
|
747
680
|
/**
|
|
748
681
|
* Clear cart ID from Next.js cookies
|
|
749
682
|
*/
|
|
750
683
|
clearCartId() {
|
|
751
|
-
|
|
752
|
-
// Assuming cookieStore.delete method exists
|
|
753
|
-
this.cookieStore.delete(this.cartIdKey);
|
|
754
|
-
}
|
|
755
|
-
catch (error) {
|
|
756
|
-
// Silently fail
|
|
757
|
-
}
|
|
684
|
+
return this.removeItem(this.cartIdKey);
|
|
758
685
|
}
|
|
759
686
|
}
|
|
760
687
|
exports.NextCookieTokenStorage = NextCookieTokenStorage;
|
|
688
|
+
/**
|
|
689
|
+
* Helper function to get cookie value
|
|
690
|
+
*/
|
|
691
|
+
function getCookieValue(name) {
|
|
692
|
+
if (typeof document === "undefined")
|
|
693
|
+
return null;
|
|
694
|
+
const match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
|
|
695
|
+
return match ? decodeURIComponent(match[2]) : null;
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Helper function to set cookie
|
|
699
|
+
*/
|
|
700
|
+
function setCookie(name, value, options = {}) {
|
|
701
|
+
if (typeof document === "undefined")
|
|
702
|
+
return;
|
|
703
|
+
const cookieOptions = {
|
|
704
|
+
path: "/",
|
|
705
|
+
...options,
|
|
706
|
+
};
|
|
707
|
+
let cookie = `${name}=${encodeURIComponent(value)}`;
|
|
708
|
+
if (cookieOptions.maxAge) {
|
|
709
|
+
cookie += `; max-age=${cookieOptions.maxAge}`;
|
|
710
|
+
}
|
|
711
|
+
if (cookieOptions.domain) {
|
|
712
|
+
cookie += `; domain=${cookieOptions.domain}`;
|
|
713
|
+
}
|
|
714
|
+
if (cookieOptions.path) {
|
|
715
|
+
cookie += `; path=${cookieOptions.path}`;
|
|
716
|
+
}
|
|
717
|
+
if (cookieOptions.secure) {
|
|
718
|
+
cookie += "; secure";
|
|
719
|
+
}
|
|
720
|
+
if (cookieOptions.sameSite) {
|
|
721
|
+
cookie += `; samesite=${cookieOptions.sameSite}`;
|
|
722
|
+
}
|
|
723
|
+
document.cookie = cookie;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Helper function to delete cookie
|
|
727
|
+
*/
|
|
728
|
+
function deleteCookie(name, path = "/") {
|
|
729
|
+
if (typeof document === "undefined")
|
|
730
|
+
return;
|
|
731
|
+
document.cookie = `${name}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT`;
|
|
732
|
+
}
|
|
761
733
|
/**
|
|
762
734
|
* Helper to create a token storage instance based on environment
|
|
763
735
|
* Automatically selects the best storage method based on context
|