@explorins/pers-sdk-react-native 2.1.17 → 2.1.18

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.
@@ -1 +1 @@
1
- {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAC;AAGlF,YAAY,EAAE,WAAW,EAAE,CAAC;AAE5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,OAAO;;;;sBA0ByB,MAAM,aAAY,gBAAgB,KAA2B,QAAQ,6BAA6B,CAAC;oCAgCrF,WAAW,KAAG,QAAQ,IAAI,CAAC;kBAgC/C,QAAQ,IAAI,CAAC;;0BA0BL,QAAQ,OAAO,CAAC;gCAmBV,QAAQ,OAAO,CAAC;mCAqBX,MAAM;qBAmBtB,QAAQ,IAAI,CAAC;wBAoBV,QAAQ,OAAO,CAAC;CAwB5D,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,OAAO,CAAC,CAAC"}
1
+ {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,6BAA6B,EAAE,MAAM,qBAAqB,CAAC;AAGlF,YAAY,EAAE,WAAW,EAAE,CAAC;AAE5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,OAAO;;;;sBAyByB,MAAM,aAAY,gBAAgB,KAA2B,QAAQ,6BAA6B,CAAC;oCA+BrF,WAAW,KAAG,QAAQ,IAAI,CAAC;kBA+B/C,QAAQ,IAAI,CAAC;;0BA2BL,QAAQ,OAAO,CAAC;gCAmBV,QAAQ,OAAO,CAAC;mCAqBX,MAAM;qBAmBtB,QAAQ,IAAI,CAAC;wBAoBV,QAAQ,OAAO,CAAC;CAwB5D,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,OAAO,CAAC,CAAC"}
@@ -39,7 +39,7 @@ import { AccountOwnerType } from '@explorins/pers-sdk/core';
39
39
  * ```
40
40
  */
41
41
  export const useAuth = () => {
42
- const { sdk, authProvider, isInitialized, isAuthenticated, user, setAuthenticationState, refreshUserData } = usePersSDK();
42
+ const { sdk, authProvider, isInitialized, isAuthenticated, user, refreshUserData } = usePersSDK();
43
43
  /**
44
44
  * Authenticates a user with a JWT token
45
45
  *
@@ -62,8 +62,7 @@ export const useAuth = () => {
62
62
  try {
63
63
  // Set token in auth provider
64
64
  await authProvider.setAccessToken(jwtToken);
65
- // Perform login using the manager
66
- // SDK emits login_success event with user data - PersSDKProvider handles state sync
65
+ // Perform login - emits login_success event which Provider handles
67
66
  const result = await sdk.auth.loginWithToken(jwtToken, userType);
68
67
  return result;
69
68
  }
@@ -89,8 +88,7 @@ export const useAuth = () => {
89
88
  throw new Error('SDK not initialized. Call initialize() first.');
90
89
  }
91
90
  try {
92
- // Use the raw data login from the auth manager
93
- // SDK emits login_success event with user data - PersSDKProvider handles state sync
91
+ // Use the raw data login - emits login_success event which Provider handles
94
92
  const result = await sdk.auth.loginWithRawData(rawUserData);
95
93
  // Set token from result
96
94
  if (result.accessToken) {
@@ -115,17 +113,18 @@ export const useAuth = () => {
115
113
  * ```
116
114
  */
117
115
  const logout = useCallback(async () => {
116
+ if (!sdk) {
117
+ throw new Error('SDK not initialized. Call initialize() first.');
118
+ }
118
119
  try {
119
- if (authProvider) {
120
- await authProvider.clearTokens();
121
- }
122
- setAuthenticationState(null, false);
120
+ // clearAuth emits logout_success event which Provider handles
121
+ await sdk.auth.clearAuth();
123
122
  }
124
123
  catch (error) {
125
124
  console.error('Logout failed:', error);
126
125
  throw error;
127
126
  }
128
- }, [authProvider, setAuthenticationState]);
127
+ }, [sdk]);
129
128
  /**
130
129
  * Retrieves the current authenticated user data
131
130
  *
@@ -199,9 +198,9 @@ export const useAuth = () => {
199
198
  if (!sdk) {
200
199
  throw new Error('SDK not initialized. Call initialize() first.');
201
200
  }
201
+ // clearAuth emits logout_success event which Provider handles
202
202
  await sdk.auth.clearAuth();
203
- setAuthenticationState(null, false);
204
- }, [sdk, setAuthenticationState]);
203
+ }, [sdk]);
205
204
  /**
206
205
  * Checks if the current authentication is valid (non-expired)
207
206
  *
package/dist/index.js CHANGED
@@ -9905,6 +9905,11 @@ class DefaultAuthProvider {
9905
9905
  async hasValidToken() {
9906
9906
  return this.tokenManager.hasAccessToken();
9907
9907
  }
9908
+ async setAuthType(authType) {
9909
+ // Auth type is derived from JWT, so this is a no-op
9910
+ // The authType in JWT is the source of truth
9911
+ // This method exists to satisfy the ExtendedTokenStorageProvider interface
9912
+ }
9908
9913
  async getAuthType() {
9909
9914
  // Read from JWT instead of separate storage for single source of truth
9910
9915
  const token = await this.tokenManager.getAccessToken();
@@ -10980,6 +10985,11 @@ class AuthManager {
10980
10985
  async clearAuth() {
10981
10986
  const authService = this.apiClient.getAuthService();
10982
10987
  await authService.clearTokens();
10988
+ this.events?.emitSuccess({
10989
+ domain: 'authentication',
10990
+ type: 'logout_success',
10991
+ userMessage: 'Successfully logged out'
10992
+ });
10983
10993
  }
10984
10994
  /**
10985
10995
  * Ensure authentication token is valid
@@ -19598,6 +19608,7 @@ const SDKContext = react.createContext(null);
19598
19608
  */
