import React, { useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useLocation, useNavigate } from 'react-router-dom'; import { handleLinkedInCallback, clearLinkedInError, fetchLinkedInAccounts } from '../../store/reducers/linkedinAccountsSlice'; import apiClient from '../../services/apiClient'; const LinkedInCallbackHandler = () => { const dispatch = useDispatch(); const location = useLocation(); const navigate = useNavigate(); const { oauthLoading, oauthError } = useSelector(state => state.linkedinAccounts); const [status, setStatus] = useState('processing'); const [message, setMessage] = useState('Processing LinkedIn authentication...'); useEffect(() => { const handleCallback = async () => { try { // Parse URL parameters const urlParams = new URLSearchParams(location.search); const code = urlParams.get('code'); const state = urlParams.get('state'); const error = urlParams.get('error'); // DEBUG: Log callback parameters console.log('🔗 [Frontend] LinkedIn callback handler started'); console.log('🔗 [Frontend] URL parameters:', { code: code?.substring(0, 20) + '...', state, error }); if (error) { console.error('🔗 [Frontend] LinkedIn authentication error:', error); setStatus('error'); setMessage(`Authentication failed: ${error}`); return; } if (!code || !state) { console.error('🔗 [Frontend] Missing callback parameters:', { code, state }); setStatus('error'); setMessage('Invalid callback parameters'); return; } setStatus('processing'); setMessage('Completing LinkedIn authentication...'); // DEBUG: Log callback processing console.log('🔗 [Frontend] Processing OAuth callback...'); console.log('🔗 [Frontend] Making API call to complete OAuth flow...'); // Make the API call to complete the OAuth flow const response = await apiClient.post('/accounts/callback', { code: code, state: state, social_network: 'LinkedIn' }); // DEBUG: Log callback response console.log('🔗 [Frontend] API response received:', response); console.log('🔗 [Frontend] Response status:', response.status); console.log('🔗 [Frontend] Response data:', response.data); if (response.data.success) { console.log('🔗 [Frontend] LinkedIn account linked successfully!'); setStatus('success'); setMessage('LinkedIn account linked successfully!'); // Dispatch success action to update Redux state await dispatch(fetchLinkedInAccounts()); // Redirect to sources page after a short delay setTimeout(() => { console.log('🔗 [Frontend] Redirecting to sources page...'); navigate('/sources'); }, 2000); } else { console.error('🔗 [Frontend] LinkedIn account linking failed:', response.data.message); setStatus('error'); setMessage(response.data.message || 'Failed to link LinkedIn account'); } } catch (error) { console.error('🔗 [Frontend] Callback handler error:', error); console.error('🔗 [Frontend] Error details:', { message: error.message, response: error.response?.data, status: error.response?.status }); setStatus('error'); setMessage('An error occurred during authentication'); } }; // Check if we're on the callback URL if (location.search.includes('code=') || location.search.includes('error=')) { console.log('🔗 [Frontend] Detected callback URL, processing...'); handleCallback(); } else { console.log('🔗 [Frontend] Not a callback URL, normal page load'); } }, [dispatch, location, navigate]); const handleRetry = () => { dispatch(clearLinkedInError()); navigate('/sources'); }; return (
Please wait while we complete the authentication process...
Redirecting to your accounts...
There was an issue with the LinkedIn authentication process.