# Environment Setup Guide

## Issue: ECONNREFUSED 127.0.0.1:1025

The error occurs because your application is trying to connect to an SMTP server on port 1025, but no email configuration is set up.

## Solution

### 1. Create a `.env` file in your project root with the following configuration:

```env
# Server Configuration
PORT=6969
NODE_ENV=development

# Database Configuration
DB_URL=mongodb://localhost:27017/contractor4industry

# Email Configuration (SMTP)
# Choose ONE of the following email providers:

# Option 1: Gmail SMTP (Recommended for development)
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_FROM=Contractor4Industry <your-email@gmail.com>

# Option 2: Outlook/Hotmail SMTP
# MAIL_HOST=smtp-mail.outlook.com
# MAIL_PORT=587
# MAIL_USERNAME=your-email@outlook.com
# MAIL_PASSWORD=your-password

# Option 3: Yahoo SMTP
# MAIL_HOST=smtp.mail.yahoo.com
# MAIL_PORT=587
# MAIL_USERNAME=your-email@yahoo.com
# MAIL_PASSWORD=your-app-password

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-here
AUTH_SECRET=your-auth-secret-key-here

# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key_here
SUCCESS_URL=http://localhost:3000/success
CANCEL_URL=http://localhost:3000/cancel
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here

# Frontend Configuration
FRONTEND_URL=http://localhost:3000
```

### 2. Gmail Setup (Recommended)

If using Gmail, you need to:

1. Enable 2-factor authentication on your Google account
2. Generate an "App Password" for your application
3. Use the app password (not your regular password) in the `MAIL_PASSWORD` field

**Steps to get Gmail App Password:**
1. Go to Google Account settings
2. Security → 2-Step Verification → App passwords
3. Generate a new app password for "Mail"
4. Use this 16-character password in your `.env` file

### 3. Alternative: Use a Development Email Service

For development, you can use services like:
- **Mailtrap** (for testing emails)
- **Ethereal Email** (fake SMTP for development)
- **MailHog** (local SMTP server)

**Ethereal Email Configuration:**
```env
MAIL_HOST=smtp.ethereal.email
MAIL_PORT=587
MAIL_USERNAME=your-ethereal-username
MAIL_PASSWORD=your-ethereal-password
```

### 4. Test Your Configuration

After setting up the `.env` file, restart your server:

```bash
npm run dev
```

Then test the registration endpoint:
```bash
curl -X POST http://localhost:6969/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "password": "password123",
    "userType": "manufacturer"
  }'
```

### 5. Common Issues and Solutions

**Issue**: Still getting ECONNREFUSED
**Solution**: Make sure your email provider allows SMTP connections and check firewall settings

**Issue**: Authentication failed
**Solution**: Verify your email credentials and app password

**Issue**: Port 1025 still being used
**Solution**: Check if you have any other `.env` files or environment variables overriding your settings

### 6. Production Considerations

For production, consider using:
- **SendGrid** (recommended for production)
- **Amazon SES**
- **Mailgun**
- **Postmark**

These services are more reliable and have better deliverability than personal email accounts.
