@explorins/pers-sdk-react-native 1.3.2 → 1.5.2

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.
Files changed (94) hide show
  1. package/README.md +276 -123
  2. package/dist/hooks/index.d.ts +10 -0
  3. package/dist/hooks/index.d.ts.map +1 -1
  4. package/dist/hooks/index.js +8 -0
  5. package/dist/hooks/useAnalytics.d.ts +96 -0
  6. package/dist/hooks/useAnalytics.d.ts.map +1 -0
  7. package/dist/hooks/useAnalytics.js +73 -0
  8. package/dist/hooks/useAuth.d.ts +52 -7
  9. package/dist/hooks/useAuth.d.ts.map +1 -1
  10. package/dist/hooks/useAuth.js +250 -3
  11. package/dist/hooks/useBusiness.d.ts +45 -3
  12. package/dist/hooks/useBusiness.d.ts.map +1 -1
  13. package/dist/hooks/useBusiness.js +182 -28
  14. package/dist/hooks/useCampaigns.d.ts +11 -4
  15. package/dist/hooks/useCampaigns.d.ts.map +1 -1
  16. package/dist/hooks/useCampaigns.js +101 -39
  17. package/dist/hooks/useDonations.d.ts +33 -0
  18. package/dist/hooks/useDonations.d.ts.map +1 -0
  19. package/dist/hooks/useDonations.js +62 -0
  20. package/dist/hooks/useFiles.d.ts +50 -0
  21. package/dist/hooks/useFiles.d.ts.map +1 -0
  22. package/dist/hooks/useFiles.js +140 -0
  23. package/dist/hooks/usePurchases.d.ts +9 -0
  24. package/dist/hooks/usePurchases.d.ts.map +1 -0
  25. package/dist/hooks/usePurchases.js +56 -0
  26. package/dist/hooks/useRedemptions.d.ts +10 -5
  27. package/dist/hooks/useRedemptions.d.ts.map +1 -1
  28. package/dist/hooks/useRedemptions.js +118 -66
  29. package/dist/hooks/useTenants.d.ts +10 -0
  30. package/dist/hooks/useTenants.d.ts.map +1 -0
  31. package/dist/hooks/useTenants.js +68 -0
  32. package/dist/hooks/useTokens.d.ts +36 -2
  33. package/dist/hooks/useTokens.d.ts.map +1 -1
  34. package/dist/hooks/useTokens.js +180 -19
  35. package/dist/hooks/useTransactionSigner.d.ts +73 -0
  36. package/dist/hooks/useTransactionSigner.d.ts.map +1 -0
  37. package/dist/hooks/useTransactionSigner.js +216 -0
  38. package/dist/hooks/useTransactions.d.ts +45 -3
  39. package/dist/hooks/useTransactions.d.ts.map +1 -1
  40. package/dist/hooks/useTransactions.js +149 -39
  41. package/dist/hooks/useUserStatus.d.ts +9 -0
  42. package/dist/hooks/useUserStatus.d.ts.map +1 -0
  43. package/dist/hooks/useUserStatus.js +57 -0
  44. package/dist/hooks/useUsers.d.ts +17 -0
  45. package/dist/hooks/useUsers.d.ts.map +1 -0
  46. package/dist/hooks/useUsers.js +120 -0
  47. package/dist/hooks/useWeb3.d.ts +69 -6
  48. package/dist/hooks/useWeb3.d.ts.map +1 -1
  49. package/dist/hooks/useWeb3.js +177 -17
  50. package/dist/index.d.ts +6 -4
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +31540 -794
  53. package/dist/index.js.map +1 -1
  54. package/dist/polyfills/index.d.ts +1 -1
  55. package/dist/polyfills/index.d.ts.map +1 -1
  56. package/dist/polyfills/index.js +155 -9
  57. package/dist/polyfills/web3-polyfills.d.ts +7 -0
  58. package/dist/polyfills/web3-polyfills.d.ts.map +1 -0
  59. package/dist/polyfills/web3-polyfills.js +85 -0
  60. package/dist/providers/PersSDKProvider.d.ts +28 -18
  61. package/dist/providers/PersSDKProvider.d.ts.map +1 -1
  62. package/dist/providers/PersSDKProvider.js +63 -129
  63. package/dist/providers/react-native-auth-provider.d.ts +19 -64
  64. package/dist/providers/react-native-auth-provider.d.ts.map +1 -1
  65. package/dist/providers/react-native-auth-provider.js +57 -175
  66. package/package.json +177 -157
  67. package/src/hooks/index.ts +13 -1
  68. package/src/hooks/useAnalytics.ts +150 -0
  69. package/src/hooks/useAuth.ts +286 -27
  70. package/src/hooks/useBusiness.ts +193 -30
  71. package/src/hooks/useCampaigns.ts +121 -43
  72. package/src/hooks/useDonations.ts +68 -0
  73. package/src/hooks/useFiles.ts +160 -0
  74. package/src/hooks/usePurchases.ts +69 -0
  75. package/src/hooks/useRedemptions.ts +135 -68
  76. package/src/hooks/useTenants.ts +77 -0
  77. package/src/hooks/useTokens.ts +189 -21
  78. package/src/hooks/useTransactionSigner.ts +287 -0
  79. package/src/hooks/useTransactions.ts +163 -41
  80. package/src/hooks/useUserStatus.ts +65 -0
  81. package/src/hooks/useUsers.ts +133 -0
  82. package/src/hooks/useWeb3.ts +201 -21
  83. package/src/index.ts +22 -17
  84. package/src/polyfills/index.ts +163 -10
  85. package/src/polyfills/web3-polyfills.ts +98 -0
  86. package/src/providers/PersSDKProvider.tsx +114 -178
  87. package/src/providers/react-native-auth-provider.ts +65 -208
  88. package/dist/index.esm.js +0 -1057
  89. package/dist/index.esm.js.map +0 -1
  90. package/dist/polyfills/index.simple.d.ts +0 -2
  91. package/dist/polyfills/index.simple.d.ts.map +0 -1
  92. package/dist/polyfills/index.simple.js +0 -17
  93. package/src/polyfills/index.simple.ts +0 -22
  94. package/src/types/external-modules.d.ts +0 -13
