|
| 1 | +# GitHub OAuth Setup Guide for Production |
| 2 | + |
| 3 | +This guide will help you set up GitHub OAuth authentication for your production deployment. |
| 4 | + |
| 5 | +## Quick Verification |
| 6 | + |
| 7 | +Run this command to verify your configuration: |
| 8 | + |
| 9 | +```bash |
| 10 | +node scripts/verify-github-oauth.js |
| 11 | +``` |
| 12 | + |
| 13 | +For production environment check: |
| 14 | + |
| 15 | +```bash |
| 16 | +node scripts/verify-github-oauth.js production |
| 17 | +``` |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Step-by-Step Setup |
| 22 | + |
| 23 | +### 1. Create GitHub OAuth App |
| 24 | + |
| 25 | +#### For Production: |
| 26 | + |
| 27 | +1. Go to https://github.com/settings/developers |
| 28 | +2. Click **"New OAuth App"** |
| 29 | +3. Fill in: |
| 30 | + ``` |
| 31 | + Application name: VetsWhoCode LMS Production |
| 32 | + Homepage URL: https://vetswhocode.vercel.app |
| 33 | + Authorization callback URL: https://vetswhocode.vercel.app/api/auth/callback/github |
| 34 | + ``` |
| 35 | +4. Click **"Register application"** |
| 36 | +5. Copy the **Client ID** |
| 37 | +6. Generate and copy the **Client Secret** (save immediately - you can't view it again!) |
| 38 | + |
| 39 | +#### For Development (if you haven't already): |
| 40 | + |
| 41 | +1. Go to https://github.com/settings/developers |
| 42 | +2. Click **"New OAuth App"** |
| 43 | +3. Fill in: |
| 44 | + ``` |
| 45 | + Application name: VetsWhoCode LMS Development |
| 46 | + Homepage URL: http://localhost:3000 |
| 47 | + Authorization callback URL: http://localhost:3000/api/auth/callback/github |
| 48 | + ``` |
| 49 | +4. Click **"Register application"** |
| 50 | +5. Copy the **Client ID** and **Client Secret** |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +### 2. Environment Variables |
| 55 | + |
| 56 | +#### Local Development (.env.local) |
| 57 | + |
| 58 | +```bash |
| 59 | +# GitHub OAuth |
| 60 | +GITHUB_CLIENT_ID=your_development_client_id |
| 61 | +GITHUB_CLIENT_SECRET=your_development_client_secret |
| 62 | + |
| 63 | +# NextAuth |
| 64 | +NEXTAUTH_SECRET=your_random_secret |
| 65 | +NEXTAUTH_URL=http://localhost:3000 |
| 66 | + |
| 67 | +# Organization (optional - allows org members to sign in) |
| 68 | +GITHUB_ORG=Vets-Who-Code |
| 69 | +``` |
| 70 | + |
| 71 | +#### Production (Vercel Environment Variables) |
| 72 | + |
| 73 | +Add these in Vercel Project Settings → Environment Variables: |
| 74 | + |
| 75 | +```bash |
| 76 | +# GitHub OAuth (from production OAuth app) |
| 77 | +GITHUB_CLIENT_ID=your_production_client_id |
| 78 | +GITHUB_CLIENT_SECRET=your_production_client_secret |
| 79 | + |
| 80 | +# NextAuth |
| 81 | +NEXTAUTH_SECRET=your_production_secret |
| 82 | +NEXTAUTH_URL=https://vetswhocode.vercel.app |
| 83 | + |
| 84 | +# Organization |
| 85 | +GITHUB_ORG=Vets-Who-Code |
| 86 | +``` |
| 87 | + |
| 88 | +**Generate NEXTAUTH_SECRET:** |
| 89 | +```bash |
| 90 | +openssl rand -base64 32 |
| 91 | +``` |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +### 3. Who Can Sign In? |
| 96 | + |
| 97 | +Based on `src/pages/api/auth/options.ts`, the authentication logic is: |
| 98 | + |
| 99 | +#### Development Mode: |
| 100 | +- ✅ **Anyone** with a GitHub account can sign in |
| 101 | + |
| 102 | +#### Production Mode: |
| 103 | +- ✅ User `jeromehardaway` (hardcoded admin) |
| 104 | +- ✅ Members of the `Vets-Who-Code` GitHub organization |
| 105 | +- ❌ Other GitHub users are blocked |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### 4. Testing Authentication |
| 110 | + |
| 111 | +#### Test Locally: |
| 112 | + |
| 113 | +1. Make sure `.env.local` has the correct development OAuth credentials |
| 114 | +2. Run the verification script: |
| 115 | + ```bash |
| 116 | + node scripts/verify-github-oauth.js |
| 117 | + ``` |
| 118 | +3. Start the dev server: |
| 119 | + ```bash |
| 120 | + npm run dev |
| 121 | + ``` |
| 122 | +4. Navigate to http://localhost:3000/login |
| 123 | +5. Click "Sign in with GitHub" |
| 124 | +6. Authorize the app |
| 125 | +7. You should be redirected back and signed in |
| 126 | + |
| 127 | +#### Test Production: |
| 128 | + |
| 129 | +1. Deploy to Vercel with production environment variables |
| 130 | +2. Navigate to https://your-app.vercel.app/login |
| 131 | +3. Sign in with GitHub |
| 132 | +4. If you're `jeromehardaway` or a member of `Vets-Who-Code` org, you should be able to sign in |
| 133 | +5. Others will be blocked |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +### 5. Troubleshooting |
| 138 | + |
| 139 | +#### "Redirect URI Mismatch" Error |
| 140 | + |
| 141 | +**Problem:** The callback URL doesn't match what's configured in GitHub OAuth app. |
| 142 | + |
| 143 | +**Solution:** |
| 144 | +- Check your GitHub OAuth app settings at https://github.com/settings/developers |
| 145 | +- Ensure the callback URL exactly matches: `https://your-domain.com/api/auth/callback/github` |
| 146 | +- No trailing slashes! |
| 147 | +- HTTPS for production, HTTP for localhost |
| 148 | + |
| 149 | +#### "Access Denied" in Production |
| 150 | + |
| 151 | +**Problem:** User is not authorized to sign in. |
| 152 | + |
| 153 | +**Solution:** |
| 154 | +- Verify the user is a member of the `Vets-Who-Code` GitHub organization |
| 155 | +- Check organization membership: |
| 156 | + ```bash |
| 157 | + # As organization owner/admin, check: |
| 158 | + # https://github.com/orgs/Vets-Who-Code/people |
| 159 | + ``` |
| 160 | +- Or user must be `jeromehardaway` |
| 161 | + |
| 162 | +#### Environment Variables Not Working |
| 163 | + |
| 164 | +**Problem:** Environment variables seem to not be loaded. |
| 165 | + |
| 166 | +**Solution:** |
| 167 | +- For Vercel: Check Project Settings → Environment Variables |
| 168 | +- Make sure you selected the correct environment (Production, Preview, Development) |
| 169 | +- Redeploy after adding environment variables |
| 170 | +- For local: Make sure `.env.local` exists and has correct format |
| 171 | + |
| 172 | +#### "Failed to fetch" or Timeout Errors |
| 173 | + |
| 174 | +**Problem:** GitHub API is not responding or rate limited. |
| 175 | + |
| 176 | +**Solution:** |
| 177 | +- Check if GitHub is down: https://www.githubstatus.com/ |
| 178 | +- The app has a 5-second timeout for GitHub API calls |
| 179 | +- Rate limiting: Make sure you're not making too many requests |
| 180 | +- Check your firewall/network isn't blocking GitHub API |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +### 6. Security Best Practices |
| 185 | + |
| 186 | +1. **Never commit secrets to git** |
| 187 | + - Use `.env.local` for local development |
| 188 | + - Add `.env.local` to `.gitignore` (already done) |
| 189 | + - Use Vercel environment variables for production |
| 190 | + |
| 191 | +2. **Use strong secrets** |
| 192 | + - Generate NEXTAUTH_SECRET with `openssl rand -base64 32` |
| 193 | + - Never use default values like "your-secret-key-here" |
| 194 | + |
| 195 | +3. **Separate OAuth apps for dev/prod** |
| 196 | + - Create separate GitHub OAuth apps for development and production |
| 197 | + - Use different client IDs and secrets |
| 198 | + - This prevents dev testing from affecting production |
| 199 | + |
| 200 | +4. **Organization access control** |
| 201 | + - Keep `GITHUB_ORG=Vets-Who-Code` to restrict production access |
| 202 | + - Regularly review organization members |
| 203 | + - Remove inactive users from the organization |
| 204 | + |
| 205 | +5. **Monitor failed sign-in attempts** |
| 206 | + - Check Vercel logs for authentication errors |
| 207 | + - GitHub will send you emails about new authorizations |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +### 7. Verification Checklist |
| 212 | + |
| 213 | +Use this checklist before deploying to production: |
| 214 | + |
| 215 | +- [ ] Created production GitHub OAuth App |
| 216 | +- [ ] Callback URL matches exactly: `https://your-domain.com/api/auth/callback/github` |
| 217 | +- [ ] Added all required environment variables to Vercel |
| 218 | +- [ ] Generated strong NEXTAUTH_SECRET (32+ characters) |
| 219 | +- [ ] NEXTAUTH_URL points to production domain (not localhost) |
| 220 | +- [ ] GITHUB_ORG is set to `Vets-Who-Code` |
| 221 | +- [ ] Ran verification script: `node scripts/verify-github-oauth.js production` |
| 222 | +- [ ] Tested sign-in locally |
| 223 | +- [ ] Deployed to Vercel |
| 224 | +- [ ] Tested sign-in in production |
| 225 | +- [ ] Verified organization members can sign in |
| 226 | +- [ ] Verified non-members are blocked |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +### 8. Quick Reference |
| 231 | + |
| 232 | +**GitHub OAuth App Settings:** |
| 233 | +- https://github.com/settings/developers |
| 234 | + |
| 235 | +**Vercel Environment Variables:** |
| 236 | +- https://vercel.com/dashboard → Your Project → Settings → Environment Variables |
| 237 | + |
| 238 | +**Check Organization Membership:** |
| 239 | +- https://github.com/orgs/Vets-Who-Code/people |
| 240 | + |
| 241 | +**Verification Script:** |
| 242 | +```bash |
| 243 | +node scripts/verify-github-oauth.js [environment] |
| 244 | +``` |
| 245 | + |
| 246 | +--- |
| 247 | + |
| 248 | +## Need Help? |
| 249 | + |
| 250 | +If you encounter issues: |
| 251 | + |
| 252 | +1. Run the verification script and fix any errors |
| 253 | +2. Check the troubleshooting section above |
| 254 | +3. Review Vercel deployment logs |
| 255 | +4. Check GitHub OAuth app settings |
| 256 | +5. Verify environment variables are set correctly in Vercel |
0 commit comments