UNPKG

@defikitdotnet/x-ai-combat

Version:

XCombatAI - Social Media Engagement Template for the Agent Framework

90 lines 5.79 kB
"use strict"; 'use client'; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const navigation_1 = require("next/navigation"); const AuthContext_1 = require("../contexts/AuthContext"); const navigation_2 = require("next/navigation"); const AuthCallback = ({ onSuccess, onError }) => { const { login } = (0, AuthContext_1.useAuth)(); const router = (0, navigation_1.useRouter)(); const params = (0, navigation_1.useParams)(); const searchParams = (0, navigation_2.useSearchParams)(); const [status, setStatus] = (0, react_1.useState)('loading'); const [error, setError] = (0, react_1.useState)(null); (0, react_1.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 ((0, jsx_runtime_1.jsx)("div", { className: "flex flex-col items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-900 p-4", children: (0, jsx_runtime_1.jsxs)("div", { className: "bg-background dark:bg-foreground p-8 rounded-lg shadow-md max-w-md w-full text-center", children: [(0, jsx_runtime_1.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" }), (0, jsx_runtime_1.jsx)("h1", { className: "text-xl font-semibold mb-2 text-foreground dark:text-background", children: "Processing your login..." }), (0, jsx_runtime_1.jsx)("p", { className: "text-gray-600 dark:text-gray-400", children: "Please wait while we complete the authentication process." })] }) })); } if (status === 'error') { return ((0, jsx_runtime_1.jsx)("div", { className: "flex flex-col items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-900 p-4", children: (0, jsx_runtime_1.jsxs)("div", { className: "bg-background dark:bg-foreground p-8 rounded-lg shadow-md max-w-md w-full text-center", children: [(0, jsx_runtime_1.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: (0, jsx_runtime_1.jsx)("svg", { className: "w-6 h-6", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: (0, jsx_runtime_1.jsx)("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M6 18L18 6M6 6l12 12" }) }) }), (0, jsx_runtime_1.jsx)("h1", { className: "text-xl font-semibold mb-2 text-foreground dark:text-background", children: "Authentication Failed" }), (0, jsx_runtime_1.jsx)("p", { className: "text-gray-600 dark:text-gray-400 mb-4", children: error || 'An error occurred during authentication.' })] }) })); } return null; }; exports.default = AuthCallback; //# sourceMappingURL=AuthCallback.js.map