# Email Verification Setup

This document provides step-by-step instructions to complete the email verification setup.

## ✅ Already Completed

1. ✅ Resend package installed
2. ✅ Prisma schema updated
3. ✅ Prisma client regenerated
4. ✅ Code implementation complete
5. ✅ API endpoints created
6. ✅ UI components updated

## 🔧 Remaining Steps

### Step 1: Run Database Migration

The database migration needs to be run as a PostgreSQL superuser (or a user with ALTER TABLE permissions).

**Option A: Using psql as postgres user**
```bash
sudo -u postgres psql -d punimtag_auth -f migrations/add-email-verification-columns.sql
```

**Option B: Using psql with password**
```bash
psql -U postgres -d punimtag_auth -f migrations/add-email-verification-columns.sql
```

**Option C: Manual SQL execution**
Connect to your database and run:
```sql
\c punimtag_auth

ALTER TABLE users 
ADD COLUMN IF NOT EXISTS email_verified BOOLEAN NOT NULL DEFAULT true;

ALTER TABLE users 
ADD COLUMN IF NOT EXISTS email_confirmation_token VARCHAR(255) UNIQUE;

ALTER TABLE users 
ADD COLUMN IF NOT EXISTS email_confirmation_token_expiry TIMESTAMP;

CREATE INDEX IF NOT EXISTS idx_users_email_confirmation_token ON users(email_confirmation_token);

UPDATE users 
SET email_verified = true 
WHERE email_confirmation_token IS NULL;
```

### Step 2: Set Up Resend

1. **Sign up for Resend:**
   - Go to [resend.com](https://resend.com)
   - Create a free account (3,000 emails/month free tier)

2. **Get your API key:**
   - Go to API Keys in your Resend dashboard
   - Create a new API key
   - Copy the key (starts with `re_`)

3. **Add to your `.env` file:**
   ```bash
   RESEND_API_KEY="re_your_api_key_here"
   RESEND_FROM_EMAIL="noreply@yourdomain.com"
   ```

   **For development/testing:**
   - You can use Resend's test domain: `onboarding@resend.dev`
   - No domain verification needed for testing

   **For production:**
   - Verify your domain in Resend dashboard
   - Use your verified domain: `noreply@yourdomain.com`

### Step 3: Verify Setup

1. **Check database columns:**
   ```sql
   \c punimtag_auth
   \d users
   ```
   You should see:
   - `email_verified` (boolean)
   - `email_confirmation_token` (varchar)
   - `email_confirmation_token_expiry` (timestamp)

2. **Test registration:**
   - Go to your registration page
   - Create a new account
   - Check your email for the confirmation message
   - Click the confirmation link
   - Try logging in

3. **Test resend:**
   - If email doesn't arrive, try the "Resend confirmation email" option on the login page

## 🔍 Troubleshooting

### "must be owner of table users"
- You need to run the migration as a PostgreSQL superuser
- Use `sudo -u postgres` or connect as the `postgres` user

### "Failed to send confirmation email"
- Check that `RESEND_API_KEY` is set correctly in `.env`
- Verify the API key is valid in Resend dashboard
- Check server logs for detailed error messages

### "Email not verified" error on login
- Make sure the user clicked the confirmation link
- Check that the token hasn't expired (24 hours)
- Use "Resend confirmation email" to get a new link

### Existing users can't log in
- The migration sets `email_verified = true` for existing users automatically
- If issues persist, manually update:
  ```sql
  UPDATE users SET email_verified = true WHERE email_confirmation_token IS NULL;
  ```

## 📝 Environment Variables Summary

Add these to your `.env` file:

```bash
# Required for email verification
RESEND_API_KEY="re_your_api_key_here"
RESEND_FROM_EMAIL="noreply@yourdomain.com"

# Optional: Override base URL for email links
# NEXT_PUBLIC_APP_URL="http://localhost:3001"
```

## ✅ Verification Checklist

- [ ] Database migration run successfully
- [ ] `email_verified` column exists in `users` table
- [ ] `email_confirmation_token` column exists
- [ ] `email_confirmation_token_expiry` column exists
- [ ] `RESEND_API_KEY` set in `.env`
- [ ] `RESEND_FROM_EMAIL` set in `.env`
- [ ] Test registration sends email
- [ ] Email confirmation link works
- [ ] Login works after verification
- [ ] Resend confirmation email works

## 🎉 You're Done!

Once all steps are complete, email verification is fully functional. New users will need to verify their email before they can log in.







