UNPKG

@defikitdotnet/x-ai-combat

Version:

XCombatAI - Social Media Engagement Template for the Agent Framework

88 lines 5.41 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useEffect, useState } from 'react'; import { useRouter, useParams } from 'next/navigation'; import { useAuth } from '../contexts/AuthContext'; import { useSearchParams } from 'next/navigation'; const AuthCallback = ({ onSuccess, onError }) => { const { login } = useAuth(); const router = useRouter(); const params = useParams(); const searchParams = useSearchParams(); const [status, setStatus] = useState('loading'); const [error, setError] = useState(null); useEffect(() => { const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:3005'; const handleCallback = async () => { const maxRetries = 5; let retryCount = 0; const delay = 1000; while (retryCount < maxRetries) { try { const errorParam = searchParams ? searchParams.get('error') : null; const agentId = Array.isArray(params?.id) ? params.id[0] : params?.id; if (!agentId) { throw new Error('Agent ID is missing in the URL'); } if (errorParam) { throw new Error(errorParam); } const response = await fetch(`${backendUrl}/xaicombat/api/auth/check`, { method: 'GET', credentials: 'include', }); if (!response.ok) { const errorData = await response.json(); if (response.status === 401 && retryCount < maxRetries - 1) { retryCount++; console.log(`Retry attempt ${retryCount}, waiting ${delay}ms...`); await new Promise(resolve => setTimeout(resolve, delay)); continue; } throw new Error(errorData.error || 'Authentication failed'); } const data = await response.json(); if (!data.jwt) { throw new Error('No authentication token received'); } await login(data.jwt); setStatus('success'); window.history.replaceState({}, document.title, window.location.pathname); if (onSuccess) { onSuccess(); } else { router.push(`/${agentId}/xaicombat`); } return; } catch (err) { console.error(`Error handling auth callback (attempt ${retryCount + 1}):`, err); if (retryCount === maxRetries - 1) { const errorMessage = err.message || 'Authentication failed'; setError(errorMessage); setStatus('error'); if (onError) { onError(errorMessage); } } retryCount++; if (retryCount < maxRetries) { console.log(`Retry attempt ${retryCount}, waiting ${delay}ms...`); await new Promise(resolve => setTimeout(resolve, delay)); } } } }; handleCallback(); }, [login, router, onSuccess, onError]); if (status === 'loading') { return (_jsx("div", { className: "flex flex-col items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-900 p-4", children: _jsxs("div", { className: "bg-background dark:bg-foreground p-8 rounded-lg shadow-md max-w-md w-full text-center", children: [_jsx("div", { className: "inline-block animate-spin h-8 w-8 border-4 border-brand-500 dark:border-brand-400 border-t-transparent rounded-full mb-4" }), _jsx("h1", { className: "text-xl font-semibold mb-2 text-foreground dark:text-background", children: "Processing your login..." }), _jsx("p", { className: "text-gray-600 dark:text-gray-400", children: "Please wait while we complete the authentication process." })] }) })); } if (status === 'error') { return (_jsx("div", { className: "flex flex-col items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-900 p-4", children: _jsxs("div", { className: "bg-background dark:bg-foreground p-8 rounded-lg shadow-md max-w-md w-full text-center", children: [_jsx("div", { className: "inline-flex items-center justify-center w-12 h-12 rounded-full bg-red-100 dark:bg-red-900/20 text-red-500 dark:text-red-400 mb-4", children: _jsx("svg", { className: "w-6 h-6", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M6 18L18 6M6 6l12 12" }) }) }), _jsx("h1", { className: "text-xl font-semibold mb-2 text-foreground dark:text-background", children: "Authentication Failed" }), _jsx("p", { className: "text-gray-600 dark:text-gray-400 mb-4", children: error || 'An error occurred during authentication.' })] }) })); } return null; }; export default AuthCallback; //# sourceMappingURL=AuthCallback.js.map