The application uses token-based authentication to protect API endpoints from abuse.
- Token Generation: A secure random token is generated and stored in environment variables
- Token Validation: Both Next.js API routes and Firebase Cloud Functions validate the token on each request
- Client-Side Token: The token is included in requests from the frontend
In development, the API will work without tokens (for easier testing). However, you can still set tokens if needed.
1. Generate a Secure Token:
# Using OpenSSL
openssl rand -hex 32
# Or using Node.js
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"2. Set Environment Variables:
For Next.js (API Routes):
# .env.local or production environment
API_TOKEN=your-generated-token-here
NEXT_PUBLIC_API_TOKEN=your-generated-token-hereFor Firebase Functions:
# Set as Firebase Function config
firebase functions:config:set api.token="your-generated-token"
# Or use Firebase Functions environment variables (recommended)
firebase functions:secrets:set API_TOKEN
# Then enter your token when prompted3. Update Function Code (if using secrets):
In functions/src/index.ts, update the token retrieval:
const expectedToken = functions.config().api?.token || process.env.API_TOKEN;-
Use Different Tokens: Consider using different tokens for:
- Client-side (
NEXT_PUBLIC_API_TOKEN) - visible in bundle - Server-side (
API_TOKEN) - not exposed to client - Firebase Functions - separate token for additional security
- Client-side (
-
Rotate Tokens Regularly: Change tokens periodically, especially if compromised
-
Never Commit Tokens:
- Add
.env.localto.gitignore(already done) - Never commit tokens to version control
- Use environment variables or secret management services
- Add
-
Use Strong Tokens:
- Minimum 32 characters
- Use cryptographically secure random generators
- Mix of alphanumeric characters
-
Monitor Usage:
- Check logs for unauthorized access attempts
- Set up rate limiting (can be added as additional security layer)
Consider adding rate limiting to prevent abuse:
- Use middleware like
express-rate-limitfor Next.js - Use Firebase Functions quotas and rate limits
- Implement per-IP or per-token rate limiting
The current setup allows all origins (*). For production, restrict CORS:
res.set('Access-Control-Allow-Origin', 'https://your-domain.com');Already implemented: 50MB maximum file size. Adjust in:
app/api/process/route.ts:MAX_FILE_SIZEfunctions/src/index.ts:MAX_FILE_SIZE
- Check that
API_TOKENorNEXT_PUBLIC_API_TOKENis set - Verify the token matches between client and server
- Check environment variables are loaded correctly
- In development, tokens are optional - this error shouldn't appear unless you've set tokens
- Verify environment variables are set in your hosting platform
- For Firebase Functions, ensure config/secrets are deployed
- Check that
NEXT_PUBLIC_API_TOKENis available at build time (not runtime)