import React, { useState, useEffect } from 'react'; import { X, Key, Lock, AlertCircle, CheckCircle, Save } from 'lucide-react'; interface AuthSettingsProps { isOpen: boolean; onClose: () => void; onSave: (apiKey: string, remember: boolean) => void; currentSignedConfig?: string; rememberCredentials: boolean; } // Helper function to redact sensitive credentials const redactCredential = (credential: string | undefined): string => { if (!credential) return ''; // For API keys in format "appId.keyId:secret" if (credential.includes(':')) { const [keyName] = credential.split(':'); // Show full app ID and key ID, but redact the secret return `${keyName}:****`; } // For tokens, show first few and last few characters if (credential.length > 20) { return `${credential.substring(0, 6)}...${credential.substring(credential.length - 4)}`; } return credential.substring(0, 4) + '...'; }; // Helper to extract API key from signed config const extractApiKey = (signedConfig: string | undefined): string => { if (!signedConfig) return ''; try { const config = JSON.parse(signedConfig); return config.apiKey || ''; } catch { return ''; } }; export const AuthSettings: React.FC = ({ isOpen, onClose, onSave, currentSignedConfig = '', rememberCredentials }) => { const currentApiKey = extractApiKey(currentSignedConfig); const [apiKey, setApiKey] = useState(currentApiKey); const [remember, setRemember] = useState(rememberCredentials); const [error, setError] = useState(''); useEffect(() => { setApiKey(extractApiKey(currentSignedConfig)); setRemember(rememberCredentials); }, [currentSignedConfig, rememberCredentials, isOpen]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (!apiKey.trim()) { setError('API Key is required'); return; } // Basic validation for API key format if (!apiKey.includes(':')) { setError('API Key should be in the format: app_name.key_name:key_secret'); return; } try { await onSave(apiKey.trim(), remember); } catch (error) { console.error('[AuthSettings] Save failed:', error); setError(error instanceof Error ? error.message : 'Failed to save credentials'); } }; if (!isOpen) return null; return (

Authentication Settings

{currentApiKey && (

Current Credentials

API Key: {redactCredential(currentApiKey)}

)}
setApiKey(e.target.value)} placeholder="your_app.key_name:key_secret" className="w-full px-3 py-2 bg-gray-800 border border-gray-700 rounded-md text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" />

Find your API key in your Ably dashboard

setRemember(e.target.checked)} className="w-4 h-4 text-blue-600 bg-gray-800 border-gray-700 rounded focus:ring-blue-500" />
{error && (
{error}
)}
); };