# Integration Guide: Adding Domain Verification to api.aethex.cloud

## Overview
This guide shows how to integrate the domain verification feature into your existing api.aethex.cloud backend.

## Files to Copy to Your API Backend

### 1. Core Utilities
Copy these to your api.aethex.cloud project:

```
📁 Your API Project/
├── utils/
│   └── domainVerification.js   ← Copy from src/backend/utils/
└── middleware/
    └── auth.js                  ← Update if needed (dev mode auth)
```

### 2. Routes
Copy and integrate:

```javascript
// In your api.aethex.cloud routes/index.js or app.js
const domainRoutes = require('./routes/domainRoutes');

// Mount the domain verification routes
app.use('/api/passport/domain', domainRoutes);
```

### 3. Database Migration
Run this SQL on your Supabase database:

```bash
# Option 1: Via Supabase Dashboard
# Go to https://supabase.com/dashboard/project/kmdeisowhtsalsekkzqd/editor
# Run the SQL from: src/backend/database/migrations/001_domain_verifications.sql

# Option 2: Via CLI (if you're in your API project with supabase linked)
supabase db push
```

## Integration Steps

### Step 1: Copy Files to Your API Project

```bash
# In your api.aethex.cloud project
mkdir -p utils routes

# Copy domain verification utilities
cp /path/to/AeThex-Connect/src/backend/utils/domainVerification.js utils/

# Copy domain routes
cp /path/to/AeThex-Connect/src/backend/routes/domainRoutes.js routes/
```

### Step 2: Install Dependencies (if not already installed)

```bash
npm install ethers --save
# (pg, crypto, dns are Node.js built-ins)
```

### Step 3: Add Routes to Your Express App

In your `app.js` or `server.js`:

```javascript
// Import the domain routes
const domainRoutes = require('./routes/domainRoutes');

// Mount at the correct path
app.use('/api/passport/domain', domainRoutes);
```

### Step 4: Environment Variables

Add to your api.aethex.cloud `.env`:

```env
# Blockchain Configuration (for .aethex domain verification)
RPC_ENDPOINT=https://polygon-mainnet.g.alchemy.com/v2/3-qjAZSq7DyEuJQKH3KPm
FREENAME_REGISTRY_ADDRESS=0x... # Add the actual contract address
```

### Step 5: Database Schema

The tables are already created on your Supabase database:
- ✅ `domain_verifications`
- ✅ `users` (extended with `verified_domain` and `domain_verified_at`)

### Step 6: Test the Integration

```bash
# Test the API endpoint
curl https://api.aethex.cloud/api/passport/domain/status \
  -H "Authorization: Bearer YOUR_TOKEN"

# Expected: {"hasVerifiedDomain": false} or your verification status
```

## Frontend Integration

### Update Your aethex.tech Frontend

The frontend is already configured to call `api.aethex.cloud`:

```javascript
import DomainVerification from './components/DomainVerification';

// In your profile/settings page:
<DomainVerification />
// It will automatically call https://api.aethex.cloud/api/passport/domain/*
```

### Add the Components

Copy these to your aethex.tech frontend:

```
📁 Your Frontend (aethex.tech)/
├── components/
│   ├── DomainVerification.jsx
│   ├── DomainVerification.css
│   ├── VerifiedDomainBadge.jsx
│   └── VerifiedDomainBadge.css
```

## API Endpoints Added

Once integrated, these endpoints will be available at api.aethex.cloud:

| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/passport/domain/request-verification` | Generate verification token |
| POST | `/api/passport/domain/verify` | Verify domain ownership |
| GET | `/api/passport/domain/status` | Get user's verification status |

## Authentication

The routes use your existing auth middleware. Make sure:
- JWT tokens are passed in `Authorization: Bearer <token>` header
- Middleware extracts `req.user.id` and `req.user.email`

If your auth is different, update `domainRoutes.js` accordingly.

## Quick Copy-Paste for Your API

### In your api.aethex.cloud app.js:

```javascript
// Add this line with your other route imports
const domainRoutes = require('./routes/domainRoutes');

// Add this line with your other route mounts
app.use('/api/passport/domain', domainRoutes);
```

### In your api.aethex.cloud database setup:

```sql
-- Run in Supabase SQL Editor
-- This creates the domain verification tables
-- (Already done if you ran the migration earlier)
```

## Testing Checklist

- [ ] Copy `domainVerification.js` to your API utils
- [ ] Copy `domainRoutes.js` to your API routes
- [ ] Add route mount in your Express app
- [ ] Verify database tables exist in Supabase
- [ ] Test endpoint: `GET /api/passport/domain/status`
- [ ] Copy frontend components to aethex.tech
- [ ] Test full flow: request → add DNS → verify

## Need Help?

The current standalone server at `localhost:3000` is just for testing. Once integrated into api.aethex.cloud, you can remove this standalone server.

All the backend code is modular and ready to plug into your existing API!