@@ -1,26 +1,12 @@
1
1
  import type { PersAuthProvider } from '@explorins/pers-sdk/core';
2
2
  import AsyncStorage from '@react-native-async-storage/async-storage';
3
3
 
4
- /**
5
- * Storage strategy for different types of data
6
- */
7
- export enum StorageStrategy {
8
- /** Use AsyncStorage for general app data */
9
- ASYNC_STORAGE = 'async_storage',
10
- /** Use Keychain for sensitive tokens (when available) */
11
- KEYCHAIN = 'keychain',
12
- /** Use in-memory storage (fallback) */
13
- MEMORY = 'memory'
14
- }
15
-
16
4
  /**
17
5
  * Configuration options for React Native Auth Provider
18
6
  */
19
7
  export interface ReactNativeAuthConfig {
20
- /** Storage strategy for access tokens */
21
- accessTokenStrategy?: StorageStrategy;
22
- /** Storage strategy for refresh tokens */
23
- refreshTokenStrategy?: StorageStrategy;
8
+ /** Use secure storage (Keychain) for tokens when available */
9
+ useSecureStorage?: boolean;
24
10
  /** Custom key prefix for storage */
25
11
  keyPrefix?: string;
26
12
  /** Enable debug logging */
@@ -28,26 +14,21 @@ export interface ReactNativeAuthConfig {
28
14
  }
29
15
 
30
16
  /**
31
- * Base React Native Authentication Provider
32
- *
33
- * This serves as a blueprint for native implementations and can be extended
34
- * by consuming applications to integrate with their specific auth systems.
17
+ * React Native Authentication Provider
35
18
  *
36
- * Features:
37
- * - Multiple storage strategies (AsyncStorage, Keychain, Memory)
38
- * - Configurable security levels per token type
39
- * - Cross-platform compatibility (iOS/Android/Web)
40
- * - Blueprint for native iOS/Android implementations
19
+ * Simple, unified implementation for React Native apps with:
20
+ * - AsyncStorage for basic token storage
21
+ * - Keychain integration for secure storage (when available)
22
+ * - Automatic fallback to AsyncStorage if Keychain is unavailable
23
+ * - Cross-platform compatibility (iOS/Android/Expo)
41
24
  */
42
- export abstract class BaseReactNativeAuthProvider implements PersAuthProvider {
43
- abstract readonly authType: 'admin' | 'user';
44
- abstract getProjectKey(): Promise<string | null>;
45
- abstract onTokenExpired(): Promise<void>;
46
-
47
- protected readonly config: Required<ReactNativeAuthConfig>;
48
- protected readonly ACCESS_TOKEN_KEY: string;
49
- protected readonly REFRESH_TOKEN_KEY: string;
50
- protected readonly PROJECT_KEY_KEY: string;
25
+ export class ReactNativeAuthProvider implements PersAuthProvider {
26
+ readonly authType: 'user' = 'user';
27
+
28
+ private readonly config: Required<ReactNativeAuthConfig>;
29
+ private readonly ACCESS_TOKEN_KEY: string;
30
+ private readonly REFRESH_TOKEN_KEY: string;
31
+ private readonly projectKey: string;
51
32
 
52
33
  // In-memory fallback storage
53
34
  private memoryStorage: Map<string, string> = new Map();
@@ -56,15 +37,15 @@ export abstract class BaseReactNativeAuthProvider implements PersAuthProvider {
56
37
  projectKey: string,
57
38
  config: ReactNativeAuthConfig = {}
58
39
  ) {
59
- // Validate projectKey
60
40
  if (!projectKey || typeof projectKey !== 'string') {
61
41
  throw new Error('ReactNativeAuthProvider: projectKey is required and must be a string');
62
42
  }
63
43
 
44
+ this.projectKey = projectKey;
45
+
64
46
  // Set default configuration
65
47
  this.config = {
66
- accessTokenStrategy: StorageStrategy.ASYNC_STORAGE,
67
- refreshTokenStrategy: StorageStrategy.KEYCHAIN,
48
+ useSecureStorage: true, // Default to secure storage when available
68
49
  keyPrefix: `pers_${projectKey.slice(0, 8)}`,
69
50
  debug: false,
70
51
  ...config
@@ -73,168 +54,116 @@ export abstract class BaseReactNativeAuthProvider implements PersAuthProvider {
73
54
  // Create storage keys
74
55
  this.ACCESS_TOKEN_KEY = `${this.config.keyPrefix}_access_token`;
75
56
  this.REFRESH_TOKEN_KEY = `${this.config.keyPrefix}_refresh_token`;
76
- this.PROJECT_KEY_KEY = `${this.config.keyPrefix}_project_key`;
77
57
 
78
58
  if (this.config.debug) {
79
- console.log('🔧 ReactNativeAuthProvider initialized with config:', this.config);
59
+ console.log('ReactNativeAuthProvider initialized:', {
60
+ projectKey: projectKey.slice(0, 8) + '...',
61
+ useSecureStorage: this.config.useSecureStorage
62
+ });
80
63
  }
81
64
  }
82
65
 
83
- // Core token management methods
66
+ async getProjectKey(): Promise<string | null> {
67
+ return this.projectKey;
68
+ }
69
+
84
70
  async getToken(): Promise<string | null> {
85
- return this.getStoredValue(this.ACCESS_TOKEN_KEY, this.config.accessTokenStrategy);
71
+ return this.getStoredValue(this.ACCESS_TOKEN_KEY);
86
72
  }
87
73
 
88
74
  async setAccessToken(token: string): Promise<void> {
89
- await this.storeValue(this.ACCESS_TOKEN_KEY, token, this.config.accessTokenStrategy);
75
+ await this.storeValue(this.ACCESS_TOKEN_KEY, token);
90
76
  if (this.config.debug) {
91
- console.log('Access token stored securely');
77
+ console.log('Access token stored securely');
92
78
  }
93
79
  }
94
80
 
95
81
  async setRefreshToken(token: string): Promise<void> {
96
- await this.storeValue(this.REFRESH_TOKEN_KEY, token, this.config.refreshTokenStrategy);
82
+ await this.storeValue(this.REFRESH_TOKEN_KEY, token);
97
83
  if (this.config.debug) {
98
- console.log('Refresh token stored securely');
84
+ console.log('Refresh token stored securely');
99
85
  }
100
86
  }
101
87
 
102
88
  async getRefreshToken(): Promise<string | null> {
103
- return this.getStoredValue(this.REFRESH_TOKEN_KEY, this.config.refreshTokenStrategy);
89
+ return this.getStoredValue(this.REFRESH_TOKEN_KEY);
104
90
  }
105
91
 
106
92
  async clearTokens(): Promise<void> {
107
93
  await Promise.all([
108
- this.removeStoredValue(this.ACCESS_TOKEN_KEY, this.config.accessTokenStrategy),
109
- this.removeStoredValue(this.REFRESH_TOKEN_KEY, this.config.refreshTokenStrategy)
94
+ this.removeStoredValue(this.ACCESS_TOKEN_KEY),
95
+ this.removeStoredValue(this.REFRESH_TOKEN_KEY)
110
96
  ]);
111
97
 
112
98
  if (this.config.debug) {
113
- console.log('All tokens cleared from storage');
99
+ console.log('All tokens cleared from storage');
100
+ }
101
+ }
102
+
103
+ async onTokenExpired(): Promise<void> {
104
+ if (this.config.debug) {
105
+ console.warn('ReactNativeAuthProvider: Token expired - implement refresh logic in your app');
114
106
  }
107
+ // Clear expired tokens
108
+ await this.clearTokens();
115
109
  }
116
110
 
117
111
  // Token validation methods
118
112
  hasValidToken(): boolean {
119
- // Synchronous check - for async check use hasValidTokenAsync()
120
113
  return true; // Actual validation happens in getToken()
121
114
  }
122
115
 
123
116
  hasRefreshToken(): boolean {
124
- // Synchronous check - for async check use hasRefreshTokenAsync()
125
117
  return true; // Actual validation happens in getRefreshToken()
126
118
  }
127
119
 
128
- // Async token validation methods
129
- async hasValidTokenAsync(): Promise<boolean> {
130
- const token = await this.getToken();
131
- return !!token;
132
- }
133
-
134
- async hasRefreshTokenAsync(): Promise<boolean> {
135
- const refreshToken = await this.getRefreshToken();
136
- return !!refreshToken;
137
- }
138
-
139
- // Legacy methods for backward compatibility
140
- setToken(token: string): void {
141
- this.setAccessToken(token).catch(error => {
142
- console.error('Legacy setToken failed:', error);
143
- });
144
- }
145
-
146
- clearToken(): void {
147
- this.clearTokens().catch(error => {
148
- console.error('Legacy clearToken failed:', error);
149
- });
150
- }
151
-
152
- // Protected storage implementation methods
153
- protected async storeValue(key: string, value: string, strategy: StorageStrategy): Promise<void> {
120
+ // Storage implementation methods
121
+ private async storeValue(key: string, value: string): Promise<void> {
154
122
  try {
155
- switch (strategy) {
156
- case StorageStrategy.KEYCHAIN:
157
- // Try Keychain first, fallback to AsyncStorage
158
- if (await this.isKeychainAvailable()) {
159
- await this.storeInKeychain(key, value);
160
- } else {
161
- await AsyncStorage.setItem(key, value);
162
- }
163
- break;
164
-
165
- case StorageStrategy.ASYNC_STORAGE:
166
- await AsyncStorage.setItem(key, value);
167
- break;
168
-
169
- case StorageStrategy.MEMORY:
170
- default:
171
- this.memoryStorage.set(key, value);
172
- break;
123
+ if (this.config.useSecureStorage && await this.isKeychainAvailable()) {
124
+ await this.storeInKeychain(key, value);
125
+ } else {
126
+ await AsyncStorage.setItem(key, value);
173
127
  }
174
128
  } catch (error) {
175
- console.error(`Failed to store value with strategy ${strategy}:`, error);
129
+ console.error(`Failed to store value for key ${key}:`, error);
176
130
  // Fallback to memory storage
177
131
  this.memoryStorage.set(key, value);
178
132
  }
179
133
  }
180
134
 
181
- protected async getStoredValue(key: string, strategy: StorageStrategy): Promise<string | null> {
135
+ private async getStoredValue(key: string): Promise<string | null> {
182
136
  try {
183
- switch (strategy) {
184
- case StorageStrategy.KEYCHAIN:
185
- // Try Keychain first, fallback to AsyncStorage
186
- if (await this.isKeychainAvailable()) {
187
- return await this.getFromKeychain(key);
188
- } else {
189
- return await AsyncStorage.getItem(key);
190
- }
191
-
192
- case StorageStrategy.ASYNC_STORAGE:
193
- return await AsyncStorage.getItem(key);
194
-
195
- case StorageStrategy.MEMORY:
196
- default:
197
- return this.memoryStorage.get(key) || null;
137
+ if (this.config.useSecureStorage && await this.isKeychainAvailable()) {
138
+ return await this.getFromKeychain(key);
139
+ } else {
140
+ return await AsyncStorage.getItem(key);
198
141
  }
199
142
  } catch (error) {
200
- console.warn(`Failed to get value with strategy ${strategy}:`, error);
143
+ console.warn(`Failed to get value for key ${key}:`, error);
201
144
  // Fallback to memory storage
202
145
  return this.memoryStorage.get(key) || null;
203
146
  }
204
147
  }
205
148
 
206
- protected async removeStoredValue(key: string, strategy: StorageStrategy): Promise<void> {
149
+ private async removeStoredValue(key: string): Promise<void> {
207
150
  try {
208
- switch (strategy) {
209
- case StorageStrategy.KEYCHAIN:
210
- // Try Keychain first, fallback to AsyncStorage
211
- if (await this.isKeychainAvailable()) {
212
- await this.removeFromKeychain(key);
213
- } else {
214
- await AsyncStorage.removeItem(key);
215
- }
216
- break;
217
-
218
- case StorageStrategy.ASYNC_STORAGE:
219
- await AsyncStorage.removeItem(key);
220
- break;
221
-
222
- case StorageStrategy.MEMORY:
223
- default:
224
- this.memoryStorage.delete(key);
225
- break;
151
+ if (this.config.useSecureStorage && await this.isKeychainAvailable()) {
152
+ await this.removeFromKeychain(key);
153
+ } else {
154
+ await AsyncStorage.removeItem(key);
226
155
  }
227
156
  } catch (error) {
228
- console.error(`Failed to remove value with strategy ${strategy}:`, error);
229
- // Ensure memory fallback is also cleared
230
- this.memoryStorage.delete(key);
157
+ console.error(`Failed to remove value for key ${key}:`, error);
231
158
  }
159
+
160
+ // Ensure memory fallback is also cleared
161
+ this.memoryStorage.delete(key);
232
162
  }
233
163
 
234
164
  // Keychain helper methods
235
165
  private async isKeychainAvailable(): Promise<boolean> {
236
166
  try {
237
- // Check if react-native-keychain is available
238
167
  const Keychain = require('react-native-keychain');
239
168
  return !!Keychain && typeof Keychain.setInternetCredentials === 'function';
240
169
  } catch {
@@ -257,76 +186,4 @@ export abstract class BaseReactNativeAuthProvider implements PersAuthProvider {
257
186
  const Keychain = require('react-native-keychain');
258
187
  await Keychain.resetInternetCredentials(key);
259
188
  }
260
- }
261
-
262
- /**
263
- * React Native Authentication Provider
264
- *
265
- * A ready-to-use implementation for basic use cases.
266
- * Uses AsyncStorage for access tokens and Keychain for refresh tokens (when available).
267
- */
268
- export class ReactNativeAuthProvider extends BaseReactNativeAuthProvider {
269
- readonly authType: 'user' = 'user';
270
-
271
- constructor(
272
- private projectKey: string,
273
- config: ReactNativeAuthConfig = {}
274
- ) {
275
- // Validate projectKey
276
- if (!projectKey || typeof projectKey !== 'string') {
277
- throw new Error('ReactNativeAuthProvider: projectKey is required and must be a string');
278
- }
279
-
280
- super(projectKey, {
281
- accessTokenStrategy: StorageStrategy.ASYNC_STORAGE,
282
- refreshTokenStrategy: StorageStrategy.KEYCHAIN,
283
- debug: false,
284
- ...config
285
- });
286
- }
287
-
288
- async getProjectKey(): Promise<string | null> {
289
- return this.projectKey;
290
- }
291
-
292
- async onTokenExpired(): Promise<void> {
293
- if (this.config.debug) {
294
- console.warn('ReactNativeAuthProvider: Token expired - no refresh logic implemented');
295
- }
296
- }
297
- }
298
-
299
- /**
300
- * Secure React Native Authentication Provider
301
- *
302
- * Maximum security implementation using Keychain for all sensitive data.
303
- */
304
- export class SecureReactNativeAuthProvider extends BaseReactNativeAuthProvider {
305
- readonly authType: 'user' = 'user';
306
-
307
- constructor(
308
- private projectKey: string,
309
- config: Omit<ReactNativeAuthConfig, 'accessTokenStrategy' | 'refreshTokenStrategy'> = {}
310
- ) {
311
- // Validate projectKey
312
- if (!projectKey || typeof projectKey !== 'string') {
313
- throw new Error('SecureReactNativeAuthProvider: projectKey is required and must be a string');
314
- }
315
-
316
- super(projectKey, {
317
- ...config,
318
- accessTokenStrategy: StorageStrategy.KEYCHAIN,
319
- refreshTokenStrategy: StorageStrategy.KEYCHAIN,
320
- });
321
- }
322
-
323
- async getProjectKey(): Promise<string | null> {
324
- return this.projectKey;
325
- }
326
-
327
- async onTokenExpired(): Promise<void> {
328
- if (this.config.debug) {
329
- console.warn('SecureReactNativeAuthProvider: Token expired - implement refresh logic');
330
- }
331
- }
332
189
  }