19599
19609
  const PersSDKProvider = ({ children, config }) => {
19600
19610
  const initializingRef = react.useRef(false);
19611
+ const eventUnsubscribeRef = react.useRef(null);
19601
19612
  // State refs for stable functions to read current values
19602
19613
  const sdkRef = react.useRef(null);
19603
19614
  const isInitializedRef = react.useRef(false);
@@ -19651,6 +19662,21 @@ const PersSDKProvider = ({ children, config }) => {
19651
19662
  };
19652
19663
  // Initialize PersSDK with platform-aware auth provider
19653
19664
  const sdkInstance = new PersSDK(httpClient, sdkConfig);
19665
+ // Subscribe to auth events BEFORE exposing SDK to consumers
19666
+ // This ensures no events are missed due to React timing
19667
+ eventUnsubscribeRef.current = sdkInstance.events.subscribe((event) => {
19668
+ if (event.type === 'session_restored' || event.type === 'login_success') {
19669
+ const details = event.details;
19670
+ if (details?.user) {
19671
+ setUser(details.user);
19672
+ setIsAuthenticated(true);
19673
+ }
19674
+ }
19675
+ if (event.type === 'logout_success' || event.type === 'session_restoration_failed' || event.code === 'AUTH_FAILED') {
19676
+ setUser(null);
19677
+ setIsAuthenticated(false);
19678
+ }
19679
+ }, { domains: ['authentication'], replay: true });
19654
19680
  setAuthProvider(authProvider);
19655
19681
  setSdk(sdkInstance);
19656
19682
  setIsInitialized(true);
@@ -19664,6 +19690,15 @@ const PersSDKProvider = ({ children, config }) => {
19664
19690
  initializingRef.current = false;
19665
19691
  }
19666
19692
  }, [isInitialized]);
19693
+ // Cleanup subscription on unmount
19694
+ react.useEffect(() => {
19695
+ return () => {
19696
+ if (eventUnsubscribeRef.current) {
19697
+ eventUnsubscribeRef.current();
19698
+ eventUnsubscribeRef.current = null;
19699
+ }
19700
+ };
19701
+ }, []);
19667
19702
  const setAuthenticationState = react.useCallback((user, isAuthenticated) => {
19668
19703
  setUser(user);
19669
19704
  setIsAuthenticated(isAuthenticated);
@@ -19710,32 +19745,7 @@ const PersSDKProvider = ({ children, config }) => {
19710
19745
  console.error('[PersSDK] Failed to restore session:', error);
19711
19746
  throw error;
19712
19747
  }
19713
- }, [setAuthenticationState]); // Depends on setAuthenticationState
19714
- // Listen for authentication events from core SDK
19715
- // Set up immediately when SDK is created (don't wait for isInitialized)
19716
- // to catch session_restored events that fire during SDK initialization
19717
- react.useEffect(() => {
19718
- if (!sdk)
19719
- return;
19720
- const unsubscribe = sdk.events.subscribe((event) => {
19721
- // Handle both session restoration and fresh login
19722
- if (event.type === 'session_restored' || event.type === 'login_success') {
19723
- const details = event.details;
19724
- if (details?.user) {
19725
- console.log(`[PersSDK] ${event.type}: syncing user state`);
19726
- setAuthenticationState(details.user, true);
19727
- }
19728
- }
19729
- // Session restoration failed or auth error - clear React state
19730
- if (event.type === 'session_restoration_failed' || event.code === 'AUTH_FAILED') {
19731
- console.log('[PersSDK] Authentication failed - clearing session');
19732
- setAuthenticationState(null, false);
19733
- }
19734
- }, { domains: ['authentication'], replay: true });
19735
- return () => {
19736
- unsubscribe();
19737
- };
19738
- }, [sdk, setAuthenticationState]);
19748
+ }, [setAuthenticationState]);
19739
19749
  // iOS/Android: Monitor app state and validate tokens when app becomes active
