@explorins/pers-sdk-react-native 1.3.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.
- package/README.md +200 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useAuth.d.ts +12 -0
- package/dist/hooks/useAuth.d.ts.map +1 -0
- package/dist/hooks/useAuth.js +16 -0
- package/dist/hooks/useBusiness.d.ts +7 -0
- package/dist/hooks/useBusiness.d.ts.map +1 -0
- package/dist/hooks/useBusiness.js +64 -0
- package/dist/hooks/useCampaigns.d.ts +8 -0
- package/dist/hooks/useCampaigns.d.ts.map +1 -0
- package/dist/hooks/useCampaigns.js +90 -0
- package/dist/hooks/useRedemptions.d.ts +9 -0
- package/dist/hooks/useRedemptions.d.ts.map +1 -0
- package/dist/hooks/useRedemptions.js +121 -0
- package/dist/hooks/useTokens.d.ts +6 -0
- package/dist/hooks/useTokens.d.ts.map +1 -0
- package/dist/hooks/useTokens.js +48 -0
- package/dist/hooks/useTransactions.d.ts +7 -0
- package/dist/hooks/useTransactions.d.ts.map +1 -0
- package/dist/hooks/useTransactions.js +78 -0
- package/dist/hooks/useWeb3.d.ts +11 -0
- package/dist/hooks/useWeb3.d.ts.map +1 -0
- package/dist/hooks/useWeb3.js +63 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +1057 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +1077 -0
- package/dist/index.js.map +1 -0
- package/dist/polyfills/index.d.ts +3 -0
- package/dist/polyfills/index.d.ts.map +1 -0
- package/dist/polyfills/index.js +13 -0
- package/dist/polyfills/index.simple.d.ts +2 -0
- package/dist/polyfills/index.simple.d.ts.map +1 -0
- package/dist/polyfills/index.simple.js +17 -0
- package/dist/providers/PersSDKProvider.d.ts +29 -0
- package/dist/providers/PersSDKProvider.d.ts.map +1 -0
- package/dist/providers/PersSDKProvider.js +194 -0
- package/dist/providers/react-native-auth-provider.d.ts +92 -0
- package/dist/providers/react-native-auth-provider.d.ts.map +1 -0
- package/dist/providers/react-native-auth-provider.js +268 -0
- package/dist/providers/react-native-http-client.d.ts +29 -0
- package/dist/providers/react-native-http-client.d.ts.map +1 -0
- package/dist/providers/react-native-http-client.js +94 -0
- package/package.json +157 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useAuth.ts +43 -0
- package/src/hooks/useBusiness.ts +69 -0
- package/src/hooks/useCampaigns.ts +96 -0
- package/src/hooks/useRedemptions.ts +129 -0
- package/src/hooks/useTokens.ts +53 -0
- package/src/hooks/useTransactions.ts +85 -0
- package/src/hooks/useWeb3.ts +70 -0
- package/src/index.ts +51 -0
- package/src/polyfills/index.simple.ts +22 -0
- package/src/polyfills/index.ts +16 -0
- package/src/providers/PersSDKProvider.tsx +274 -0
- package/src/providers/react-native-auth-provider.ts +332 -0
- package/src/providers/react-native-http-client.ts +129 -0
- package/src/types/external-modules.d.ts +13 -0
- package/src/types/react-native-globals.d.ts +46 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
|
|
4
|
+
export const useTransactions = () => {
|
|
5
|
+
const { transactions, isInitialized, isAuthenticated } = usePersSDK();
|
|
6
|
+
|
|
7
|
+
if (!isAuthenticated && isInitialized) {
|
|
8
|
+
console.warn('SDK not authenticated. Some transaction operations may fail.');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const createTransaction = useCallback(async (request: any) => {
|
|
12
|
+
if (!isInitialized) {
|
|
13
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
14
|
+
}
|
|
15
|
+
if (!transactions?.createTransaction) {
|
|
16
|
+
throw new Error('createTransaction method not available');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
console.log('🔄 Creating transaction with request:', request);
|
|
21
|
+
const result = await transactions.createTransaction(request);
|
|
22
|
+
|
|
23
|
+
// React Native specific: Handle signature URLs
|
|
24
|
+
if (result?.actionable?.actionUrl) {
|
|
25
|
+
try {
|
|
26
|
+
const { Linking } = require('react-native');
|
|
27
|
+
console.log('🔗 Opening signature URL:', result.actionable.actionUrl);
|
|
28
|
+
await Linking.openURL(result.actionable.actionUrl);
|
|
29
|
+
} catch (linkingError) {
|
|
30
|
+
console.error('❌ Failed to open signature URL:', linkingError);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log('✅ Transaction created successfully:', result);
|
|
35
|
+
return result;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error('❌ Failed to create transaction:', error);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}, [transactions]);
|
|
41
|
+
|
|
42
|
+
const getTransactionById = useCallback(async (transactionId: string) => {
|
|
43
|
+
if (!isInitialized) {
|
|
44
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
45
|
+
}
|
|
46
|
+
if (!transactions?.getTransactionById) {
|
|
47
|
+
throw new Error('getTransactionById method not available');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const result = await transactions.getTransactionById(transactionId);
|
|
52
|
+
console.log('✅ Transaction fetched successfully:', result);
|
|
53
|
+
return result;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error('❌ Failed to fetch transaction:', error);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}, [transactions]);
|
|
59
|
+
|
|
60
|
+
const getTransactionHistory = useCallback(async (filters?: any) => {
|
|
61
|
+
if (!isInitialized) {
|
|
62
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
63
|
+
}
|
|
64
|
+
if (!transactions?.getTransactionHistory) {
|
|
65
|
+
console.warn('getTransactionHistory method not available');
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const result = await transactions.getTransactionHistory(filters);
|
|
71
|
+
console.log('✅ Transaction history fetched successfully:', result);
|
|
72
|
+
return result;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error('❌ Failed to fetch transaction history:', error);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}, [transactions]);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
createTransaction,
|
|
81
|
+
getTransactionById,
|
|
82
|
+
getTransactionHistory,
|
|
83
|
+
isAvailable: isInitialized && !!transactions,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
|
|
4
|
+
export const useWeb3 = () => {
|
|
5
|
+
const { web3, isInitialized, isAuthenticated, accountAddress } = usePersSDK();
|
|
6
|
+
|
|
7
|
+
if (!isAuthenticated && isInitialized) {
|
|
8
|
+
console.warn('SDK not authenticated. Some web3 operations may fail.');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const getTokenBalance = useCallback(async (request: any) => {
|
|
12
|
+
if (!isInitialized) {
|
|
13
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
14
|
+
}
|
|
15
|
+
if (!web3?.getTokenBalance) {
|
|
16
|
+
throw new Error('getTokenBalance method not available');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const result = await web3.getTokenBalance(request);
|
|
21
|
+
console.log('✅ Token balance fetched successfully:', result);
|
|
22
|
+
return result;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error('❌ Failed to fetch token balance:', error);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}, [web3]);
|
|
28
|
+
|
|
29
|
+
const getTokenCollection = useCallback(async (request: any) => {
|
|
30
|
+
if (!isInitialized) {
|
|
31
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
32
|
+
}
|
|
33
|
+
if (!web3?.getTokenCollection) {
|
|
34
|
+
console.warn('getTokenCollection method not available');
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const result = await web3.getTokenCollection(request);
|
|
40
|
+
console.log('✅ Token collection fetched successfully:', result);
|
|
41
|
+
return result;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error('❌ Failed to fetch token collection:', error);
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}, [web3]);
|
|
47
|
+
|
|
48
|
+
const getWalletInfo = useCallback(async () => {
|
|
49
|
+
if (!isInitialized) {
|
|
50
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
51
|
+
}
|
|
52
|
+
if (!accountAddress) {
|
|
53
|
+
console.warn('No account address available');
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
address: accountAddress,
|
|
59
|
+
isConnected: !!accountAddress,
|
|
60
|
+
};
|
|
61
|
+
}, [isInitialized, accountAddress]);
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
getTokenBalance,
|
|
65
|
+
getTokenCollection,
|
|
66
|
+
getWalletInfo,
|
|
67
|
+
accountAddress: isInitialized ? accountAddress : null,
|
|
68
|
+
isAvailable: isInitialized && !!web3,
|
|
69
|
+
};
|
|
70
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Initialize React Native polyfills automatically
|
|
2
|
+
import './polyfills';
|
|
3
|
+
|
|
4
|
+
// TEMPORARY: Minimal exports to isolate the .from() error
|
|
5
|
+
console.log('🔍 [DEBUG] Loading React Native SDK...');
|
|
6
|
+
|
|
7
|
+
// Export React Native specific providers (FIRST - break circular dependency)
|
|
8
|
+
export {
|
|
9
|
+
BaseReactNativeAuthProvider,
|
|
10
|
+
ReactNativeAuthProvider,
|
|
11
|
+
SecureReactNativeAuthProvider,
|
|
12
|
+
StorageStrategy,
|
|
13
|
+
type ReactNativeAuthConfig
|
|
14
|
+
} from './providers/react-native-auth-provider';
|
|
15
|
+
|
|
16
|
+
console.log('🔍 [DEBUG] Auth providers loaded successfully');
|
|
17
|
+
|
|
18
|
+
// Export Provider and Context directly (avoid circular import through hooks)
|
|
19
|
+
export {
|
|
20
|
+
PersSDKProvider,
|
|
21
|
+
usePersSDK,
|
|
22
|
+
type PersSDKConfig,
|
|
23
|
+
type PersSDKContext
|
|
24
|
+
} from './providers/PersSDKProvider';
|
|
25
|
+
|
|
26
|
+
// Export hooks (Primary Interface)
|
|
27
|
+
export {
|
|
28
|
+
useAuth,
|
|
29
|
+
useTokens,
|
|
30
|
+
useTransactions,
|
|
31
|
+
useBusiness,
|
|
32
|
+
useCampaigns,
|
|
33
|
+
useRedemptions,
|
|
34
|
+
useWeb3
|
|
35
|
+
} from './hooks';
|
|
36
|
+
|
|
37
|
+
console.log('🔍 [DEBUG] React Native SDK loaded with full exports including useWeb3');
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
ReactNativeHttpClient
|
|
41
|
+
} from './providers/react-native-http-client';
|
|
42
|
+
|
|
43
|
+
// Export polyfill utilities for advanced usage
|
|
44
|
+
export {
|
|
45
|
+
initializeReactNativePolyfills,
|
|
46
|
+
isReactNative
|
|
47
|
+
} from './polyfills';
|
|
48
|
+
|
|
49
|
+
// Note: For types and DTOs, import directly from @explorins/pers-shared
|
|
50
|
+
// This maintains clean separation and avoids circular dependencies
|
|
51
|
+
// Example: import { TokenDTO, BusinessDTO } from '@explorins/pers-shared';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
|
|
3
|
+
// Simple polyfills based on proven web project pattern
|
|
4
|
+
// Minimal setup that actually works
|
|
5
|
+
|
|
6
|
+
// Set up global environment
|
|
7
|
+
(global as any).global = global;
|
|
8
|
+
(global as any).Buffer = Buffer;
|
|
9
|
+
|
|
10
|
+
// Only add what's actually needed for React Native
|
|
11
|
+
if (typeof global !== 'undefined' && !global.process) {
|
|
12
|
+
global.process = { env: {} } as any;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Export initialization function for compatibility
|
|
16
|
+
export const initializeReactNativePolyfills = (): boolean => {
|
|
17
|
+
console.log('✅ [PERS SDK] Simple polyfills initialized');
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Auto-initialize
|
|
22
|
+
initializeReactNativePolyfills();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Minimal React Native environment setup
|
|
2
|
+
// No polyfills needed - modules are disabled at Metro level
|
|
3
|
+
|
|
4
|
+
// Simple initialization function for compatibility
|
|
5
|
+
export const initializeReactNativePolyfills = (): boolean => {
|
|
6
|
+
console.log('✅ [PERS SDK] React Native environment ready (no polyfills needed)');
|
|
7
|
+
return true;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// Platform detection
|
|
11
|
+
export const isReactNative = (): boolean => {
|
|
12
|
+
return typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Auto-initialize
|
|
16
|
+
initializeReactNativePolyfills();
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import React, { createContext, useContext, useEffect, useState, ReactNode, useCallback, useRef } from 'react';
|
|
2
|
+
import { PersApiClient } from '@explorins/pers-sdk/core';
|
|
3
|
+
import { TokenSDK } from '@explorins/pers-sdk/token';
|
|
4
|
+
import { createTransactionSDK } from '@explorins/pers-sdk/transaction';
|
|
5
|
+
import { createBusinessSDK } from '@explorins/pers-sdk/business';
|
|
6
|
+
import { createCampaignSDK } from '@explorins/pers-sdk/campaign';
|
|
7
|
+
import { createRedemptionSDK } from '@explorins/pers-sdk/redemption';
|
|
8
|
+
import { createWeb3SDK } from '@explorins/pers-sdk/web3';
|
|
9
|
+
import { ReactNativeHttpClient } from './react-native-http-client';
|
|
10
|
+
import { BaseReactNativeAuthProvider } from './react-native-auth-provider';
|
|
11
|
+
|
|
12
|
+
// Types for the SDK context
|
|
13
|
+
export interface PersSDKConfig {
|
|
14
|
+
apiProjectKey: string;
|
|
15
|
+
environment?: 'development' | 'staging' | 'production';
|
|
16
|
+
timeout?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PersSDKContext {
|
|
20
|
+
// SDK instances
|
|
21
|
+
tokens: any | null;
|
|
22
|
+
transactions: any | null;
|
|
23
|
+
business: any | null;
|
|
24
|
+
campaigns: any | null;
|
|
25
|
+
redemptions: any | null;
|
|
26
|
+
web3: any | null;
|
|
27
|
+
apiClient: PersApiClient | null;
|
|
28
|
+
|
|
29
|
+
// State
|
|
30
|
+
isInitialized: boolean;
|
|
31
|
+
isAuthenticated: boolean;
|
|
32
|
+
user: any | null;
|
|
33
|
+
accountAddress: string | null;
|
|
34
|
+
|
|
35
|
+
// Methods
|
|
36
|
+
initialize: (config: PersSDKConfig) => Promise<void>;
|
|
37
|
+
login: (jwtToken: string, userType?: 'user' | 'admin') => Promise<any>;
|
|
38
|
+
loginWithRawData: (rawUserData: any) => Promise<any>;
|
|
39
|
+
logout: () => Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Create the context
|
|
43
|
+
const SDKContext = createContext<PersSDKContext | null>(null);
|
|
44
|
+
|
|
45
|
+
// Custom auth provider for React Native
|
|
46
|
+
class ReactNativeSDKAuthProvider extends BaseReactNativeAuthProvider {
|
|
47
|
+
public authType: 'admin' | 'user' = 'user';
|
|
48
|
+
private _token: string | null = null;
|
|
49
|
+
|
|
50
|
+
constructor(private projectKey: string) {
|
|
51
|
+
super(projectKey);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async getToken(): Promise<string | null> {
|
|
55
|
+
return this._token;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async setToken(token: string | null): Promise<void> {
|
|
59
|
+
this._token = token;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async getProjectKey(): Promise<string | null> {
|
|
63
|
+
return this.projectKey;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async onTokenExpired(): Promise<void> {
|
|
67
|
+
console.log('Token expired - clearing token');
|
|
68
|
+
this._token = null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Provider component
|
|
73
|
+
export const PersSDKProvider: React.FC<{
|
|
74
|
+
children: ReactNode;
|
|
75
|
+
}> = ({ children }) => {
|
|
76
|
+
const initializingRef = useRef(false);
|
|
77
|
+
const [apiClient, setApiClient] = useState<PersApiClient | null>(null);
|
|
78
|
+
const [authProvider, setAuthProvider] = useState<ReactNativeSDKAuthProvider | null>(null);
|
|
79
|
+
const [sdks, setSdks] = useState<{
|
|
80
|
+
tokens: any | null;
|
|
81
|
+
transactions: any | null;
|
|
82
|
+
business: any | null;
|
|
83
|
+
campaigns: any | null;
|
|
84
|
+
redemptions: any | null;
|
|
85
|
+
web3: any | null;
|
|
86
|
+
}>({
|
|
87
|
+
tokens: null,
|
|
88
|
+
transactions: null,
|
|
89
|
+
business: null,
|
|
90
|
+
campaigns: null,
|
|
91
|
+
redemptions: null,
|
|
92
|
+
web3: null,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
96
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
97
|
+
const [user, setUser] = useState<any>(null);
|
|
98
|
+
const [accountAddress, setAccountAddress] = useState<string | null>(null);
|
|
99
|
+
|
|
100
|
+
const initialize = useCallback(async (config: PersSDKConfig) => {
|
|
101
|
+
// Prevent multiple initializations
|
|
102
|
+
if (isInitialized || initializingRef.current) {
|
|
103
|
+
console.log('⚠️ SDK already initialized or initializing, skipping...');
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
initializingRef.current = true;
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
console.log('🚀 Initializing PERS SDK with config:', config);
|
|
111
|
+
|
|
112
|
+
// Create auth provider
|
|
113
|
+
const auth = new ReactNativeSDKAuthProvider(config.apiProjectKey);
|
|
114
|
+
setAuthProvider(auth);
|
|
115
|
+
|
|
116
|
+
// Create HTTP client
|
|
117
|
+
const httpClient = new ReactNativeHttpClient();
|
|
118
|
+
|
|
119
|
+
// Create API client
|
|
120
|
+
const client = new PersApiClient(httpClient, {
|
|
121
|
+
apiProjectKey: config.apiProjectKey,
|
|
122
|
+
authProvider: auth,
|
|
123
|
+
environment: config.environment || 'development',
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
setApiClient(client);
|
|
127
|
+
|
|
128
|
+
// Initialize domain SDKs
|
|
129
|
+
const domainSDKs = {
|
|
130
|
+
tokens: new TokenSDK(client),
|
|
131
|
+
transactions: createTransactionSDK(client),
|
|
132
|
+
business: createBusinessSDK(client),
|
|
133
|
+
campaigns: createCampaignSDK(client),
|
|
134
|
+
redemptions: createRedemptionSDK(client),
|
|
135
|
+
web3: createWeb3SDK(client),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
setSdks(domainSDKs);
|
|
139
|
+
setIsInitialized(true);
|
|
140
|
+
|
|
141
|
+
console.log('✅ PERS SDK initialized successfully');
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error('❌ Failed to initialize PERS SDK:', error);
|
|
144
|
+
initializingRef.current = false;
|
|
145
|
+
throw error;
|
|
146
|
+
} finally {
|
|
147
|
+
initializingRef.current = false;
|
|
148
|
+
}
|
|
149
|
+
}, []);
|
|
150
|
+
|
|
151
|
+
const login = useCallback(async (jwtToken: string, userType: 'user' | 'admin' = 'user') => {
|
|
152
|
+
if (!apiClient || !authProvider) {
|
|
153
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
console.log(`🔐 Logging in as ${userType}...`);
|
|
158
|
+
|
|
159
|
+
// Set token in auth provider
|
|
160
|
+
await authProvider.setToken(jwtToken);
|
|
161
|
+
|
|
162
|
+
// Perform login with API client
|
|
163
|
+
const result = userType === 'admin'
|
|
164
|
+
? await apiClient.loginAdmin(jwtToken)
|
|
165
|
+
: await apiClient.loginUser(jwtToken);
|
|
166
|
+
|
|
167
|
+
const userData = result.user || result.admin;
|
|
168
|
+
const userAccountAddress = (userData as any)?.accountAddress ||
|
|
169
|
+
(userData as any)?.wallets?.[0]?.address ||
|
|
170
|
+
null;
|
|
171
|
+
|
|
172
|
+
setUser(userData);
|
|
173
|
+
setAccountAddress(userAccountAddress);
|
|
174
|
+
setIsAuthenticated(true);
|
|
175
|
+
|
|
176
|
+
console.log('✅ Login successful');
|
|
177
|
+
return result;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error('❌ Login failed:', error);
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}, [apiClient, authProvider]);
|
|
183
|
+
|
|
184
|
+
const loginWithRawData = useCallback(async (rawUserData: any) => {
|
|
185
|
+
if (!apiClient || !authProvider) {
|
|
186
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
console.log('🔐 Logging in with raw user data...');
|
|
191
|
+
|
|
192
|
+
const result = await apiClient.loginUserWithRawData(rawUserData);
|
|
193
|
+
|
|
194
|
+
// Set token from result
|
|
195
|
+
if (result.accessToken) {
|
|
196
|
+
await authProvider.setToken(result.accessToken);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const userData = result.user;
|
|
200
|
+
const userAccountAddress = (userData as any)?.accountAddress ||
|
|
201
|
+
(userData as any)?.wallets?.[0]?.address ||
|
|
202
|
+
null;
|
|
203
|
+
|
|
204
|
+
setUser(userData);
|
|
205
|
+
setAccountAddress(userAccountAddress);
|
|
206
|
+
setIsAuthenticated(true);
|
|
207
|
+
|
|
208
|
+
console.log('✅ Raw data login successful');
|
|
209
|
+
return result;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
console.error('❌ Raw data login failed:', error);
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
}, [apiClient, authProvider]);
|
|
215
|
+
|
|
216
|
+
const logout = useCallback(async () => {
|
|
217
|
+
try {
|
|
218
|
+
console.log('🔓 Logging out...');
|
|
219
|
+
|
|
220
|
+
if (authProvider) {
|
|
221
|
+
await authProvider.setToken(null);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
setUser(null);
|
|
225
|
+
setAccountAddress(null);
|
|
226
|
+
setIsAuthenticated(false);
|
|
227
|
+
|
|
228
|
+
console.log('✅ Logout successful');
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error('❌ Logout failed:', error);
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}, [authProvider]);
|
|
234
|
+
|
|
235
|
+
const contextValue: PersSDKContext = {
|
|
236
|
+
// SDK instances
|
|
237
|
+
tokens: sdks.tokens,
|
|
238
|
+
transactions: sdks.transactions,
|
|
239
|
+
business: sdks.business,
|
|
240
|
+
campaigns: sdks.campaigns,
|
|
241
|
+
redemptions: sdks.redemptions,
|
|
242
|
+
web3: sdks.web3,
|
|
243
|
+
apiClient,
|
|
244
|
+
|
|
245
|
+
// State
|
|
246
|
+
isInitialized,
|
|
247
|
+
isAuthenticated,
|
|
248
|
+
user,
|
|
249
|
+
accountAddress,
|
|
250
|
+
|
|
251
|
+
// Methods
|
|
252
|
+
initialize,
|
|
253
|
+
login,
|
|
254
|
+
loginWithRawData,
|
|
255
|
+
logout,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
return (
|
|
259
|
+
<SDKContext.Provider value={contextValue}>
|
|
260
|
+
{children}
|
|
261
|
+
</SDKContext.Provider>
|
|
262
|
+
);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// Custom hook to use the SDK context
|
|
266
|
+
export const usePersSDK = (): PersSDKContext => {
|
|
267
|
+
const context = useContext(SDKContext);
|
|
268
|
+
|
|
269
|
+
if (!context) {
|
|
270
|
+
throw new Error('usePersSDK must be used within a PersSDKProvider');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return context;
|
|
274
|
+
};
|