This project now includes Super Simple API Route Protection using a shared secret between frontend and backend.
Add these variables to your .env.local file:
# API Protection - Required for both server and client
# IMPORTANT: Use the same value for both variables
API_SECRET=your-super-secret-api-key-change-this-in-production
NEXT_PUBLIC_API_SECRET=your-super-secret-api-key-change-this-in-production
# Admin Authentication
ADMIN_PASSWORD=your-admin-password-here
# External API Keys (existing)
NEXT_PUBLIC_RTIRL_PULL_KEY=your-rtirl-pull-key
NEXT_PUBLIC_OPENWEATHER_KEY=your-openweather-api-key
NEXT_PUBLIC_LOCATIONIQ_KEY=your-locationiq-api-key
NEXT_PUBLIC_TIMEZONEDB_KEY=your-timezonedb-api-keyAll API routes are now protected with shared secret authentication:
- ✅
/api/get-settings- Protected - ✅
/api/save-settings- Protected - ✅
/api/get-location- Protected - ✅
/api/save-location- Protected - ✅
/api/get-weather- Protected - ✅
/api/save-weather- Protected - ✅
/api/get-timezone- Protected - ✅
/api/save-timezone- Protected - ✅
/api/settings-stream- Protected (SSE) - ❌
/api/admin-login- Uses separate password auth
For regular API calls (GET/POST):
- Header:
X-API-Secret: your-secret-here - Alternative:
Authorization: Bearer your-secret-here
For Server-Sent Events (SSE):
- URL parameter:
/api/settings-stream?secret=your-secret-here - (Required because EventSource doesn't support custom headers)
The frontend automatically includes authentication headers:
import { authenticatedFetch, createAuthenticatedEventSource } from '@/lib/client-auth';
// Regular API calls
const response = await authenticatedFetch('/api/save-settings', {
method: 'POST',
body: JSON.stringify(settings)
});
// Server-Sent Events
const eventSource = createAuthenticatedEventSource('/api/settings-stream');-
Generate a strong secret:
# Example: Generate a random 32-character secret openssl rand -hex 32 -
Set environment variables on your hosting platform:
- Vercel: Project Settings → Environment Variables
- Netlify: Site Settings → Environment Variables
- Railway/Heroku: Config vars
-
Important: Use the same secret value for both
API_SECRETandNEXT_PUBLIC_API_SECRET
- ✅ Prevents unauthorized access to your overlay data
- ✅ Protects against API abuse and data scraping
- ✅ Simple to implement and maintain
- ✅ Works with both regular requests and real-time streams
- ✅ Backward compatible with existing admin authentication
401 Unauthorized errors:
- Check that both
API_SECRETandNEXT_PUBLIC_API_SECREThave the same value - Restart your development server after adding environment variables
- Verify the secret is not empty or using the fallback value
SSE connection failures:
- The settings stream uses URL parameters for auth due to EventSource limitations
- Check browser network tab for the actual request URL with secret parameter