19740
19750
  react.useEffect(() => {
19741
19751
  if (!sdk || reactNative.Platform.OS === 'web') {
@@ -19743,7 +19753,6 @@ const PersSDKProvider = ({ children, config }) => {
19743
19753
  }
19744
19754
  const handleAppStateChange = async (nextAppState) => {
19745
19755
  if (nextAppState === 'active' && isAuthenticated) {
19746
- console.log('[PersSDK] App became active - validating tokens');
19747
19756
  try {
19748
19757
  // Trigger token validation when app resumes from background
19749
19758
  // This ensures tokens are checked after extended inactivity
@@ -19833,7 +19842,7 @@ const usePersSDK = () => {
19833
19842
  * ```
19834
19843
  */
19835
19844
  const useAuth = () => {
19836
- const { sdk, authProvider, isInitialized, isAuthenticated, user, setAuthenticationState, refreshUserData } = usePersSDK();
19845
+ const { sdk, authProvider, isInitialized, isAuthenticated, user, refreshUserData } = usePersSDK();
19837
19846
  /**
19838
19847
  * Authenticates a user with a JWT token
19839
19848
  *
@@ -19856,8 +19865,7 @@ const useAuth = () => {
19856
19865
  try {
19857
19866
  // Set token in auth provider
19858
19867
  await authProvider.setAccessToken(jwtToken);
19859
- // Perform login using the manager
19860
- // SDK emits login_success event with user data - PersSDKProvider handles state sync
19868
+ // Perform login - emits login_success event which Provider handles
19861
19869
  const result = await sdk.auth.loginWithToken(jwtToken, userType);
19862
19870
  return result;
19863
19871
  }
@@ -19883,8 +19891,7 @@ const useAuth = () => {
19883
19891
  throw new Error('SDK not initialized. Call initialize() first.');
19884
19892
  }
19885
19893
  try {
19886
- // Use the raw data login from the auth manager
19887
- // SDK emits login_success event with user data - PersSDKProvider handles state sync
19894
+ // Use the raw data login - emits login_success event which Provider handles
19888
19895
  const result = await sdk.auth.loginWithRawData(rawUserData);
19889
19896
  // Set token from result
19890
19897
  if (result.accessToken) {
@@ -19909,17 +19916,18 @@ const useAuth = () => {
19909
19916
  * ```
19910
19917
  */
19911
19918
  const logout = react.useCallback(async () => {
19919
+ if (!sdk) {
19920
+ throw new Error('SDK not initialized. Call initialize() first.');
19921
+ }
19912
19922
  try {
19913
- if (authProvider) {
19914
- await authProvider.clearTokens();
19915
- }
19916
- setAuthenticationState(null, false);
19923
+ // clearAuth emits logout_success event which Provider handles
19924
+ await sdk.auth.clearAuth();
19917
19925
  }
19918
19926
  catch (error) {
19919
19927
  console.error('Logout failed:', error);
19920
19928
  throw error;
19921
19929
  }
19922
- }, [authProvider, setAuthenticationState]);
19930
+ }, [sdk]);
19923
19931
  /**
19924
19932
  * Retrieves the current authenticated user data
19925
19933
  *
@@ -19993,9 +20001,9 @@ const useAuth = () => {
19993
20001
  if (!sdk) {
19994
20002
  throw new Error('SDK not initialized. Call initialize() first.');
19995
20003
  }
20004
+ // clearAuth emits logout_success event which Provider handles
19996
20005
  await sdk.auth.clearAuth();
19997
- setAuthenticationState(null, false);
19998
- }, [sdk, setAuthenticationState]);
20006
+ }, [sdk]);
19999
20007
  /**
20000
20008
  * Checks if the current authentication is valid (non-expired)
20001
20009
  *