import { useEffect } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { useNetlifyOAuth } from '@/hooks/useNetlifyOAuth'; import { useDeploySettings } from '@/hooks/useDeploySettings'; import { useToast } from '@/hooks/useToast'; import { Card, CardContent } from '@/components/ui/card'; export default function NetlifyOAuth() { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const { handleCallback } = useNetlifyOAuth(); const { isInitialized } = useDeploySettings(); const { toast } = useToast(); useEffect(() => { // Wait for deploy settings to be initialized if (!isInitialized) { return; } const code = searchParams.get('code'); const state = searchParams.get('state'); const error = searchParams.get('error'); const errorDescription = searchParams.get('error_description'); const storedState = localStorage.getItem('netlify_oauth_state'); // Handle OAuth errors if (error) { toast({ title: "Netlify OAuth Error", description: errorDescription || error, variant: "destructive", }); navigate('/settings/deploy'); return; } // Handle successful OAuth callback if (code && state && storedState) { handleCallback(code, state).then((success) => { if (success) { toast({ title: "Netlify Connected", description: "Successfully connected to Netlify. You can now deploy your projects.", }); } else { toast({ title: "Connection Failed", description: "Failed to connect to Netlify. Please try again.", variant: "destructive", }); } // Always redirect to Deploy settings after handling the callback navigate('/settings/deploy'); }); } else { // No code or state parameters - redirect to Deploy settings navigate('/settings/deploy'); } }, [searchParams, handleCallback, toast, navigate, isInitialized]); return (
Processing your Netlify authentication...