/** * Auth Callback - PRODUCTION READY * Handles OAuth redirects and email confirmations */ import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { auth } from '../lib/auth-client'; export default function AuthCallback() { const navigate = useNavigate(); const [error, setError] = useState(null); const [isEmailConfirm, setIsEmailConfirm] = useState(false); useEffect(() => { let isMounted = true; let attempts = 0; // Check for error in URL const params = new URLSearchParams(window.location.search); const hashParams = new URLSearchParams(window.location.hash.substring(1)); const urlError = params.get('error') || hashParams.get('error'); // Check if this is an email confirmation callback const type = hashParams.get('type') || params.get('type'); if (type === 'signup' || type === 'email_confirmation' || type === 'recovery') { setIsEmailConfirm(true); } if (urlError) { const desc = params.get('error_description') || hashParams.get('error_description'); setError(desc || urlError); return; } // Native OAuth token from FastAPI const customToken = params.get('token'); if (customToken) { localStorage.setItem('auth_token', customToken); fetch(`${import.meta.env.VITE_API_URL || ''}/api/v1/auth/me`, { headers: { 'Authorization': `Bearer ${customToken}` } }) .then(res => res.json()) .then(data => { if (data.success && isMounted) { localStorage.setItem('auth_user', JSON.stringify(data.user)); localStorage.setItem('userId', data.user.id); window.location.href = '/data-hub'; } else if (isMounted) { setError('Failed to fetch user profile'); } }) .catch(() => { if (isMounted) setError('Authentication error'); }); return; } // Poll for session (Legacy fallback) const checkSession = async () => { attempts++; const { data: { session } } = await auth.getSession(); if (!isMounted) return; if (session) { // Store userId for API calls localStorage.setItem('userId', session.user.id); // If email confirmation, show success briefly then redirect to data-hub if (isEmailConfirm) { setTimeout(() => window.location.href = '/data-hub', 1500); } else { // OAuth login - go directly to data-hub window.location.href = '/data-hub'; } } else if (attempts < 20) { // Keep polling (10 seconds max) setTimeout(checkSession, 500); } else { // No session - might be just email confirmation without auto-login if (isEmailConfirm) { setTimeout(() => navigate('/login', { replace: true }), 2000); } else { setError('Authentication timeout. Please try again.'); } } }; // Wait a bit for URL tokens to be processed, then start checking setTimeout(checkSession, 300); return () => { isMounted = false; }; }, [navigate, isEmailConfirm]); if (error) { return (

Login Failed

{error}

Try Again
); } // Email confirmation success view if (isEmailConfirm) { return (

Email Confirmed! 🎉

Your account is now verified.

Redirecting...

); } return (

Completing login...

); }