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 (

✓ Domain Verified

{currentStatus.domain} {currentStatus.verificationType === 'blockchain' ? 'Blockchain' : 'DNS'}

Verified on {new Date(currentStatus.verifiedAt).toLocaleDateString()}

); } return (

Verify Your Domain

Prove ownership of a domain to display it on your profile

{error && (
⚠️ {error}
)} {!verificationInstructions ? (
setDomain(e.target.value.toLowerCase().trim())} disabled={loading} className="domain-input" /> Enter a traditional domain (e.g., dev.aethex.dev) or a .aethex blockchain domain
) : (

{domain.endsWith('.aethex') ? 'Connect Your Wallet' : `Add this DNS record to ${verificationInstructions.domain}:` }

{domain.endsWith('.aethex') ? ( // Blockchain verification

Connect the wallet that owns {domain}

setWalletAddress(e.target.value.trim())} disabled={loading} className="wallet-input" />
) : ( // DNS verification
Type: {verificationInstructions.recordType}
Name: {verificationInstructions.recordName}
Value:
{verificationInstructions.recordValue}

How to add DNS records:

  1. Go to your domain's DNS settings (Google Domains, Cloudflare, etc.)
  2. Add a new TXT record with the values above
  3. Wait 5-10 minutes for DNS to propagate
  4. Click "Verify Domain" below

⏱️ This verification expires on {new Date(verificationInstructions.expiresAt).toLocaleDateString()}

{verificationStatus && (
{verificationStatus.verified ? ( ✓ Domain verified successfully! ) : ( ✗ {verificationStatus.error} )}
)}
)}
)}
); }