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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
return app
|
| 201 |
|
| 202 |
if __name__ == '__main__':
|
|
|
|
| 197 |
'message': f'Health check failed: {str(e)}'
|
| 198 |
}, 503
|
| 199 |
|
| 200 |
+
# Add OAuth callback handler route
|
| 201 |
+
@app.route('/auth/callback')
|
| 202 |
+
def handle_auth_callback():
|
| 203 |
+
"""Handle OAuth callback from social networks."""
|
| 204 |
+
try:
|
| 205 |
+
# Parse URL parameters
|
| 206 |
+
import requests
|
| 207 |
+
from urllib.parse import parse_qs, urlparse
|
| 208 |
+
|
| 209 |
+
url = request.url
|
| 210 |
+
parsed_url = urlparse(url)
|
| 211 |
+
query_params = parse_qs(parsed_url.query)
|
| 212 |
+
|
| 213 |
+
code = query_params.get('code', [None])[0]
|
| 214 |
+
state = query_params.get('state', [None])[0]
|
| 215 |
+
error = query_params.get('error', [None])[0]
|
| 216 |
+
|
| 217 |
+
app.logger.info(f"π [OAuth] Direct callback handler triggered")
|
| 218 |
+
app.logger.info(f"π [OAuth] URL: {url}")
|
| 219 |
+
app.logger.info(f"π [OAuth] Code: {code[:20] + '...' if code else None}")
|
| 220 |
+
app.logger.info(f"π [OAuth] State: {state}")
|
| 221 |
+
app.logger.info(f"π [OAuth] Error: {error}")
|
| 222 |
+
|
| 223 |
+
if error:
|
| 224 |
+
app.logger.error(f"π [OAuth] OAuth error: {error}")
|
| 225 |
+
# Return error page or redirect with error
|
| 226 |
+
return f"""
|
| 227 |
+
<html>
|
| 228 |
+
<head><title>Authentication Error</title></head>
|
| 229 |
+
<body>
|
| 230 |
+
<h2>Authentication Failed</h2>
|
| 231 |
+
<p>Error: {error}</p>
|
| 232 |
+
<p><a href="/sources">Go back to sources</a></p>
|
| 233 |
+
<script>
|
| 234 |
+
setTimeout(() => {{
|
| 235 |
+
window.location.href = '/sources';
|
| 236 |
+
}}, 3000);
|
| 237 |
+
</script>
|
| 238 |
+
</body>
|
| 239 |
+
</html>
|
| 240 |
+
""", 400
|
| 241 |
+
|
| 242 |
+
if not code or not state:
|
| 243 |
+
app.logger.error(f"π [OAuth] Missing required parameters")
|
| 244 |
+
return """
|
| 245 |
+
<html>
|
| 246 |
+
<head><title>Invalid Callback</title></head>
|
| 247 |
+
<body>
|
| 248 |
+
<h2>Invalid Callback</h2>
|
| 249 |
+
<p>Missing required parameters.</p>
|
| 250 |
+
<p><a href="/sources">Go back to sources</a></p>
|
| 251 |
+
<script>
|
| 252 |
+
setTimeout(() => {{
|
| 253 |
+
window.location.href = '/sources';
|
| 254 |
+
}}, 3000);
|
| 255 |
+
</script>
|
| 256 |
+
</body>
|
| 257 |
+
</html>
|
| 258 |
+
""", 400
|
| 259 |
+
|
| 260 |
+
# Forward the callback to the accounts API
|
| 261 |
+
app.logger.info(f"π [OAuth] Forwarding callback to accounts API")
|
| 262 |
+
|
| 263 |
+
# Get the JWT token from cookies
|
| 264 |
+
from flask_jwt_extended import jwt_required, get_jwt_identity, create_access_token
|
| 265 |
+
import jwt
|
| 266 |
+
|
| 267 |
+
# Try to get token from cookies
|
| 268 |
+
token = request.cookies.get('access_token')
|
| 269 |
+
if not token:
|
| 270 |
+
app.logger.error(f"π [OAuth] No token found in cookies")
|
| 271 |
+
return """
|
| 272 |
+
<html>
|
| 273 |
+
<head><title>Authentication Error</title></head>
|
| 274 |
+
<body>
|
| 275 |
+
<h2>Authentication Error</h2>
|
| 276 |
+
<p>No authentication token found.</p>
|
| 277 |
+
<p><a href="/login">Please login again</a></p>
|
| 278 |
+
</body>
|
| 279 |
+
</html>
|
| 280 |
+
""", 401
|
| 281 |
+
|
| 282 |
+
# Make the callback request to the accounts API
|
| 283 |
+
api_url = f"{request.host_url.rstrip('/')}/api/accounts/callback"
|
| 284 |
+
headers = {
|
| 285 |
+
'Content-Type': 'application/json',
|
| 286 |
+
'Authorization': f'Bearer {token}'
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
callback_data = {
|
| 290 |
+
'code': code,
|
| 291 |
+
'state': state,
|
| 292 |
+
'social_network': 'LinkedIn'
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
app.logger.info(f"π [OAuth] Making POST request to {api_url}")
|
| 296 |
+
|
| 297 |
+
response = requests.post(api_url, json=callback_data, headers=headers, timeout=30)
|
| 298 |
+
|
| 299 |
+
app.logger.info(f"π [OAuth] API response status: {response.status_code}")
|
| 300 |
+
app.logger.info(f"π [OAuth] API response data: {response.text}")
|
| 301 |
+
|
| 302 |
+
if response.status_code == 200:
|
| 303 |
+
result = response.json()
|
| 304 |
+
if result.get('success'):
|
| 305 |
+
app.logger.info(f"π [OAuth] Account linked successfully")
|
| 306 |
+
# Return success page
|
| 307 |
+
return f"""
|
| 308 |
+
<html>
|
| 309 |
+
<head><title>Authentication Successful</title></head>
|
| 310 |
+
<body>
|
| 311 |
+
<h2>Authentication Successful!</h2>
|
| 312 |
+
<p>Your LinkedIn account has been linked successfully.</p>
|
| 313 |
+
<p><a href="/sources">Go to your sources</a></p>
|
| 314 |
+
<script>
|
| 315 |
+
setTimeout(() => {{
|
| 316 |
+
window.location.href = '/sources';
|
| 317 |
+
}}, 2000);
|
| 318 |
+
</script>
|
| 319 |
+
</body>
|
| 320 |
+
</html>
|
| 321 |
+
"""
|
| 322 |
+
else:
|
| 323 |
+
app.logger.error(f"π [OAuth] Account linking failed: {result.get('message')}")
|
| 324 |
+
return f"""
|
| 325 |
+
<html>
|
| 326 |
+
<head><title>Authentication Failed</title></head>
|
| 327 |
+
<body>
|
| 328 |
+
<h2>Authentication Failed</h2>
|
| 329 |
+
<p>{result.get('message', 'Unknown error')}</p>
|
| 330 |
+
<p><a href="/sources">Go back to sources</a></p>
|
| 331 |
+
<script>
|
| 332 |
+
setTimeout(() => {{
|
| 333 |
+
window.location.href = '/sources';
|
| 334 |
+
}}, 3000);
|
| 335 |
+
</script>
|
| 336 |
+
</body>
|
| 337 |
+
</html>
|
| 338 |
+
""", 400
|
| 339 |
+
else:
|
| 340 |
+
app.logger.error(f"π [OAuth] API request failed: {response.status_code}")
|
| 341 |
+
return f"""
|
| 342 |
+
<html>
|
| 343 |
+
<head><title>Authentication Error</title></head>
|
| 344 |
+
<body>
|
| 345 |
+
<h2>Authentication Error</h2>
|
| 346 |
+
<p>Failed to complete authentication: {response.status_code}</p>
|
| 347 |
+
<p><a href="/sources">Go back to sources</a></p>
|
| 348 |
+
<script>
|
| 349 |
+
setTimeout(() => {{
|
| 350 |
+
window.location.href = '/sources';
|
| 351 |
+
}}, 3000);
|
| 352 |
+
</script>
|
| 353 |
+
</body>
|
| 354 |
+
</html>
|
| 355 |
+
""", 500
|
| 356 |
+
|
| 357 |
+
except Exception as e:
|
| 358 |
+
app.logger.error(f"π [OAuth] Callback handler error: {str(e)}")
|
| 359 |
+
import traceback
|
| 360 |
+
app.logger.error(f"π [OAuth] Traceback: {traceback.format_exc()}")
|
| 361 |
+
return f"""
|
| 362 |
+
<html>
|
| 363 |
+
<head><title>Authentication Error</title></head>
|
| 364 |
+
<body>
|
| 365 |
+
<h2>Authentication Error</h2>
|
| 366 |
+
<p>An error occurred during authentication: {str(e)}</p>
|
| 367 |
+
<p><a href="/sources">Go back to sources</a></p>
|
| 368 |
+
<script>
|
| 369 |
+
setTimeout(() => {{
|
| 370 |
+
window.location.href = '/sources';
|
| 371 |
+
}}, 3000);
|
| 372 |
+
</script>
|
| 373 |
+
</body>
|
| 374 |
+
</html>
|
| 375 |
+
""", 500
|
| 376 |
+
|
| 377 |
return app
|
| 378 |
|
| 379 |
if __name__ == '__main__':
|
frontend/src/components/LinkedInAccount/LinkedInCallbackHandler.jsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import React, { useEffect, useState } from 'react';
|
| 2 |
import { useDispatch, useSelector } from 'react-redux';
|
| 3 |
import { useLocation, useNavigate } from 'react-router-dom';
|
| 4 |
-
import { handleLinkedInCallback, clearLinkedInError } from '../../store/reducers/linkedinAccountsSlice';
|
|
|
|
| 5 |
|
| 6 |
const LinkedInCallbackHandler = () => {
|
| 7 |
const dispatch = useDispatch();
|
|
@@ -42,30 +43,39 @@ const LinkedInCallbackHandler = () => {
|
|
| 42 |
setStatus('processing');
|
| 43 |
setMessage('Completing LinkedIn authentication...');
|
| 44 |
|
| 45 |
-
// DEBUG: Log callback
|
| 46 |
-
console.log('π [Frontend]
|
| 47 |
-
console.log('π [Frontend]
|
| 48 |
|
| 49 |
-
//
|
| 50 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
// DEBUG: Log callback
|
| 53 |
-
console.log('π [Frontend]
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
if (
|
| 56 |
console.log('π [Frontend] LinkedIn account linked successfully!');
|
| 57 |
setStatus('success');
|
| 58 |
setMessage('LinkedIn account linked successfully!');
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
// Redirect to sources page after a short delay
|
| 61 |
setTimeout(() => {
|
| 62 |
console.log('π [Frontend] Redirecting to sources page...');
|
| 63 |
navigate('/sources');
|
| 64 |
}, 2000);
|
| 65 |
} else {
|
| 66 |
-
console.error('π [Frontend] LinkedIn account linking failed:',
|
| 67 |
setStatus('error');
|
| 68 |
-
setMessage(
|
| 69 |
}
|
| 70 |
} catch (error) {
|
| 71 |
console.error('π [Frontend] Callback handler error:', error);
|
|
@@ -79,7 +89,13 @@ const LinkedInCallbackHandler = () => {
|
|
| 79 |
}
|
| 80 |
};
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}, [dispatch, location, navigate]);
|
| 84 |
|
| 85 |
const handleRetry = () => {
|
|
|
|
| 1 |
import React, { useEffect, useState } from 'react';
|
| 2 |
import { useDispatch, useSelector } from 'react-redux';
|
| 3 |
import { useLocation, useNavigate } from 'react-router-dom';
|
| 4 |
+
import { handleLinkedInCallback, clearLinkedInError, fetchLinkedInAccounts } from '../../store/reducers/linkedinAccountsSlice';
|
| 5 |
+
import apiClient from '../../services/apiClient';
|
| 6 |
|
| 7 |
const LinkedInCallbackHandler = () => {
|
| 8 |
const dispatch = useDispatch();
|
|
|
|
| 43 |
setStatus('processing');
|
| 44 |
setMessage('Completing LinkedIn authentication...');
|
| 45 |
|
| 46 |
+
// DEBUG: Log callback processing
|
| 47 |
+
console.log('π [Frontend] Processing OAuth callback...');
|
| 48 |
+
console.log('π [Frontend] Making API call to complete OAuth flow...');
|
| 49 |
|
| 50 |
+
// Make the API call to complete the OAuth flow
|
| 51 |
+
const response = await apiClient.post('/accounts/callback', {
|
| 52 |
+
code: code,
|
| 53 |
+
state: state,
|
| 54 |
+
social_network: 'LinkedIn'
|
| 55 |
+
});
|
| 56 |
|
| 57 |
+
// DEBUG: Log callback response
|
| 58 |
+
console.log('π [Frontend] API response received:', response);
|
| 59 |
+
console.log('π [Frontend] Response status:', response.status);
|
| 60 |
+
console.log('π [Frontend] Response data:', response.data);
|
| 61 |
|
| 62 |
+
if (response.data.success) {
|
| 63 |
console.log('π [Frontend] LinkedIn account linked successfully!');
|
| 64 |
setStatus('success');
|
| 65 |
setMessage('LinkedIn account linked successfully!');
|
| 66 |
|
| 67 |
+
// Dispatch success action to update Redux state
|
| 68 |
+
await dispatch(fetchLinkedInAccounts());
|
| 69 |
+
|
| 70 |
// Redirect to sources page after a short delay
|
| 71 |
setTimeout(() => {
|
| 72 |
console.log('π [Frontend] Redirecting to sources page...');
|
| 73 |
navigate('/sources');
|
| 74 |
}, 2000);
|
| 75 |
} else {
|
| 76 |
+
console.error('π [Frontend] LinkedIn account linking failed:', response.data.message);
|
| 77 |
setStatus('error');
|
| 78 |
+
setMessage(response.data.message || 'Failed to link LinkedIn account');
|
| 79 |
}
|
| 80 |
} catch (error) {
|
| 81 |
console.error('π [Frontend] Callback handler error:', error);
|
|
|
|
| 89 |
}
|
| 90 |
};
|
| 91 |
|
| 92 |
+
// Check if we're on the callback URL
|
| 93 |
+
if (location.search.includes('code=') || location.search.includes('error=')) {
|
| 94 |
+
console.log('π [Frontend] Detected callback URL, processing...');
|
| 95 |
+
handleCallback();
|
| 96 |
+
} else {
|
| 97 |
+
console.log('π [Frontend] Not a callback URL, normal page load');
|
| 98 |
+
}
|
| 99 |
}, [dispatch, location, navigate]);
|
| 100 |
|
| 101 |
const handleRetry = () => {
|
frontend/src/services/linkedinAuthService.js
CHANGED
|
@@ -44,29 +44,21 @@ class LinkedInAuthService {
|
|
| 44 |
async handleCallback(code, state) {
|
| 45 |
try {
|
| 46 |
// DEBUG: Log callback request
|
| 47 |
-
console.log('π [LinkedIn]
|
| 48 |
console.log('π [LinkedIn] Callback payload:', {
|
| 49 |
code: code.substring(0, 20) + '...',
|
| 50 |
state: state,
|
| 51 |
social_network: 'LinkedIn'
|
| 52 |
});
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
social_network: 'LinkedIn'
|
| 58 |
-
});
|
| 59 |
-
|
| 60 |
-
// DEBUG: Log callback response
|
| 61 |
-
console.log('π [LinkedIn] Backend response received:', response);
|
| 62 |
-
console.log('π [LinkedIn] Response status:', response.status);
|
| 63 |
-
console.log('π [LinkedIn] Response data:', response.data);
|
| 64 |
-
|
| 65 |
-
if (import.meta.env.VITE_NODE_ENV === 'development') {
|
| 66 |
-
console.log('π [LinkedIn] OAuth callback handled successfully');
|
| 67 |
-
}
|
| 68 |
|
| 69 |
-
return
|
|
|
|
|
|
|
|
|
|
| 70 |
} catch (error) {
|
| 71 |
// DEBUG: Log callback error
|
| 72 |
console.error('π [LinkedIn] OAuth callback error:', error);
|
|
@@ -77,9 +69,6 @@ class LinkedInAuthService {
|
|
| 77 |
headers: error.response?.headers
|
| 78 |
});
|
| 79 |
|
| 80 |
-
if (import.meta.env.VITE_NODE_ENV === 'development') {
|
| 81 |
-
console.error('π [LinkedIn] OAuth callback error:', error.response?.data || error.message);
|
| 82 |
-
}
|
| 83 |
return {
|
| 84 |
success: false,
|
| 85 |
message: error.response?.data?.message || 'Failed to complete LinkedIn authentication'
|
|
|
|
| 44 |
async handleCallback(code, state) {
|
| 45 |
try {
|
| 46 |
// DEBUG: Log callback request
|
| 47 |
+
console.log('π [LinkedIn] Processing OAuth callback...');
|
| 48 |
console.log('π [LinkedIn] Callback payload:', {
|
| 49 |
code: code.substring(0, 20) + '...',
|
| 50 |
state: state,
|
| 51 |
social_network: 'LinkedIn'
|
| 52 |
});
|
| 53 |
|
| 54 |
+
// The callback is now handled by the frontend callback handler
|
| 55 |
+
// We just need to return success to indicate the callback was processed
|
| 56 |
+
console.log('π [LinkedIn] Callback processing initiated by frontend handler');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
return {
|
| 59 |
+
success: true,
|
| 60 |
+
message: 'OAuth callback processing initiated'
|
| 61 |
+
};
|
| 62 |
} catch (error) {
|
| 63 |
// DEBUG: Log callback error
|
| 64 |
console.error('π [LinkedIn] OAuth callback error:', error);
|
|
|
|
| 69 |
headers: error.response?.headers
|
| 70 |
});
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
return {
|
| 73 |
success: false,
|
| 74 |
message: error.response?.data?.message || 'Failed to complete LinkedIn authentication'
|