// Password visibility toggle is handled by event delegation in account-settings.js // API Key Form Submission document.getElementById('apiKeysForm')?.addEventListener('submit', async function(e) { e.preventDefault(); const apiKey = document.getElementById('krakenApiKey').value.trim(); const apiSecret = document.getElementById('krakenApiSecret').value.trim(); // Basic validation if (!apiKey || !apiSecret) { showNotification('Both API Key and Secret are required', 'error'); return; } // Advanced validation (Kraken API keys are typically 56 chars) if (apiKey.length < 50 || apiKey.length > 60) { showNotification('API Key appears invalid (typically 56 characters)', 'warning'); // Continue despite warning } if (apiSecret.length < 50 || apiSecret.length > 100) { showNotification('API Secret appears invalid', 'warning'); // Continue despite warning } try { const response = await fetch('/api/apikey.php', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': '' }, body: JSON.stringify({ apiKey: apiKey, apiSecret: apiSecret }) }); const result = await response.json(); if (result.success) { showNotification('API keys saved successfully', 'success'); // Mask the secret in the UI after saving document.getElementById('krakenApiSecret').type = 'password'; document.querySelector('.toggle-password').textContent = '👁️'; // Update local cache if needed if (typeof updateApiKeyCache === 'function') { updateApiKeyCache(apiKey, apiSecret); } } else { showNotification(result.message || 'Failed to save API keys', 'error'); } } catch (error) { console.error('API Key Save Error:', error); showNotification('Network error - please try again', 'error'); } }); // Optional: Input formatting for better UX document.getElementById('krakenApiKey')?.addEventListener('input', function() { this.value = this.value.replace(/\s+/g, ''); }); document.getElementById('krakenApiSecret')?.addEventListener('input', function() { this.value = this.value.replace(/\s+/g, ''); });