import React, { useState, useEffect } from 'react'; import './DomainVerification.css'; /** * Domain verification UI component * Allows users to verify domain ownership via DNS TXT records or blockchain */ export default function DomainVerification({ apiBaseUrl = 'https://api.aethex.cloud/api/passport/domain' }) { const [domain, setDomain] = useState(''); const [walletAddress, setWalletAddress] = useState(''); const [verificationInstructions, setVerificationInstructions] = useState(null); const [verificationStatus, setVerificationStatus] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [currentStatus, setCurrentStatus] = useState(null); // Load current verification status on mount useEffect(() => { loadCurrentStatus(); }, []); /** * Load current verification status */ async function loadCurrentStatus() { try { const token = localStorage.getItem('authToken'); const response = await fetch(`${apiBaseUrl}/status`, { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const data = await response.json(); setCurrentStatus(data); } } catch (err) { console.error('Failed to load status:', err); } } /** * Request verification token from backend */ async function requestVerification() { if (!domain) { setError('Please enter a domain'); return; } setLoading(true); setError(null); try { const token = localStorage.getItem('authToken'); const response = await fetch(`${apiBaseUrl}/request-verification`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ domain }) }); const data = await response.json(); if (data.success) { setVerificationInstructions(data.verification); } else { setError(data.error || 'Failed to request verification'); } } catch (err) { setError('Network error. Please try again.'); console.error('Failed to request verification:', err); } finally { setLoading(false); } } /** * Verify domain ownership by checking DNS or blockchain */ async function verifyDomain() { setLoading(true); setError(null); try { const token = localStorage.getItem('authToken'); const body = { domain }; // Add wallet address if verifying .aethex domain if (domain.endsWith('.aethex')) { body.walletAddress = walletAddress; } const response = await fetch(`${apiBaseUrl}/verify`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(body) }); const data = await response.json(); setVerificationStatus(data); if (data.verified) { // Refresh status and reload after short delay setTimeout(() => { loadCurrentStatus(); window.location.reload(); }, 1500); } else { setError(data.error || 'Verification failed'); } } catch (err) { setError('Network error. Please try again.'); console.error('Verification failed:', err); } finally { setLoading(false); } } /** * Copy text to clipboard */ function copyToClipboard(text) { navigator.clipboard.writeText(text); // You could add a toast notification here alert('Copied to clipboard!'); } /** * Reset form */ function resetForm() { setVerificationInstructions(null); setVerificationStatus(null); setDomain(''); setWalletAddress(''); setError(null); } // Show current verified domain if exists if (currentStatus?.hasVerifiedDomain) { return (
Verified on {new Date(currentStatus.verifiedAt).toLocaleDateString()}
Prove ownership of a domain to display it on your profile
{error && (Connect the wallet that owns {domain}
{verificationInstructions.recordValue}
How to add DNS records:
⏱️ This verification expires on {new Date(verificationInstructions.expiresAt).toLocaleDateString()}