import React, { useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useLocation, useNavigate } from 'react-router-dom'; import { handleLinkedInCallback, clearLinkedInError } from '../../store/reducers/linkedinAccountsSlice'; 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 action dispatch console.log('🔗 [Frontend] Dispatching LinkedIn callback action...'); console.log('🔗 [Frontend] Callback payload:', { code: code.substring(0, 20) + '...', state }); // Dispatch the callback action const result = await dispatch(handleLinkedInCallback({ code, state })).unwrap(); // DEBUG: Log callback result console.log('🔗 [Frontend] Callback result:', result); if (result.success) { console.log('🔗 [Frontend] LinkedIn account linked successfully!'); setStatus('success'); setMessage('LinkedIn account linked successfully!'); // 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:', result.message); setStatus('error'); setMessage(result.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'); } }; handleCallback(); }, [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.