@explorins/pers-sdk-react-native 2.3.6 → 2.3.8
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/LICENSE +21 -0
- package/README.md +1266 -1244
- package/dist/__tests__/PersSDKProvider.test.js +33 -0
- package/dist/hooks/useBusiness.d.ts +2 -2
- package/dist/hooks/useBusiness.d.ts.map +1 -1
- package/dist/hooks/useBusiness.js +2 -2
- package/dist/hooks/useIPFSUrl.d.ts +3 -3
- package/dist/hooks/useIPFSUrl.d.ts.map +1 -1
- package/dist/hooks/useIPFSUrl.js +7 -7
- package/dist/hooks/useTransactions.d.ts.map +1 -1
- package/dist/hooks/useTransactions.js +13 -5
- package/dist/hooks/useWeb3.js +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2412 -492
- package/dist/index.js.map +1 -1
- package/dist/providers/PersSDKProvider.d.ts +4 -0
- package/dist/providers/PersSDKProvider.d.ts.map +1 -1
- package/dist/providers/PersSDKProvider.js +14 -0
- package/dist/providers/react-native-http-client.d.ts +2 -0
- package/dist/providers/react-native-http-client.d.ts.map +1 -1
- package/dist/providers/react-native-http-client.js +5 -2
- package/dist/storage/rn-secure-storage.d.ts.map +1 -1
- package/dist/storage/rn-secure-storage.js +21 -8
- package/package.json +3 -3
- package/src/__tests__/PersSDKProvider.test.tsx +484 -420
- package/src/__tests__/setup.ts +40 -40
- package/src/hooks/index.ts +60 -60
- package/src/hooks/useAnalytics.ts +327 -327
- package/src/hooks/useAuth.ts +265 -265
- package/src/hooks/useBookings.ts +191 -191
- package/src/hooks/useBusiness.ts +251 -251
- package/src/hooks/useCampaigns.ts +500 -500
- package/src/hooks/useDonations.ts +66 -66
- package/src/hooks/useEvents.ts +372 -372
- package/src/hooks/useFiles.ts +155 -155
- package/src/hooks/useIPFSUrl.ts +192 -192
- package/src/hooks/usePurchases.ts +65 -65
- package/src/hooks/useRedemptions.ts +467 -467
- package/src/hooks/useTenants.ts +72 -72
- package/src/hooks/useTokenBalances.ts +385 -385
- package/src/hooks/useTokens.ts +313 -313
- package/src/hooks/useTransactionSigner.ts +611 -611
- package/src/hooks/useTransactions.ts +299 -290
- package/src/hooks/useTriggerSources.ts +301 -301
- package/src/hooks/useUserStatus.ts +61 -61
- package/src/hooks/useUsers.ts +167 -167
- package/src/hooks/useWeb3.ts +355 -355
- package/src/index.ts +777 -774
- package/src/polyfills/index.ts +160 -160
- package/src/polyfills/web3-polyfills.ts +97 -97
- package/src/providers/PersSDKProvider.tsx +393 -374
- package/src/providers/react-native-auth-provider.ts +110 -110
- package/src/providers/react-native-http-client.ts +150 -145
- package/src/providers/rn-dpop-provider.ts +190 -190
- package/src/storage/async-storage-token-storage.ts +185 -185
- package/src/storage/rn-secure-storage.ts +182 -170
- package/src/types/react-native-globals.d.ts +45 -45
- package/src/utils/index.ts +8 -8
- package/src/utils/platform-detection.ts +86 -86
- package/dist/index-CzLxqePZ.js +0 -7717
- package/dist/index-CzLxqePZ.js.map +0 -1
package/src/hooks/useAuth.ts
CHANGED
|
@@ -1,266 +1,266 @@
|
|
|
1
|
-
import { useCallback } from 'react';
|
|
2
|
-
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
-
import type { RawUserData } from '@explorins/pers-sdk/core';
|
|
4
|
-
import { AccountOwnerType } from '@explorins/pers-sdk/core';
|
|
5
|
-
import type { UserDTO, SessionAuthContextResponseDTO } from '@explorins/pers-sdk';
|
|
6
|
-
|
|
7
|
-
// Re-export RawUserData for convenience
|
|
8
|
-
export type { RawUserData };
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* React hook for authentication operations in the PERS SDK
|
|
12
|
-
*
|
|
13
|
-
* Provides methods for user login, logout, token management, and authentication state.
|
|
14
|
-
* Automatically manages authentication state and user data synchronization.
|
|
15
|
-
*
|
|
16
|
-
* @returns Authentication hook with methods and state
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* function LoginComponent() {
|
|
21
|
-
* const { login, logout, isAuthenticated, user } = useAuth();
|
|
22
|
-
*
|
|
23
|
-
* const handleLogin = async (token: string) => {
|
|
24
|
-
* try {
|
|
25
|
-
* await login(token, 'user');
|
|
26
|
-
* console.log('Login successful');
|
|
27
|
-
* } catch (error) {
|
|
28
|
-
* console.error('Login failed:', error);
|
|
29
|
-
* }
|
|
30
|
-
* };
|
|
31
|
-
*
|
|
32
|
-
* return (
|
|
33
|
-
* <div>
|
|
34
|
-
* {isAuthenticated ? (
|
|
35
|
-
* <div>
|
|
36
|
-
* <p>Welcome, {user?.name}</p>
|
|
37
|
-
* <button onClick={logout}>Logout</button>
|
|
38
|
-
* </div>
|
|
39
|
-
* ) : (
|
|
40
|
-
* <button onClick={() => handleLogin('jwt-token')}>Login</button>
|
|
41
|
-
* )}
|
|
42
|
-
* </div>
|
|
43
|
-
* );
|
|
44
|
-
* }
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export const useAuth = () => {
|
|
48
|
-
const {
|
|
49
|
-
sdk,
|
|
50
|
-
authProvider,
|
|
51
|
-
isInitialized,
|
|
52
|
-
isAuthenticated,
|
|
53
|
-
user,
|
|
54
|
-
refreshUserData
|
|
55
|
-
} = usePersSDK();
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Authenticates a user with a JWT token
|
|
59
|
-
*
|
|
60
|
-
* @param jwtToken - The JWT token to authenticate with
|
|
61
|
-
* @param userType - The type of user (AccountOwnerType), defaults to USER
|
|
62
|
-
* @returns Promise resolving to session auth context with user data
|
|
63
|
-
* @throws Error if SDK is not initialized or authentication fails
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```typescript
|
|
67
|
-
* const { login } = useAuth();
|
|
68
|
-
* const result = await login(jwtToken, AccountOwnerType.USER);
|
|
69
|
-
* console.log('Logged in user:', result.user);
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
const login = useCallback(async (jwtToken: string, userType: AccountOwnerType = AccountOwnerType.USER): Promise<SessionAuthContextResponseDTO> => {
|
|
73
|
-
if (!sdk || !authProvider) {
|
|
74
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
// Set token in auth provider
|
|
79
|
-
await authProvider.setAccessToken(jwtToken);
|
|
80
|
-
|
|
81
|
-
// Perform login - emits login_success event which Provider handles
|
|
82
|
-
const result = await sdk.auth.loginWithToken(jwtToken, userType);
|
|
83
|
-
|
|
84
|
-
return result;
|
|
85
|
-
} catch (error) {
|
|
86
|
-
console.error('Login failed:', error);
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
89
|
-
}, [sdk, authProvider]);
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Logs in a user using raw user data (direct authentication bypass)
|
|
93
|
-
*
|
|
94
|
-
* @param rawUserData - Raw user data for direct authentication
|
|
95
|
-
* @throws Error if SDK is not initialized or authentication fails
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```typescript
|
|
99
|
-
* const { loginWithRawData } = useAuth();
|
|
100
|
-
* await loginWithRawData({ userId: '123', email: 'user@example.com' });
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
const loginWithRawData = useCallback(async (rawUserData: RawUserData): Promise<void> => {
|
|
104
|
-
if (!sdk || !authProvider) {
|
|
105
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
// Use the raw data login - emits login_success event which Provider handles
|
|
110
|
-
const result = await sdk.auth.loginWithRawData(rawUserData);
|
|
111
|
-
|
|
112
|
-
// Set token from result
|
|
113
|
-
if (result.accessToken) {
|
|
114
|
-
await authProvider.setAccessToken(result.accessToken);
|
|
115
|
-
}
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('Raw data login failed:', error);
|
|
118
|
-
throw error;
|
|
119
|
-
}
|
|
120
|
-
}, [sdk, authProvider]);
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Logs out the current user and clears authentication state
|
|
124
|
-
*
|
|
125
|
-
* @throws Error if logout process fails
|
|
126
|
-
*
|
|
127
|
-
* @example
|
|
128
|
-
* ```typescript
|
|
129
|
-
* const { logout } = useAuth();
|
|
130
|
-
* await logout();
|
|
131
|
-
* console.log('User logged out');
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
const logout = useCallback(async (): Promise<void> => {
|
|
135
|
-
if (!sdk) {
|
|
136
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
try {
|
|
140
|
-
// clearAuth emits logout_success event which Provider handles
|
|
141
|
-
await sdk.auth.clearAuth();
|
|
142
|
-
} catch (error) {
|
|
143
|
-
console.error('Logout failed:', error);
|
|
144
|
-
throw error;
|
|
145
|
-
}
|
|
146
|
-
}, [sdk]);
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Retrieves the current authenticated user data
|
|
150
|
-
*
|
|
151
|
-
* @returns Promise resolving to current user data
|
|
152
|
-
* @throws Error if SDK is not initialized
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* const { getCurrentUser } = useAuth();
|
|
157
|
-
* const user = await getCurrentUser();
|
|
158
|
-
* console.log('Current user:', user.email);
|
|
159
|
-
* ```
|
|
160
|
-
*/
|
|
161
|
-
const getCurrentUser = useCallback(async (): Promise<UserDTO> => {
|
|
162
|
-
if (!sdk) {
|
|
163
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
164
|
-
}
|
|
165
|
-
return sdk.auth.getCurrentUser();
|
|
166
|
-
}, [sdk]);
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Checks if the current session is authenticated
|
|
170
|
-
*
|
|
171
|
-
* @returns Promise resolving to authentication status
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* ```typescript
|
|
175
|
-
* const { checkIsAuthenticated } = useAuth();
|
|
176
|
-
* const isAuth = await checkIsAuthenticated();
|
|
177
|
-
* console.log('Is authenticated:', isAuth);
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
const checkIsAuthenticated = useCallback(async (): Promise<boolean> => {
|
|
181
|
-
if (!sdk) {
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
return sdk.auth.isAuthenticated();
|
|
185
|
-
}, [sdk]);
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Refreshes authentication tokens
|
|
189
|
-
*
|
|
190
|
-
* @param refreshToken - Optional refresh token to use
|
|
191
|
-
* @returns Promise resolving to new session auth response
|
|
192
|
-
* @throws Error if SDK is not initialized
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```typescript
|
|
196
|
-
* const { refreshTokens } = useAuth();
|
|
197
|
-
* const tokens = await refreshTokens();
|
|
198
|
-
* console.log('Tokens refreshed:', tokens);
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
|
-
const refreshTokens = useCallback(async (refreshToken?: string) => {
|
|
202
|
-
if (!sdk) {
|
|
203
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
204
|
-
}
|
|
205
|
-
return sdk.auth.refreshTokens(refreshToken);
|
|
206
|
-
}, [sdk]);
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Clears all authentication data and resets auth state
|
|
210
|
-
*
|
|
211
|
-
* @throws Error if SDK is not initialized
|
|
212
|
-
*
|
|
213
|
-
* @example
|
|
214
|
-
* ```typescript
|
|
215
|
-
* const { clearAuth } = useAuth();
|
|
216
|
-
* await clearAuth();
|
|
217
|
-
* console.log('Auth data cleared');
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
|
-
const clearAuth = useCallback(async (): Promise<void> => {
|
|
221
|
-
if (!sdk) {
|
|
222
|
-
throw new Error('SDK not initialized. Call initialize() first.');
|
|
223
|
-
}
|
|
224
|
-
// clearAuth emits logout_success event which Provider handles
|
|
225
|
-
await sdk.auth.clearAuth();
|
|
226
|
-
}, [sdk]);
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Checks if the current authentication is valid (non-expired)
|
|
230
|
-
*
|
|
231
|
-
* @returns Boolean indicating if auth is valid
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* ```typescript
|
|
235
|
-
* const { hasValidAuth } = useAuth();
|
|
236
|
-
* const isValid = hasValidAuth();
|
|
237
|
-
* console.log('Auth is valid:', isValid);
|
|
238
|
-
* ```
|
|
239
|
-
*/
|
|
240
|
-
const hasValidAuth = useCallback(async (): Promise<boolean> => {
|
|
241
|
-
if (!sdk) {
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
return await sdk.auth.hasValidAuth();
|
|
245
|
-
}, [sdk]);
|
|
246
|
-
|
|
247
|
-
return {
|
|
248
|
-
// State
|
|
249
|
-
isInitialized,
|
|
250
|
-
isAuthenticated,
|
|
251
|
-
user,
|
|
252
|
-
|
|
253
|
-
// Methods
|
|
254
|
-
login,
|
|
255
|
-
loginWithRawData,
|
|
256
|
-
logout,
|
|
257
|
-
refreshUserData,
|
|
258
|
-
getCurrentUser,
|
|
259
|
-
checkIsAuthenticated,
|
|
260
|
-
refreshTokens,
|
|
261
|
-
clearAuth,
|
|
262
|
-
hasValidAuth,
|
|
263
|
-
};
|
|
264
|
-
};
|
|
265
|
-
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { RawUserData } from '@explorins/pers-sdk/core';
|
|
4
|
+
import { AccountOwnerType } from '@explorins/pers-sdk/core';
|
|
5
|
+
import type { UserDTO, SessionAuthContextResponseDTO } from '@explorins/pers-sdk';
|
|
6
|
+
|
|
7
|
+
// Re-export RawUserData for convenience
|
|
8
|
+
export type { RawUserData };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* React hook for authentication operations in the PERS SDK
|
|
12
|
+
*
|
|
13
|
+
* Provides methods for user login, logout, token management, and authentication state.
|
|
14
|
+
* Automatically manages authentication state and user data synchronization.
|
|
15
|
+
*
|
|
16
|
+
* @returns Authentication hook with methods and state
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* function LoginComponent() {
|
|
21
|
+
* const { login, logout, isAuthenticated, user } = useAuth();
|
|
22
|
+
*
|
|
23
|
+
* const handleLogin = async (token: string) => {
|
|
24
|
+
* try {
|
|
25
|
+
* await login(token, 'user');
|
|
26
|
+
* console.log('Login successful');
|
|
27
|
+
* } catch (error) {
|
|
28
|
+
* console.error('Login failed:', error);
|
|
29
|
+
* }
|
|
30
|
+
* };
|
|
31
|
+
*
|
|
32
|
+
* return (
|
|
33
|
+
* <div>
|
|
34
|
+
* {isAuthenticated ? (
|
|
35
|
+
* <div>
|
|
36
|
+
* <p>Welcome, {user?.name}</p>
|
|
37
|
+
* <button onClick={logout}>Logout</button>
|
|
38
|
+
* </div>
|
|
39
|
+
* ) : (
|
|
40
|
+
* <button onClick={() => handleLogin('jwt-token')}>Login</button>
|
|
41
|
+
* )}
|
|
42
|
+
* </div>
|
|
43
|
+
* );
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export const useAuth = () => {
|
|
48
|
+
const {
|
|
49
|
+
sdk,
|
|
50
|
+
authProvider,
|
|
51
|
+
isInitialized,
|
|
52
|
+
isAuthenticated,
|
|
53
|
+
user,
|
|
54
|
+
refreshUserData
|
|
55
|
+
} = usePersSDK();
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Authenticates a user with a JWT token
|
|
59
|
+
*
|
|
60
|
+
* @param jwtToken - The JWT token to authenticate with
|
|
61
|
+
* @param userType - The type of user (AccountOwnerType), defaults to USER
|
|
62
|
+
* @returns Promise resolving to session auth context with user data
|
|
63
|
+
* @throws Error if SDK is not initialized or authentication fails
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const { login } = useAuth();
|
|
68
|
+
* const result = await login(jwtToken, AccountOwnerType.USER);
|
|
69
|
+
* console.log('Logged in user:', result.user);
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
const login = useCallback(async (jwtToken: string, userType: AccountOwnerType = AccountOwnerType.USER): Promise<SessionAuthContextResponseDTO> => {
|
|
73
|
+
if (!sdk || !authProvider) {
|
|
74
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
// Set token in auth provider
|
|
79
|
+
await authProvider.setAccessToken(jwtToken);
|
|
80
|
+
|
|
81
|
+
// Perform login - emits login_success event which Provider handles
|
|
82
|
+
const result = await sdk.auth.loginWithToken(jwtToken, userType);
|
|
83
|
+
|
|
84
|
+
return result;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('Login failed:', error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}, [sdk, authProvider]);
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Logs in a user using raw user data (direct authentication bypass)
|
|
93
|
+
*
|
|
94
|
+
* @param rawUserData - Raw user data for direct authentication
|
|
95
|
+
* @throws Error if SDK is not initialized or authentication fails
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const { loginWithRawData } = useAuth();
|
|
100
|
+
* await loginWithRawData({ userId: '123', email: 'user@example.com' });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
const loginWithRawData = useCallback(async (rawUserData: RawUserData): Promise<void> => {
|
|
104
|
+
if (!sdk || !authProvider) {
|
|
105
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
// Use the raw data login - emits login_success event which Provider handles
|
|
110
|
+
const result = await sdk.auth.loginWithRawData(rawUserData);
|
|
111
|
+
|
|
112
|
+
// Set token from result
|
|
113
|
+
if (result.accessToken) {
|
|
114
|
+
await authProvider.setAccessToken(result.accessToken);
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error('Raw data login failed:', error);
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}, [sdk, authProvider]);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Logs out the current user and clears authentication state
|
|
124
|
+
*
|
|
125
|
+
* @throws Error if logout process fails
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const { logout } = useAuth();
|
|
130
|
+
* await logout();
|
|
131
|
+
* console.log('User logged out');
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
const logout = useCallback(async (): Promise<void> => {
|
|
135
|
+
if (!sdk) {
|
|
136
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
// clearAuth emits logout_success event which Provider handles
|
|
141
|
+
await sdk.auth.clearAuth();
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error('Logout failed:', error);
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}, [sdk]);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Retrieves the current authenticated user data
|
|
150
|
+
*
|
|
151
|
+
* @returns Promise resolving to current user data
|
|
152
|
+
* @throws Error if SDK is not initialized
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const { getCurrentUser } = useAuth();
|
|
157
|
+
* const user = await getCurrentUser();
|
|
158
|
+
* console.log('Current user:', user.email);
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
const getCurrentUser = useCallback(async (): Promise<UserDTO> => {
|
|
162
|
+
if (!sdk) {
|
|
163
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
164
|
+
}
|
|
165
|
+
return sdk.auth.getCurrentUser();
|
|
166
|
+
}, [sdk]);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Checks if the current session is authenticated
|
|
170
|
+
*
|
|
171
|
+
* @returns Promise resolving to authentication status
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const { checkIsAuthenticated } = useAuth();
|
|
176
|
+
* const isAuth = await checkIsAuthenticated();
|
|
177
|
+
* console.log('Is authenticated:', isAuth);
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
const checkIsAuthenticated = useCallback(async (): Promise<boolean> => {
|
|
181
|
+
if (!sdk) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
return sdk.auth.isAuthenticated();
|
|
185
|
+
}, [sdk]);
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Refreshes authentication tokens
|
|
189
|
+
*
|
|
190
|
+
* @param refreshToken - Optional refresh token to use
|
|
191
|
+
* @returns Promise resolving to new session auth response
|
|
192
|
+
* @throws Error if SDK is not initialized
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const { refreshTokens } = useAuth();
|
|
197
|
+
* const tokens = await refreshTokens();
|
|
198
|
+
* console.log('Tokens refreshed:', tokens);
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
const refreshTokens = useCallback(async (refreshToken?: string) => {
|
|
202
|
+
if (!sdk) {
|
|
203
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
204
|
+
}
|
|
205
|
+
return sdk.auth.refreshTokens(refreshToken);
|
|
206
|
+
}, [sdk]);
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Clears all authentication data and resets auth state
|
|
210
|
+
*
|
|
211
|
+
* @throws Error if SDK is not initialized
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* const { clearAuth } = useAuth();
|
|
216
|
+
* await clearAuth();
|
|
217
|
+
* console.log('Auth data cleared');
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
const clearAuth = useCallback(async (): Promise<void> => {
|
|
221
|
+
if (!sdk) {
|
|
222
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
223
|
+
}
|
|
224
|
+
// clearAuth emits logout_success event which Provider handles
|
|
225
|
+
await sdk.auth.clearAuth();
|
|
226
|
+
}, [sdk]);
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Checks if the current authentication is valid (non-expired)
|
|
230
|
+
*
|
|
231
|
+
* @returns Boolean indicating if auth is valid
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```typescript
|
|
235
|
+
* const { hasValidAuth } = useAuth();
|
|
236
|
+
* const isValid = hasValidAuth();
|
|
237
|
+
* console.log('Auth is valid:', isValid);
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
const hasValidAuth = useCallback(async (): Promise<boolean> => {
|
|
241
|
+
if (!sdk) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
return await sdk.auth.hasValidAuth();
|
|
245
|
+
}, [sdk]);
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
// State
|
|
249
|
+
isInitialized,
|
|
250
|
+
isAuthenticated,
|
|
251
|
+
user,
|
|
252
|
+
|
|
253
|
+
// Methods
|
|
254
|
+
login,
|
|
255
|
+
loginWithRawData,
|
|
256
|
+
logout,
|
|
257
|
+
refreshUserData,
|
|
258
|
+
getCurrentUser,
|
|
259
|
+
checkIsAuthenticated,
|
|
260
|
+
refreshTokens,
|
|
261
|
+
clearAuth,
|
|
262
|
+
hasValidAuth,
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
|
|
266
266
|
export type AuthHook = ReturnType<typeof useAuth>;
|