|
| 1 | +# Clerk Auth Deployment Guide |
| 2 | + |
| 3 | +## What Clerk Does |
| 4 | + |
| 5 | +We use Clerk for authentication. It handles: |
| 6 | +- User signup/login (email/password + OAuth) |
| 7 | +- JWT token generation |
| 8 | +- User profile management (username, email) |
| 9 | +- Webhooks to sync user data to our database |
| 10 | + |
| 11 | +## Local Development |
| 12 | + |
| 13 | +Currently using **ngrok** to forward webhooks from Clerk to your local machine: |
| 14 | +- Ngrok URL: `https://xxxx.ngrok.io/webhooks/clerk` |
| 15 | +- Configured in Clerk Dashboard → Webhooks |
| 16 | + |
| 17 | +## Production Deployment Checklist |
| 18 | + |
| 19 | +When you deploy to production: |
| 20 | + |
| 21 | +### 1. Update Webhook URL in Clerk Dashboard |
| 22 | +- Go to [Clerk Dashboard](https://dashboard.clerk.com) |
| 23 | +- Navigate to: **Webhooks** section |
| 24 | +- Delete the ngrok webhook endpoint |
| 25 | +- Add new endpoint: `https://yourdomain.com/webhooks/clerk` |
| 26 | +- Events to subscribe to: |
| 27 | + - `user.created` |
| 28 | + - `user.updated` |
| 29 | + - `user.deleted` |
| 30 | + |
| 31 | +### 2. Update Environment Variables |
| 32 | +Make sure your production backend has: |
| 33 | +``` |
| 34 | +CLERK_PUBLISHABLE_KEY=pk_live_xxxx |
| 35 | +CLERK_SECRET_KEY=sk_live_xxxx |
| 36 | +CLERK_WEBHOOK_SECRET=whsec_xxxx |
| 37 | +``` |
| 38 | +(Use **live** keys, not **test** keys) |
| 39 | + |
| 40 | +### 3. Update Frontend Environment Variables |
| 41 | +Update your production frontend: |
| 42 | +``` |
| 43 | +NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_live_xxxx |
| 44 | +``` |
| 45 | + |
| 46 | +### 4. Update Redirect URLs in Clerk Dashboard |
| 47 | +- Go to: **Paths** section |
| 48 | +- Update these URLs to your production domain: |
| 49 | + - Sign-in URL: `https://yourdomain.com` |
| 50 | + - Sign-up URL: `https://yourdomain.com` |
| 51 | + - After sign-in: `https://yourdomain.com` |
| 52 | + - After sign-up: `https://yourdomain.com` |
| 53 | + |
| 54 | +### 5. Configure Allowed Origins (CORS) |
| 55 | +- Go to: **API Keys** section → **Advanced** |
| 56 | +- Add your production frontend URL to allowed origins |
| 57 | + |
| 58 | +## Testing Production Auth |
| 59 | + |
| 60 | +1. Sign up with a new account on production |
| 61 | +2. Check backend logs - should see: `INFO: Created local user (ID: X) for Clerk user: user_xxx` |
| 62 | +3. Check database - user should exist in `users` table |
| 63 | +4. Try username change in Clerk profile - should sync to DB |
| 64 | +5. Try profanity username (e.g., "fuckthisshit") - should auto-sanitize to `user_xxxxxxxx` |
| 65 | + |
| 66 | +## Troubleshooting |
| 67 | + |
| 68 | +**Webhook not firing:** |
| 69 | +- Check webhook URL is correct in Clerk Dashboard |
| 70 | +- Verify webhook secret matches in `.env` |
| 71 | +- Check backend logs for signature verification errors |
| 72 | + |
| 73 | +**401 Unauthorized:** |
| 74 | +- Verify you're using **live** keys in production (not test keys) |
| 75 | +- Check `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` is set correctly in frontend |
| 76 | +- Make sure backend `CLERK_SECRET_KEY` matches Clerk dashboard |
| 77 | + |
| 78 | +**User not syncing:** |
| 79 | +- Check webhook events are enabled (`user.created`, `user.updated`, `user.deleted`) |
| 80 | +- Verify backend logs show webhook received |
| 81 | +- Check database for user creation errors |
| 82 | + |
| 83 | +## Important Notes |
| 84 | + |
| 85 | +- **Profanity Filter**: Usernames are auto-filtered using `go-away` library. Users see sanitized usernames in app, original in Clerk. |
| 86 | +- **No ngrok in prod**: Remove ngrok completely - use your actual domain for webhooks |
| 87 | +- **Live vs Test keys**: Never mix test and live keys - they don't work together |
0 commit comments