This runbook documents the complete replacement of **Firebase Authentication** with **Cloudflare Turnstile** bot and abuse protection across the CRE Innovations Web Studio platform, and provides step-by-step instructions for activating and testing it.
Firebase Authentication has been **completely removed** from the repository to eliminate authentication friction, OAuth consent requirements, and complex Google Cloud service account setups.
In its place, **Cloudflare Turnstile** provides lightweight, privacy-preserving bot protection that verifies human visitors without frustrating puzzles (no crosswalks, traffic lights, or CAPTCHA solving):
-`firebase` and `firebase-admin` dependencies removed from `package.json`
### Files Added / Updated (Cloudflare Turnstile):
-`src/lib/turnstile.ts`: Server-side token validation against `https://challenges.cloudflare.com/turnstile/v0/siteverify`.
-`src/components/turnstile/TurnstileWidget.tsx`: SSR-safe, dynamic client component with real-time status micro-badges.
-`src/app/api/try/[toolId]/route.ts`: Validates `cf-turnstile-response` token before executing GPU neural pipelines.
-`src/components/tools/DynamicToolWorkspace.tsx`: Turnstile widget embedded right above the Process button with automatic token attachment to `FormData`.
-`src/components/sections/TryItWidget.tsx`: Turnstile verification integrated into quick-try card.
-`src/components/ui/Navbar.tsx`: Restored clean header without Firebase login modals.
-`src/app/layout.tsx`: Restored clean root layout without `<AuthProvider>`.
---
## 2. Quick Activation (2 Minutes)
Activating Cloudflare Turnstile is **100% free** and takes under two minutes.
- **Managed** *(Recommended)*: Cloudflare automatically decides when to run an invisible background check vs. showing a single unobtrusive checkbox if risk is elevated.
- **Non-interactive**: Runs a purely automated browser challenge without user interaction.
> If `CLOUDFLARE_TURNSTILE_SECRET_KEY` is omitted or empty during local development, the server gracefully logs a notice and bypasses the check so developers are never blocked while provisioning keys.
Cloudflare maintains official dummy keys that allow testing all pass, fail, and interactive scenarios immediately without creating a Cloudflare account:
| Testing Scenario | Public Site Key (`NEXT_PUBLIC_TURNSTILE_SITE_KEY`) | Server Secret Key (`CLOUDFLARE_TURNSTILE_SECRET_KEY`) | What Happens |
| :--- | :--- | :--- | :--- |
| **Always Passes** | `1x00000000000000000000AA` | `1x0000000000000000000000000000000AA` | Instantly validates without user clicking. Inference succeeds. |
| **Always Blocks / Fails** | `2x00000000000000000000AB` | `2x0000000000000000000000000000000AA` | Challenge fails. Server responds with HTTP `403 BOT_VERIFICATION_FAILED`. |
| **Interactive Checkbox** | `3x00000000000000000000FF` | `1x0000000000000000000000000000000AA` | Shows interactive "Verify you are human" checkbox. |
└─► [PASS] ──► Execute backend GPU model pipeline & stream lossless 4K result
```
---
## 6. Production Deployment Guide
### Vercel / Railway / Render / AWS
Add these two Environment Variables in your hosting dashboard:
-`NEXT_PUBLIC_TURNSTILE_SITE_KEY`: Your production Turnstile Site Key.
-`CLOUDFLARE_TURNSTILE_SECRET_KEY`: Your production Turnstile Secret Key.
### Restarting the Server
```bash
# 1. Clean build
npm run build
# 2. Start production server
npm run start
```
---
## 7. Troubleshooting & FAQs
### Q: Why do I see "Human verification confirmed" even without setting keys?
**A:** In development mode (`NODE_ENV !== 'production'`), if no keys are provided, the widget defaults to Cloudflare's official `Always Passes` test key so developers and testers can use the app offline or before creating a Cloudflare account.
### Q: What if a user has JavaScript disabled or an aggressive ad-blocker?
**A:** Cloudflare Turnstile is served from `challenges.cloudflare.com`, which is whitelisted on standard ad-blockers as a privacy-respecting CAPTCHA alternative. If a network error prevents loading, `/api/try/[toolId]` returns a friendly `retryable: true` message instructing the user to refresh.
1.**Gatekeeper Enforcement**: All client requests target `/api/try/[toolId]`. If the incoming request lacks a valid Cloudflare Turnstile token, Next.js rejects it immediately with `403 Forbidden` (`BOT_VERIFICATION_FAILED`). No network packets are sent to the actual backend.
2.**Zero Client Secret Leakage**: The backend's real URLs (`https://apigp0006.creinnovations.in` or local FastAPI ports) and the backend authentication secret (`API_SECRET_DEFAULT`) exist exclusively in server-side environment variables. They are never exported into client JavaScript bundles.
3.**Backend-Side Defense**: The actual backend server enforces `FirebaseAppCheckMiddleware` / `secretKey` verification on all incoming requests. Any direct request sent to the backend server bypassing the Next.js proxy will lack the secret key and receive `401 Unauthorized` (`The provided secret key is invalid.`).