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 error = urlParams.get('error'); const oauthSuccess = urlParams.get('oauth_success'); const accountLinked = urlParams.get('account_linked'); const from = urlParams.get('from'); // DEBUG: Log callback parameters console.log('🔗 [Frontend] LinkedIn callback handler started'); console.log('🔗 [Frontend] URL parameters:', { error, oauthSuccess, accountLinked, from }); // Check if we're coming from LinkedIn OAuth if (from === 'linkedin') { if (error) { console.error('🔗 [Frontend] OAuth error:', error); setStatus('error'); setMessage(`Authentication failed: ${error}`); return; } if (oauthSuccess === 'true') { if (accountLinked === 'true') { console.log('🔗 [Frontend] Account linked successfully!'); setStatus('success'); setMessage('LinkedIn account linked successfully!'); // Dispatch success action to update Redux state await dispatch(fetchLinkedInAccounts()); // Redirect to accounts page after a short delay setTimeout(() => { console.log('🔗 [Frontend] Redirecting to accounts page...'); window.location.href = '/accounts'; // Use direct navigation instead of React Router }, 2000); } else { console.log('🔗 [Frontend] OAuth successful but account not linked'); setStatus('processing'); setMessage('Processing account linking...'); // The backend should handle everything, so we just wait setTimeout(() => { setStatus('success'); setMessage('LinkedIn account linked successfully!'); dispatch(fetchLinkedInAccounts()); setTimeout(() => navigate('/sources'), 2000); }, 1000); } return; } } // Fallback: if this looks like an OAuth callback but we don't recognize it if (location.search.includes('code=') || location.search.includes('error=')) { console.log('🔗 [Frontend] Unrecognized OAuth callback format'); setStatus('error'); setMessage('Authentication process incomplete. Please try again.'); return; } // Normal page load console.log('🔗 [Frontend] Not a callback URL, normal page load'); } 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=') || location.search.includes('oauth_success=')) { 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('/accounts'); }; return (
Please wait while we complete the authentication process...
Redirecting to your accounts...
There was an issue with the LinkedIn authentication process.