# Facebook OAuth Setup Guide

## The Problem
Facebook doesn't allow `.test` domains for OAuth redirects. You're getting this error:
> "The domain of this URL isn't included in the app's domains"

## Solution for Local Development

### Step 1: Use localhost instead of .test domain

1. **Start your Laravel app on localhost**:
   ```bash
   php artisan serve --host=localhost --port=8000
   ```

2. **Access your app at**: `http://localhost:8000` instead of `https://neosolvix.test`

### Step 2: Configure Facebook App

1. **Go to** [Facebook Developers](https://developers.facebook.com/)

2. **Create or edit your app**

3. **In App Settings > Basic**:
   - **App Domains**: `localhost` (just the word "localhost", no protocol or port)
   - **Site URL**: `http://localhost:8000`

4. **In Facebook Login > Settings**:
   - **Valid OAuth Redirect URIs**: `http://localhost:8000/social/oauth/facebook/callback`

5. **Add Facebook Login Product**:
   - Go to Products > Add Product > Facebook Login
   - This gives you access to basic login permissions

6. **For Page Management (Optional)**:
   - Go to Products > Add Product > Facebook Pages API
   - This enables `pages_manage_posts`, `pages_read_engagement`, `pages_show_list`

7. **Important Notes**:
   - DO NOT use `https://` for localhost
   - DO NOT include port in App Domains
   - DO include port in Site URL and Redirect URIs

### Step 3: Update Your .env File

```env
FACEBOOK_CLIENT_ID=your_facebook_app_id
FACEBOOK_CLIENT_SECRET=your_facebook_app_secret

# No need for FACEBOOK_REDIRECT_URI - it's handled automatically
```

### Step 4: Test the Integration

1. Navigate to: `http://localhost:8000/websites/24/marketing/social`
2. Click "Connect Account" > Facebook
3. You should be redirected to Facebook for authorization

## Alternative Solutions

### Option A: Use ngrok (if localhost doesn't work)

1. **Install ngrok**: https://ngrok.com/
2. **Start your Laravel app**: `php artisan serve`
3. **In another terminal**: `ngrok http 8000`
4. **Use the ngrok URL** (e.g., `https://abc123.ngrok.io`) in Facebook settings

### Option B: Development Mode

Facebook apps in "Development Mode" are more permissive:

1. **In Facebook App Dashboard**:
   - Go to **App Review > Permissions and Features**
   - Ensure your app is in **Development Mode**
   - Add test users if needed

2. **Test Users**:
   - Go to **Roles > Test Users**
   - Create test users for testing OAuth flow

## Facebook Permissions Guide

### ✅ Available Without App Review (Development Mode)
- `public_profile` - Basic profile info
- `email` - User's email address
- `pages_show_list` - List user's pages
- `pages_read_engagement` - Read page insights
- `pages_manage_posts` - Create and manage page posts

### ⚠️ Requires App Review (Production Mode)
- `publish_video` - Upload videos to pages
- `pages_read_user_content` - Read user-generated content on pages
- `pages_manage_metadata` - Update page information
- `pages_manage_ads` - Manage page advertisements

### 🚫 Deprecated/Restricted
- `publish_actions` - Deprecated, use `pages_manage_posts` instead
- `manage_pages` - Deprecated, use specific page permissions

### Current Integration Uses:
Our integration requests these permissions:
- `pages_manage_posts` - To publish posts to Facebook pages
- `pages_read_engagement` - To read likes, comments, shares
- `pages_show_list` - To show available pages for connection

## Production Setup

For production, you'll need:

1. **Real domain** (e.g., `yourdomain.com`)
2. **HTTPS enabled**
3. **App Review** for advanced permissions
4. **Privacy Policy URL** in Facebook app settings

### Production Facebook Settings:
- **App Domains**: `yourdomain.com`
- **Site URL**: `https://yourdomain.com`
- **Valid OAuth Redirect URIs**: `https://yourdomain.com/social/oauth/facebook/callback`

## Troubleshooting

### Error: "App Not Setup: This app is still in development mode"
- Add your Facebook account as a test user
- Or switch app to Live mode (requires app review)

### Error: "URL Blocked: This redirect failed"
- Check that your redirect URI exactly matches what's in Facebook settings
- Ensure no trailing slashes or extra parameters

### Error: "Invalid Scopes"
- Remove any advanced permissions not approved by Facebook
- Start with basic permissions: `public_profile`, `email`
- For pages: add `pages_show_list`, `pages_read_engagement`, `pages_manage_posts`
- **AVOID**: `publish_video` (requires special approval)
- **AVOID**: `pages_read_user_content` (requires app review)

## Test Checklist

- [ ] App uses `localhost:8000` not `.test` domain
- [ ] Facebook App Domains set to `localhost`
- [ ] Facebook Site URL set to `http://localhost:8000`
- [ ] Redirect URI set to `http://localhost:8000/social/oauth/facebook/callback`
- [ ] App in Development Mode
- [ ] User is admin/developer of the Facebook app
- [ ] Laravel app accessible at `http://localhost:8000`

## Need Help?

If you're still having issues:
1. Check Laravel logs: `tail -f storage/logs/laravel.log`
2. Check browser developer console for errors
3. Verify your Facebook app credentials in .env file
4. Try with a fresh Facebook app in development mode