@onairos/react-native 3.0.0 → 3.0.1

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,534 +1,419 @@
1
- import { Linking, Platform } from 'react-native';
2
- import { updateCredentials, OnairosCredentials } from '../utils/secureStorage';
3
- import { sha256 } from '../utils/crypto';
4
- import { onairosApi } from '../api';
5
-
6
- // Define OAuth configuration types
7
- export interface OAuthConfig {
8
- clientId: string;
9
- redirectUri: string;
10
- scope: string;
11
- authorizationEndpoint: string;
12
- tokenEndpoint: string;
13
- responseType: string;
14
- }
15
-
16
- // Platform-specific OAuth configurations
17
- const OAUTH_CONFIGS: Record<string, OAuthConfig> = {
18
- instagram: {
19
- clientId: 'YOUR_INSTAGRAM_CLIENT_ID', // Replace with actual client ID
20
- redirectUri: 'onairosreact://auth/instagram',
21
- scope: 'user_profile,user_media',
22
- authorizationEndpoint: 'https://api.instagram.com/oauth/authorize',
23
- tokenEndpoint: 'https://api.instagram.com/oauth/access_token',
24
- responseType: 'code',
25
- },
26
- youtube: {
27
- clientId: 'YOUR_YOUTUBE_CLIENT_ID', // Replace with actual client ID
28
- redirectUri: 'onairosreact://auth/youtube',
29
- scope: 'https://www.googleapis.com/auth/youtube.readonly',
30
- authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth',
31
- tokenEndpoint: 'https://oauth2.googleapis.com/token',
32
- responseType: 'code',
33
- },
34
- pinterest: {
35
- clientId: 'YOUR_PINTEREST_CLIENT_ID', // Replace with actual client ID
36
- redirectUri: 'onairosreact://auth/pinterest',
37
- scope: 'boards:read,pins:read',
38
- authorizationEndpoint: 'https://www.pinterest.com/oauth/',
39
- tokenEndpoint: 'https://api.pinterest.com/v5/oauth/token',
40
- responseType: 'code',
41
- },
42
- reddit: {
43
- clientId: 'YOUR_REDDIT_CLIENT_ID', // Replace with actual client ID
44
- redirectUri: 'onairosreact://auth/reddit',
45
- scope: 'identity,read',
46
- authorizationEndpoint: 'https://www.reddit.com/api/v1/authorize',
47
- tokenEndpoint: 'https://www.reddit.com/api/v1/access_token',
48
- responseType: 'code',
49
- },
50
- };
51
-
52
- /**
53
- * Generate a state value for OAuth to prevent CSRF attacks
54
- */
55
- const generateState = (): string => {
56
- const randomValue = Math.random().toString(36).substring(2, 15);
57
- return sha256(randomValue).substring(0, 10);
58
- };
59
-
60
- /**
61
- * Initialize OAuth service handlers and listeners
62
- */
63
- export const initializeOAuthService = (): void => {
64
- // Set up deep linking handlers for OAuth redirects
65
- Linking.addEventListener('url', handleDeepLink);
66
- };
67
-
68
- /**
69
- * Clean up OAuth service handlers and listeners
70
- */
71
- export const cleanupOAuthService = (): void => {
72
- // Clean up deep linking handlers
73
- if (Platform.OS === 'android') {
74
- Linking.removeEventListener('url', handleDeepLink);
75
- }
76
- };
77
-
78
- // Keep track of current OAuth state and callbacks
79
- let currentOAuthState: string | null = null;
80
- let currentOAuthPlatform: string | null = null;
81
- let currentOAuthResolve: ((value: any) => void) | null = null;
82
- let currentOAuthReject: ((error: Error) => void) | null = null;
83
-
84
- /**
85
- * Handle deep link callbacks from OAuth providers
86
- */
87
- const handleDeepLink = async (event: { url: string }): Promise<void> => {
88
- try {
89
- const { url } = event;
90
-
91
- // Check if this is an OAuth callback URL
92
- if (!url.startsWith('onairosreact://auth/')) {
93
- return;
94
- }
95
-
96
- // Extract platform from URL path
97
- const platform = url.split('onairosreact://auth/')[1].split('?')[0];
98
-
99
- // Only handle if it matches current OAuth flow
100
- if (platform !== currentOAuthPlatform) {
101
- return;
102
- }
103
-
104
- // Parse URL parameters
105
- const params = new URL(url).searchParams;
106
- const code = params.get('code');
107
- const state = params.get('state');
108
- const error = params.get('error');
109
-
110
- // Validate state to prevent CSRF attacks
111
- if (state !== currentOAuthState) {
112
- if (currentOAuthReject) {
113
- currentOAuthReject(new Error('OAuth state mismatch - possible CSRF attack'));
114
- }
115
- return;
116
- }
117
-
118
- // Handle errors
119
- if (error) {
120
- if (currentOAuthReject) {
121
- currentOAuthReject(new Error(`OAuth error: ${error}`));
122
- }
123
- return;
124
- }
125
-
126
- // Proceed with token exchange if code is present
127
- if (code) {
128
- const tokenResult = await exchangeCodeForToken(platform, code);
129
-
130
- if (currentOAuthResolve) {
131
- currentOAuthResolve(tokenResult);
132
- }
133
- } else {
134
- if (currentOAuthReject) {
135
- currentOAuthReject(new Error('No authorization code received'));
136
- }
137
- }
138
- } catch (error) {
139
- console.error('Error handling OAuth deep link:', error);
140
- if (currentOAuthReject) {
141
- currentOAuthReject(error as Error);
142
- }
143
- } finally {
144
- // Reset state
145
- currentOAuthState = null;
146
- currentOAuthPlatform = null;
147
- currentOAuthResolve = null;
148
- currentOAuthReject = null;
149
- }
150
- };
151
-
152
- /**
153
- * Exchange OAuth authorization code for access token
154
- */
155
- const exchangeCodeForToken = async (platform: string, code: string): Promise<any> => {
156
- try {
157
- const config = OAUTH_CONFIGS[platform];
158
-
159
- if (!config) {
160
- throw new Error(`Unsupported platform: ${platform}`);
161
- }
162
-
163
- // Prepare token request parameters
164
- const params = new URLSearchParams({
165
- grant_type: 'authorization_code',
166
- code,
167
- redirect_uri: config.redirectUri,
168
- client_id: config.clientId,
169
- });
170
-
171
- // Exchange code for token
172
- const response = await fetch(config.tokenEndpoint, {
173
- method: 'POST',
174
- headers: {
175
- 'Content-Type': 'application/x-www-form-urlencoded',
176
- },
177
- body: params.toString(),
178
- });
179
-
180
- const data = await response.json();
181
-
182
- if (!response.ok) {
183
- throw new Error(data.error || 'Failed to exchange code for token');
184
- }
185
-
186
- // Fetch user information based on the platform
187
- const userInfo = await fetchUserInfo(platform, data.access_token);
188
-
189
- return {
190
- token: data.access_token,
191
- refreshToken: data.refresh_token,
192
- expiresIn: data.expires_in,
193
- username: userInfo.username,
194
- userId: userInfo.id,
195
- };
196
- } catch (error) {
197
- console.error(`Error exchanging code for token (${platform}):`, error);
198
- throw error;
199
- }
200
- };
201
-
202
- /**
203
- * Fetch user information from the connected platform
204
- */
205
- const fetchUserInfo = async (platform: string, accessToken: string): Promise<any> => {
206
- try {
207
- let endpoint;
208
- let headers: Record<string, string> = {
209
- Authorization: `Bearer ${accessToken}`,
210
- };
211
-
212
- // Platform-specific API endpoints for user info
213
- switch (platform) {
214
- case 'instagram':
215
- endpoint = 'https://graph.instagram.com/me?fields=id,username';
216
- break;
217
- case 'youtube':
218
- endpoint = 'https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true';
219
- break;
220
- case 'pinterest':
221
- endpoint = 'https://api.pinterest.com/v5/user_account';
222
- break;
223
- case 'reddit':
224
- endpoint = 'https://oauth.reddit.com/api/v1/me';
225
- break;
226
- default:
227
- throw new Error(`Unsupported platform: ${platform}`);
228
- }
229
-
230
- const response = await fetch(endpoint, { headers });
231
- const data = await response.json();
232
-
233
- if (!response.ok) {
234
- throw new Error(data.error || 'Failed to fetch user info');
235
- }
236
-
237
- // Extract user information based on platform-specific response format
238
- switch (platform) {
239
- case 'instagram':
240
- return { id: data.id, username: data.username };
241
- case 'youtube':
242
- return {
243
- id: data.items[0].id,
244
- username: data.items[0].snippet.title
245
- };
246
- case 'pinterest':
247
- return {
248
- id: data.id,
249
- username: data.username || data.full_name
250
- };
251
- case 'reddit':
252
- return { id: data.id, username: data.name };
253
- default:
254
- throw new Error(`Unsupported platform: ${platform}`);
255
- }
256
- } catch (error) {
257
- console.error(`Error fetching user info (${platform}):`, error);
258
- throw error;
259
- }
260
- };
261
-
262
- /**
263
- * Initiate OAuth flow for a specific platform
264
- */
265
- export const connectPlatform = (platform: string): Promise<any> => {
266
- return new Promise((resolve, reject) => {
267
- try {
268
- const config = OAUTH_CONFIGS[platform];
269
-
270
- if (!config) {
271
- reject(new Error(`Unsupported platform: ${platform}`));
272
- return;
273
- }
274
-
275
- // Generate and save state for CSRF protection
276
- const state = generateState();
277
- currentOAuthState = state;
278
- currentOAuthPlatform = platform;
279
- currentOAuthResolve = resolve;
280
- currentOAuthReject = reject;
281
-
282
- // Build OAuth URL
283
- const authUrl = new URL(config.authorizationEndpoint);
284
- authUrl.searchParams.append('client_id', config.clientId);
285
- authUrl.searchParams.append('redirect_uri', config.redirectUri);
286
- authUrl.searchParams.append('response_type', config.responseType);
287
- authUrl.searchParams.append('scope', config.scope);
288
- authUrl.searchParams.append('state', state);
289
-
290
- // Open browser to start OAuth flow
291
- Linking.openURL(authUrl.toString());
292
- } catch (error) {
293
- reject(error);
294
-
295
- // Reset state on error
296
- currentOAuthState = null;
297
- currentOAuthPlatform = null;
298
- currentOAuthResolve = null;
299
- currentOAuthReject = null;
300
- }
301
- });
302
- };
303
-
304
- /**
305
- * Disconnect a platform by removing its credentials
306
- */
307
- export const disconnectPlatform = async (
308
- platform: string,
309
- credentials: OnairosCredentials
310
- ): Promise<boolean> => {
311
- try {
312
- if (!credentials.platforms) {
313
- return false;
314
- }
315
-
316
- // Create new credentials object with the platform removed
317
- const updatedPlatforms = { ...credentials.platforms };
318
- delete updatedPlatforms[platform as keyof typeof updatedPlatforms];
319
-
320
- // Update stored credentials
321
- const result = await updateCredentials({
322
- platforms: updatedPlatforms,
323
- });
324
-
325
- return result;
326
- } catch (error) {
327
- console.error(`Error disconnecting platform (${platform}):`, error);
328
- return false;
329
- }
330
- };
331
-
332
- /**
333
- * Store platform connection in credentials
334
- */
335
- export const storePlatformConnection = async (
336
- platform: string,
337
- connectionData: any,
338
- credentials: OnairosCredentials
339
- ): Promise<boolean> => {
340
- try {
341
- // Create updated platforms object
342
- const updatedPlatforms = {
343
- ...credentials.platforms,
344
- [platform]: {
345
- token: connectionData.token,
346
- username: connectionData.username,
347
- userId: connectionData.userId,
348
- },
349
- };
350
-
351
- // Update stored credentials
352
- const result = await updateCredentials({
353
- platforms: updatedPlatforms,
354
- });
355
-
356
- return result;
357
- } catch (error) {
358
- console.error(`Error storing platform connection (${platform}):`, error);
359
- return false;
360
- }
361
- };
362
-
363
- export interface AuthorizationData {
364
- accountName: string;
365
- authUrl: string;
366
- }
367
-
368
- export interface PlatformConnectionResult {
369
- success: boolean;
370
- userName?: string;
371
- error?: string;
372
- }
373
-
374
- /**
375
- * Service for handling OAuth connections to various platforms
376
- */
377
- export const OAuthService = {
378
- // Base API URL
379
- _apiBaseUrl: 'https://api2.onairos.uk',
380
-
381
- /**
382
- * Connect to a specific platform using OAuth
383
- * @param platform The platform to connect to (e.g., 'instagram', 'youtube')
384
- * @returns A promise that resolves to a connection result
385
- */
386
- connectPlatform: async (platform: string): Promise<PlatformConnectionResult> => {
387
- try {
388
- console.log(`[OAuth] Initiating connection to ${platform}`);
389
-
390
- // Get authorization data from API
391
- const authData = await OAuthService._getAuthorizationData(platform);
392
-
393
- if (!authData || !authData.accountName) {
394
- throw new Error(`Failed to get authorization data for ${platform}`);
395
- }
396
-
397
- // Launch the OAuth flow in a WebView
398
- const success = await OAuthService._launchOAuthFlow(
399
- platform,
400
- `${OAuthService._apiBaseUrl}/${authData.accountName}/authorize`,
401
- `onairos://${platform.toLowerCase()}/callback`
402
- );
403
-
404
- return {
405
- success,
406
- userName: success ? `User_${platform}` : undefined,
407
- error: success ? undefined : `Failed to connect to ${platform}`
408
- };
409
- } catch (error) {
410
- console.error(`${platform} connection error:`, error);
411
- return {
412
- success: false,
413
- error: error instanceof Error ? error.message : 'Unknown error'
414
- };
415
- }
416
- },
417
-
418
- /**
419
- * Get authorization data for a platform from the API
420
- * @param platform The platform to get authorization data for
421
- * @returns Authorization data for the platform
422
- */
423
- _getAuthorizationData: async (platform: string): Promise<AuthorizationData> => {
424
- try {
425
- // For testing, we can use a mock app ID
426
- let appId = 'com.onairos.mock';
427
-
428
- // In real implementation, we would get this from the app's package info
429
- try {
430
- // This would normally use react-native-device-info or similar
431
- // appId = await DeviceInfo.getBundleId();
432
- } catch (e) {
433
- console.warn('Failed to get app identifier:', e);
434
- }
435
-
436
- const response = await onairosApi.post('getOAuthData', {
437
- platform,
438
- appId: appId,
439
- redirectUri: `onairos://${platform.toLowerCase()}/callback`,
440
- });
441
-
442
- if (response && response.accountName) {
443
- return {
444
- accountName: response.accountName,
445
- authUrl: `${OAuthService._apiBaseUrl}/${response.accountName}/authorize`,
446
- };
447
- } else {
448
- throw new Error('Invalid response from getOAuthData');
449
- }
450
- } catch (error) {
451
- console.error('Error getting authorization data:', error);
452
- throw error;
453
- }
454
- },
455
-
456
- /**
457
- * Launch the OAuth flow for a platform
458
- * @param platform The platform to launch the OAuth flow for
459
- * @param authUrl The URL to authorize with
460
- * @param callbackUrlPattern The URL pattern to expect as a callback
461
- * @returns A promise that resolves to true if the connection was successful
462
- */
463
- _launchOAuthFlow: async (
464
- platform: string,
465
- authUrl: string,
466
- callbackUrlPattern: string
467
- ): Promise<boolean> => {
468
- try {
469
- console.log(`[OAuth] Opening URL for ${platform}: ${authUrl}`);
470
-
471
- // For now, we'll use a simpler approach just to mock the flow
472
- // In a real implementation, this would open a WebView in a modal
473
- // and handle the OAuth callback
474
-
475
- // Check if we can open the URL
476
- const canOpen = await Linking.canOpenURL(authUrl);
477
-
478
- if (!canOpen) {
479
- throw new Error(`Cannot open URL: ${authUrl}`);
480
- }
481
-
482
- // We'll simulate a successful connection after a delay
483
- // In a real app, this would be handled by the WebView navigation
484
- await new Promise(resolve => setTimeout(resolve, 1000));
485
-
486
- // Return success
487
- return true;
488
- } catch (error) {
489
- console.error(`Error launching OAuth flow for ${platform}:`, error);
490
- return false;
491
- }
492
- },
493
-
494
- /**
495
- * Handle an OAuth callback URL
496
- * @param url The callback URL to handle
497
- * @returns The result of processing the callback
498
- */
499
- handleCallback: async (url: string): Promise<PlatformConnectionResult> => {
500
- try {
501
- console.log(`[OAuth] Handling callback URL: ${url}`);
502
-
503
- // Extract the platform and parameters from the URL
504
- const urlParts = url.split('/');
505
- const platform = urlParts[2]; // Assuming format is onairos://platform/callback
506
-
507
- // Extract query parameters
508
- const params = new URLSearchParams(url.split('?')[1] || '');
509
- const code = params.get('code');
510
-
511
- if (!code) {
512
- return {
513
- success: false,
514
- error: 'No authorization code found in callback URL'
515
- };
516
- }
517
-
518
- // In a real implementation, we would send the code to the API
519
- // to get an access token
520
-
521
- // Simulate a successful connection
522
- return {
523
- success: true,
524
- userName: `User_${platform}`
525
- };
526
- } catch (error) {
527
- console.error('Error handling OAuth callback:', error);
528
- return {
529
- success: false,
530
- error: error instanceof Error ? error.message : 'Unknown error'
531
- };
532
- }
533
- }
534
- };
1
+ import { Linking, Platform } from 'react-native';
2
+ import { updateCredentials, OnairosCredentials } from '../utils/secureStorage';
3
+ import { sha256 } from '../utils/crypto';
4
+ import { onairosApi } from '../api';
5
+
6
+ // Define OAuth configuration types
7
+ export interface OAuthConfig {
8
+ clientId: string;
9
+ redirectUri: string;
10
+ scope: string;
11
+ authorizationEndpoint: string;
12
+ tokenEndpoint: string;
13
+ responseType: string;
14
+ }
15
+
16
+ // Platform-specific OAuth configurations
17
+ const OAUTH_CONFIGS: Record<string, OAuthConfig> = {
18
+ instagram: {
19
+ clientId: 'YOUR_INSTAGRAM_CLIENT_ID', // Replace with actual client ID
20
+ redirectUri: 'onairosreact://auth/instagram',
21
+ scope: 'user_profile,user_media',
22
+ authorizationEndpoint: 'https://api.instagram.com/oauth/authorize',
23
+ tokenEndpoint: 'https://api.instagram.com/oauth/access_token',
24
+ responseType: 'code',
25
+ },
26
+ youtube: {
27
+ clientId: 'YOUR_YOUTUBE_CLIENT_ID', // Replace with actual client ID
28
+ redirectUri: 'onairosreact://auth/youtube',
29
+ scope: 'https://www.googleapis.com/auth/youtube.readonly',
30
+ authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth',
31
+ tokenEndpoint: 'https://oauth2.googleapis.com/token',
32
+ responseType: 'code',
33
+ },
34
+ pinterest: {
35
+ clientId: 'YOUR_PINTEREST_CLIENT_ID', // Replace with actual client ID
36
+ redirectUri: 'onairosreact://auth/pinterest',
37
+ scope: 'boards:read,pins:read',
38
+ authorizationEndpoint: 'https://www.pinterest.com/oauth/',
39
+ tokenEndpoint: 'https://api.pinterest.com/v5/oauth/token',
40
+ responseType: 'code',
41
+ },
42
+ reddit: {
43
+ clientId: 'YOUR_REDDIT_CLIENT_ID', // Replace with actual client ID
44
+ redirectUri: 'onairosreact://auth/reddit',
45
+ scope: 'identity,read',
46
+ authorizationEndpoint: 'https://www.reddit.com/api/v1/authorize',
47
+ tokenEndpoint: 'https://www.reddit.com/api/v1/access_token',
48
+ responseType: 'code',
49
+ },
50
+ };
51
+
52
+ /**
53
+ * Generate a state value for OAuth to prevent CSRF attacks
54
+ */
55
+ const generateState = (): string => {
56
+ const randomValue = Math.random().toString(36).substring(2, 15);
57
+ return sha256(randomValue).substring(0, 10);
58
+ };
59
+
60
+ /**
61
+ * Initialize OAuth service handlers and listeners
62
+ */
63
+ export const initializeOAuthService = (): void => {
64
+ // Set up deep linking handlers for OAuth redirects
65
+ Linking.addEventListener('url', handleDeepLink);
66
+ };
67
+
68
+ /**
69
+ * Clean up OAuth service handlers and listeners
70
+ */
71
+ export const cleanupOAuthService = (): void => {
72
+ // Use the modern React Native Linking API
73
+ if (typeof Linking.removeAllListeners === 'function') {
74
+ Linking.removeAllListeners('url');
75
+ }
76
+ };
77
+
78
+ // Keep track of current OAuth state and callbacks
79
+ let currentOAuthState: string | null = null;
80
+ let currentOAuthPlatform: string | null = null;
81
+ let currentOAuthResolve: ((value: any) => void) | null = null;
82
+ let currentOAuthReject: ((error: Error) => void) | null = null;
83
+
84
+ /**
85
+ * Handle deep link callbacks from OAuth providers
86
+ */
87
+ const handleDeepLink = async (event: { url: string }): Promise<void> => {
88
+ try {
89
+ const { url } = event;
90
+
91
+ // Check if this is an OAuth callback URL
92
+ if (!url.startsWith('onairosreact://auth/')) {
93
+ return;
94
+ }
95
+
96
+ // Extract platform from URL path
97
+ const platform = url.split('onairosreact://auth/')[1].split('?')[0];
98
+
99
+ // Only handle if it matches current OAuth flow
100
+ if (platform !== currentOAuthPlatform) {
101
+ return;
102
+ }
103
+
104
+ // Parse URL parameters
105
+ const params = new URL(url).searchParams;
106
+ const code = params.get('code');
107
+ const state = params.get('state');
108
+ const error = params.get('error');
109
+
110
+ // Validate state to prevent CSRF attacks
111
+ if (state !== currentOAuthState) {
112
+ if (currentOAuthReject) {
113
+ currentOAuthReject(new Error('OAuth state mismatch - possible CSRF attack'));
114
+ }
115
+ return;
116
+ }
117
+
118
+ // Handle errors
119
+ if (error) {
120
+ if (currentOAuthReject) {
121
+ currentOAuthReject(new Error(`OAuth error: ${error}`));
122
+ }
123
+ return;
124
+ }
125
+
126
+ // Proceed with token exchange if code is present
127
+ if (code) {
128
+ const tokenResult = await exchangeCodeForToken(platform, code);
129
+
130
+ if (currentOAuthResolve) {
131
+ currentOAuthResolve(tokenResult);
132
+ }
133
+ } else {
134
+ if (currentOAuthReject) {
135
+ currentOAuthReject(new Error('No authorization code received'));
136
+ }
137
+ }
138
+ } catch (error) {
139
+ console.error('Error handling OAuth deep link:', error);
140
+ if (currentOAuthReject) {
141
+ currentOAuthReject(error as Error);
142
+ }
143
+ } finally {
144
+ // Reset state
145
+ currentOAuthState = null;
146
+ currentOAuthPlatform = null;
147
+ currentOAuthResolve = null;
148
+ currentOAuthReject = null;
149
+ }
150
+ };
151
+
152
+ /**
153
+ * Exchange OAuth authorization code for access token
154
+ */
155
+ const exchangeCodeForToken = async (platform: string, code: string): Promise<any> => {
156
+ try {
157
+ const config = OAUTH_CONFIGS[platform];
158
+
159
+ if (!config) {
160
+ throw new Error(`Unsupported platform: ${platform}`);
161
+ }
162
+
163
+ // Prepare token request parameters
164
+ const params = new URLSearchParams({
165
+ grant_type: 'authorization_code',
166
+ code,
167
+ redirect_uri: config.redirectUri,
168
+ client_id: config.clientId,
169
+ });
170
+
171
+ // Exchange code for token
172
+ const response = await fetch(config.tokenEndpoint, {
173
+ method: 'POST',
174
+ headers: {
175
+ 'Content-Type': 'application/x-www-form-urlencoded',
176
+ },
177
+ body: params.toString(),
178
+ });
179
+
180
+ const data = await response.json();
181
+
182
+ if (!response.ok) {
183
+ throw new Error(data.error || 'Failed to exchange code for token');
184
+ }
185
+
186
+ // Fetch user information based on the platform
187
+ const userInfo = await fetchUserInfo(platform, data.access_token);
188
+
189
+ return {
190
+ token: data.access_token,
191
+ refreshToken: data.refresh_token,
192
+ expiresIn: data.expires_in,
193
+ username: userInfo.username,
194
+ userId: userInfo.id,
195
+ };
196
+ } catch (error) {
197
+ console.error(`Error exchanging code for token (${platform}):`, error);
198
+ throw error;
199
+ }
200
+ };
201
+
202
+ /**
203
+ * Fetch user information from the connected platform
204
+ */
205
+ const fetchUserInfo = async (platform: string, accessToken: string): Promise<any> => {
206
+ try {
207
+ let endpoint;
208
+ let headers: Record<string, string> = {
209
+ Authorization: `Bearer ${accessToken}`,
210
+ };
211
+
212
+ // Platform-specific API endpoints for user info
213
+ switch (platform) {
214
+ case 'instagram':
215
+ endpoint = 'https://graph.instagram.com/me?fields=id,username';
216
+ break;
217
+ case 'youtube':
218
+ endpoint = 'https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true';
219
+ break;
220
+ case 'pinterest':
221
+ endpoint = 'https://api.pinterest.com/v5/user_account';
222
+ break;
223
+ case 'reddit':
224
+ endpoint = 'https://oauth.reddit.com/api/v1/me';
225
+ break;
226
+ default:
227
+ throw new Error(`Unsupported platform: ${platform}`);
228
+ }
229
+
230
+ const response = await fetch(endpoint, { headers });
231
+ const data = await response.json();
232
+
233
+ if (!response.ok) {
234
+ throw new Error(data.error || 'Failed to fetch user info');
235
+ }
236
+
237
+ // Extract user information based on platform-specific response format
238
+ switch (platform) {
239
+ case 'instagram':
240
+ return { id: data.id, username: data.username };
241
+ case 'youtube':
242
+ return {
243
+ id: data.items[0].id,
244
+ username: data.items[0].snippet.title
245
+ };
246
+ case 'pinterest':
247
+ return {
248
+ id: data.id,
249
+ username: data.username || data.full_name
250
+ };
251
+ case 'reddit':
252
+ return { id: data.id, username: data.name };
253
+ default:
254
+ throw new Error(`Unsupported platform: ${platform}`);
255
+ }
256
+ } catch (error) {
257
+ console.error(`Error fetching user info (${platform}):`, error);
258
+ throw error;
259
+ }
260
+ };
261
+
262
+ /**
263
+ * Connect to a platform using OAuth
264
+ */
265
+ export const connectPlatform = (platform: string): Promise<any> => {
266
+ return new Promise((resolve, reject) => {
267
+ try {
268
+ const config = OAUTH_CONFIGS[platform];
269
+
270
+ if (!config) {
271
+ throw new Error(`Unsupported platform: ${platform}`);
272
+ }
273
+
274
+ // Generate state for CSRF protection
275
+ const state = generateState();
276
+
277
+ // Build authorization URL
278
+ const authUrl = new URL(config.authorizationEndpoint);
279
+ authUrl.searchParams.append('client_id', config.clientId);
280
+ authUrl.searchParams.append('redirect_uri', config.redirectUri);
281
+ authUrl.searchParams.append('response_type', config.responseType);
282
+ authUrl.searchParams.append('scope', config.scope);
283
+ authUrl.searchParams.append('state', state);
284
+
285
+ // Set up current OAuth state for callback handling
286
+ currentOAuthState = state;
287
+ currentOAuthPlatform = platform;
288
+ currentOAuthResolve = resolve;
289
+ currentOAuthReject = reject;
290
+
291
+ // Open browser or WebView to the authorization URL
292
+ Linking.openURL(authUrl.toString());
293
+ } catch (error) {
294
+ reject(error);
295
+ }
296
+ });
297
+ };
298
+
299
+ /**
300
+ * Disconnect from a platform
301
+ */
302
+ export const disconnectPlatform = async (
303
+ platform: string,
304
+ credentials: OnairosCredentials
305
+ ): Promise<boolean> => {
306
+ try {
307
+ // Call Onairos API to disconnect platform
308
+ await onairosApi.post('/users/disconnect-platform', {
309
+ platform,
310
+ username: credentials.username,
311
+ });
312
+
313
+ // Update local credentials to remove platform
314
+ const updatedPlatforms = { ...credentials.platforms };
315
+
316
+ // Type-safe platform removal using keyof operator
317
+ if (updatedPlatforms && platform in updatedPlatforms) {
318
+ delete updatedPlatforms[platform as keyof typeof updatedPlatforms];
319
+ }
320
+
321
+ await updateCredentials({
322
+ ...credentials,
323
+ platforms: updatedPlatforms,
324
+ });
325
+
326
+ return true;
327
+ } catch (error) {
328
+ console.error(`Error disconnecting platform (${platform}):`, error);
329
+ return false;
330
+ }
331
+ };
332
+
333
+ /**
334
+ * Store platform connection data in user credentials
335
+ */
336
+ export const storePlatformConnection = async (
337
+ platform: string,
338
+ connectionData: any,
339
+ credentials: OnairosCredentials
340
+ ): Promise<boolean> => {
341
+ try {
342
+ // Only accept valid platform types
343
+ const validPlatform = (
344
+ platform === 'instagram' ||
345
+ platform === 'youtube' ||
346
+ platform === 'pinterest' ||
347
+ platform === 'reddit'
348
+ );
349
+
350
+ if (!validPlatform) {
351
+ throw new Error(`Unsupported platform: ${platform}`);
352
+ }
353
+
354
+ // Update platforms in credentials with type safety
355
+ const updatedPlatforms = {
356
+ ...credentials.platforms,
357
+ };
358
+
359
+ // Type-safe assignment
360
+ const platformData = {
361
+ username: connectionData.username,
362
+ userId: connectionData.userId,
363
+ token: connectionData.token,
364
+ refreshToken: connectionData.refreshToken,
365
+ expiresAt: connectionData.expiresIn ?
366
+ Date.now() + (connectionData.expiresIn * 1000) :
367
+ null,
368
+ connectedAt: Date.now(),
369
+ };
370
+
371
+ // Assign platform data based on platform type
372
+ if (platform === 'instagram') updatedPlatforms.instagram = platformData;
373
+ else if (platform === 'youtube') updatedPlatforms.youtube = platformData;
374
+ else if (platform === 'pinterest') updatedPlatforms.pinterest = platformData;
375
+ else if (platform === 'reddit') updatedPlatforms.reddit = platformData;
376
+
377
+ // Update stored credentials
378
+ await updateCredentials({
379
+ ...credentials,
380
+ platforms: updatedPlatforms,
381
+ });
382
+
383
+ return true;
384
+ } catch (error) {
385
+ console.error(`Error storing platform connection (${platform}):`, error);
386
+ return false;
387
+ }
388
+ };
389
+
390
+ export interface AuthorizationData {
391
+ accountName: string;
392
+ authUrl: string;
393
+ }
394
+
395
+ export interface PlatformConnectionResult {
396
+ success: boolean;
397
+ userName?: string;
398
+ error?: string;
399
+ }
400
+
401
+ /**
402
+ * Service for handling OAuth connections to various platforms
403
+ */
404
+ export const OAuthService = {
405
+ initializeOAuthService,
406
+ cleanupOAuthService,
407
+ connectPlatform,
408
+ disconnectPlatform,
409
+ storePlatformConnection,
410
+ // Base API URL
411
+ _apiBaseUrl: 'https://api2.onairos.uk',
412
+ };
413
+
414
+ /**
415
+ * Re-export from oauthService.ts with correct capitalization
416
+ * This file exists to solve the casing issue in imports
417
+ */
418
+
419
+ export * from './oauthService';