@proveanything/smartlinks-auth-ui 0.1.17 → 0.1.19

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.js CHANGED
@@ -11245,6 +11245,8 @@ const AuthProvider = ({ children, proxyMode = false, accountCacheTTL = 5 * 60 *
11245
11245
  const [accountInfo, setAccountInfo] = React.useState(null);
11246
11246
  const [isLoading, setIsLoading] = React.useState(true);
11247
11247
  const callbacksRef = React.useRef(new Set());
11248
+ // Initialization guard to prevent concurrent runs (NOT persisted across remounts)
11249
+ const initializingRef = React.useRef(false);
11248
11250
  // Notify all subscribers of auth state changes
11249
11251
  const notifyAuthStateChange = React.useCallback((type, currentUser, currentToken, currentAccountData, currentAccountInfo) => {
11250
11252
  callbacksRef.current.forEach(callback => {
@@ -11264,6 +11266,13 @@ const AuthProvider = ({ children, proxyMode = false, accountCacheTTL = 5 * 60 *
11264
11266
  }, []);
11265
11267
  // Initialize auth state - different behavior for proxy mode vs standalone mode
11266
11268
  React.useEffect(() => {
11269
+ // Prevent concurrent initialization only
11270
+ if (initializingRef.current) {
11271
+ console.log('[AuthContext] Skipping initialization - already in progress');
11272
+ return;
11273
+ }
11274
+ let isMounted = true;
11275
+ initializingRef.current = true;
11267
11276
  const initializeAuth = async () => {
11268
11277
  try {
11269
11278
  if (proxyMode) {
@@ -11275,7 +11284,7 @@ const AuthProvider = ({ children, proxyMode = false, accountCacheTTL = 5 * 60 *
11275
11284
  // empty/undefined if no user is logged in
11276
11285
  const accountAny = accountResponse;
11277
11286
  const hasValidSession = accountAny?.uid && accountAny.uid.length > 0;
11278
- if (hasValidSession) {
11287
+ if (hasValidSession && isMounted) {
11279
11288
  // User is logged in with valid account
11280
11289
  const userFromAccount = {
11281
11290
  uid: accountAny.uid,
@@ -11289,14 +11298,17 @@ const AuthProvider = ({ children, proxyMode = false, accountCacheTTL = 5 * 60 *
11289
11298
  console.log('[AuthContext] Proxy mode: initialized from parent account, uid:', accountAny.uid);
11290
11299
  notifyAuthStateChange('LOGIN', userFromAccount, null, accountResponse, accountResponse);
11291
11300
  }
11292
- else {
11301
+ else if (isMounted) {
11293
11302
  console.log('[AuthContext] Proxy mode: no valid session (no uid), awaiting login');
11294
11303
  }
11295
11304
  }
11296
11305
  catch (error) {
11297
11306
  console.log('[AuthContext] Proxy mode: auth.getAccount() failed, awaiting login:', error);
11298
11307
  }
11299
- setIsLoading(false);
11308
+ if (isMounted) {
11309
+ setIsLoading(false);
11310
+ initializingRef.current = false;
11311
+ }
11300
11312
  return;
11301
11313
  }
11302
11314
  // STANDALONE MODE: Load from persistent storage
@@ -11304,28 +11316,47 @@ const AuthProvider = ({ children, proxyMode = false, accountCacheTTL = 5 * 60 *
11304
11316
  const storedUser = await tokenStorage.getUser();
11305
11317
  const storedAccountData = await tokenStorage.getAccountData();
11306
11318
  if (storedToken && storedUser) {
11307
- setToken(storedToken.token);
11308
- setUser(storedUser);
11309
- setAccountData(storedAccountData);
11310
- // Set bearer token in global Smartlinks SDK via auth.verifyToken
11311
- smartlinks__namespace.auth.verifyToken(storedToken.token).catch(err => {
11312
- console.warn('Failed to restore bearer token on init:', err);
11313
- });
11319
+ // Verify token FIRST before setting state
11320
+ try {
11321
+ console.log('[AuthContext] Verifying stored token...');
11322
+ await smartlinks__namespace.auth.verifyToken(storedToken.token);
11323
+ // Only set state if verification succeeded and component still mounted
11324
+ if (isMounted) {
11325
+ setToken(storedToken.token);
11326
+ setUser(storedUser);
11327
+ setAccountData(storedAccountData);
11328
+ console.log('[AuthContext] Session restored successfully');
11329
+ }
11330
+ }
11331
+ catch (err) {
11332
+ console.warn('[AuthContext] Token verification failed, clearing stored credentials:', err);
11333
+ await tokenStorage.clearAll();
11334
+ // Don't set user state - leave as logged out
11335
+ }
11314
11336
  }
11315
11337
  // Load cached account info if available
11316
- const cachedAccountInfo = await tokenStorage.getAccountInfo();
11317
- if (cachedAccountInfo && !cachedAccountInfo.isStale) {
11318
- setAccountInfo(cachedAccountInfo.data);
11338
+ if (isMounted) {
11339
+ const cachedAccountInfo = await tokenStorage.getAccountInfo();
11340
+ if (cachedAccountInfo && !cachedAccountInfo.isStale) {
11341
+ setAccountInfo(cachedAccountInfo.data);
11342
+ }
11319
11343
  }
11320
11344
  }
11321
11345
  catch (error) {
11322
- console.error('Failed to initialize auth from storage:', error);
11346
+ console.error('[AuthContext] Failed to initialize auth from storage:', error);
11323
11347
  }
11324
11348
  finally {
11325
- setIsLoading(false);
11349
+ if (isMounted) {
11350
+ setIsLoading(false);
11351
+ initializingRef.current = false;
11352
+ }
11326
11353
  }
11327
11354
  };
11328
11355
  initializeAuth();
11356
+ // Cleanup for hot reload
11357
+ return () => {
11358
+ isMounted = false;
11359
+ };
11329
11360
  }, [proxyMode, notifyAuthStateChange]);
11330
11361
  // Listen for parent auth state changes (proxy mode only)
11331
11362
  React.useEffect(() => {