@tagadapay/plugin-sdk 2.4.25 → 2.4.28
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.
|
@@ -350,6 +350,58 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
|
|
|
350
350
|
setIsLoading(false);
|
|
351
351
|
}
|
|
352
352
|
}, [apiService, hasAttemptedAnonymousToken, initializeSession, finalDebugMode]);
|
|
353
|
+
// Initialize token from storage or create anonymous token (extracted to stable callback)
|
|
354
|
+
const initializeToken = useCallback(async () => {
|
|
355
|
+
try {
|
|
356
|
+
console.debug('[SDK] Initializing token...');
|
|
357
|
+
setIsLoading(true);
|
|
358
|
+
// Check for existing token
|
|
359
|
+
const existingToken = getClientToken();
|
|
360
|
+
let tokenToUse = null;
|
|
361
|
+
// Check URL params for token
|
|
362
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
363
|
+
const queryToken = urlParams.get('token');
|
|
364
|
+
if (queryToken) {
|
|
365
|
+
console.debug('[SDK] Found token in URL params');
|
|
366
|
+
tokenToUse = queryToken;
|
|
367
|
+
setClientToken(queryToken);
|
|
368
|
+
}
|
|
369
|
+
else if (existingToken && !isTokenExpired(existingToken)) {
|
|
370
|
+
console.debug('[SDK] Using existing token from storage');
|
|
371
|
+
tokenToUse = existingToken;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
console.debug('[SDK] No valid token found');
|
|
375
|
+
// Determine storeId for anonymous token
|
|
376
|
+
const targetStoreId = storeId || 'default-store';
|
|
377
|
+
await createAnonymousToken(targetStoreId);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (tokenToUse) {
|
|
381
|
+
setToken(tokenToUse);
|
|
382
|
+
// Update the API service with the token
|
|
383
|
+
apiService.updateToken(tokenToUse);
|
|
384
|
+
// Decode token to get session data
|
|
385
|
+
const decodedSession = decodeJWTClient(tokenToUse);
|
|
386
|
+
if (decodedSession) {
|
|
387
|
+
setSession(decodedSession);
|
|
388
|
+
// Initialize session with API call
|
|
389
|
+
await initializeSession(decodedSession);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
console.error('[SDK] Failed to decode token');
|
|
393
|
+
setIsInitialized(true);
|
|
394
|
+
setIsSessionInitialized(false); // Session failed to initialize
|
|
395
|
+
setIsLoading(false);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
console.error('[SDK] Error initializing token:', error);
|
|
401
|
+
setIsInitialized(true);
|
|
402
|
+
setIsLoading(false);
|
|
403
|
+
}
|
|
404
|
+
}, [apiService, storeId, createAnonymousToken, initializeSession]);
|
|
353
405
|
// Initialize token from storage or create anonymous token
|
|
354
406
|
// This runs in the background after phases 1 & 2 complete, but doesn't block rendering
|
|
355
407
|
useEffect(() => {
|
|
@@ -361,59 +413,19 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
|
|
|
361
413
|
return;
|
|
362
414
|
}
|
|
363
415
|
isInitializing.current = true;
|
|
364
|
-
const initializeToken = async () => {
|
|
365
|
-
try {
|
|
366
|
-
console.debug('[SDK] Initializing token...');
|
|
367
|
-
setIsLoading(true);
|
|
368
|
-
// Check for existing token
|
|
369
|
-
const existingToken = getClientToken();
|
|
370
|
-
let tokenToUse = null;
|
|
371
|
-
// Check URL params for token
|
|
372
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
373
|
-
const queryToken = urlParams.get('token');
|
|
374
|
-
if (queryToken) {
|
|
375
|
-
console.debug('[SDK] Found token in URL params');
|
|
376
|
-
tokenToUse = queryToken;
|
|
377
|
-
setClientToken(queryToken);
|
|
378
|
-
}
|
|
379
|
-
else if (existingToken && !isTokenExpired(existingToken)) {
|
|
380
|
-
console.debug('[SDK] Using existing token from storage');
|
|
381
|
-
tokenToUse = existingToken;
|
|
382
|
-
}
|
|
383
|
-
else {
|
|
384
|
-
console.debug('[SDK] No valid token found');
|
|
385
|
-
// Determine storeId for anonymous token
|
|
386
|
-
const targetStoreId = storeId || 'default-store';
|
|
387
|
-
await createAnonymousToken(targetStoreId);
|
|
388
|
-
return;
|
|
389
|
-
}
|
|
390
|
-
if (tokenToUse) {
|
|
391
|
-
setToken(tokenToUse);
|
|
392
|
-
// Update the API service with the token
|
|
393
|
-
apiService.updateToken(tokenToUse);
|
|
394
|
-
// Decode token to get session data
|
|
395
|
-
const decodedSession = decodeJWTClient(tokenToUse);
|
|
396
|
-
if (decodedSession) {
|
|
397
|
-
setSession(decodedSession);
|
|
398
|
-
// Initialize session with API call
|
|
399
|
-
await initializeSession(decodedSession);
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
console.error('[SDK] Failed to decode token');
|
|
403
|
-
setIsInitialized(true);
|
|
404
|
-
setIsSessionInitialized(false); // Session failed to initialize
|
|
405
|
-
setIsLoading(false);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
catch (error) {
|
|
410
|
-
console.error('[SDK] Error initializing token:', error);
|
|
411
|
-
setIsInitialized(true);
|
|
412
|
-
setIsLoading(false);
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
416
|
void initializeToken();
|
|
416
|
-
}, [storeId,
|
|
417
|
+
}, [storeId, configLoading, initializeToken]);
|
|
418
|
+
useEffect(() => {
|
|
419
|
+
function onStorage() {
|
|
420
|
+
// Re-run initialization when token may have changed in another tab
|
|
421
|
+
isInitializing.current = false;
|
|
422
|
+
void initializeToken();
|
|
423
|
+
}
|
|
424
|
+
window.addEventListener('storage', onStorage);
|
|
425
|
+
return () => {
|
|
426
|
+
window.removeEventListener('storage', onStorage);
|
|
427
|
+
};
|
|
428
|
+
}, [initializeToken]);
|
|
417
429
|
// Update auth state when customer/session changes
|
|
418
430
|
useEffect(() => {
|
|
419
431
|
setAuth({
|