masterzdran.react.fastapi.project.accelerator
1.0.0
Prefix Reserved
dotnet new install masterzdran.react.fastapi.project.accelerator::1.0.0
Secure Config Management Example Project
This project demonstrates a secure architecture for handling configuration and secrets in a full-stack application. It consists of a FastAPI backend and a React/TypeScript frontend, both designed to follow security best practices for managing sensitive configuration.
Architecture Overview
![Architecture Diagram]
Key Security Features
- Runtime Config Injection: Sensitive information is only delivered at runtime, not baked into builds
- API Key Protection: Backend endpoints are protected by an API key
- CORS Protection: Backend only accepts requests from authorized origins
- Environment Variable Management: All secrets are stored in environment variables, not in code
- Secure Frontend Fetch Pattern: Frontend securely retrieves and uses API keys
Project Structure
.
├── backend/ # FastAPI Python backend
│ ├── app/ # Application code
│ │ ├── __init__.py
│ │ ├── main.py # Main FastAPI application
│ │ ├── middleware.py # CORS and other middleware
│ │ ├── routes/ # API routes
│ │ │ ├── __init__.py
│ │ │ └── config.py # Config endpoints
│ │ └── utils/ # Utility functions
│ │ ├── __init__.py
│ │ └── security.py # API key validation
│ ├── .env.example # Example environment variables
│ └── requirements.txt # Python dependencies
│
├── frontend/ # React/TypeScript frontend
│ ├── index.html # Entry HTML file
│ ├── package.json # npm dependencies and scripts
│ ├── tsconfig.json # TypeScript configuration
│ ├── vite.config.ts # Vite configuration
│ └── src/ # Source code
│ ├── App.tsx # Main application component
│ ├── main.tsx # Entry point
│ ├── api/ # API client code
│ │ └── index.ts # API client implementation
│ └── components/ # React components
│ └── ConfigDisplay.tsx # Display fetched config
│
├── .vscode/ # VSCode configuration
│ └── launch.json # Debug configurations
│
└── README.md # This file
Getting Started
Prerequisites
- Node.js 18+ and npm
- Python 3.10+
- Visual Studio Code (recommended for debugging)
Backend Setup
Navigate to the backend directory:
cd backendCreate a virtual environment and activate it:
python -m venv venv # On Windows venv\Scripts\activate # On Linux/Mac source venv/bin/activateInstall dependencies:
pip install -r requirements.txtCreate a
.envfile based on.env.example:cp .env.example .envModify the
.envfile with your actual values:API_KEY=your_secure_api_key_here ENTRA_CLIENT_ID=your_entra_client_id ENTRA_SCOPE=api://your-app-id/access_as_user FRONTEND_ORIGIN=http://localhost:5173Start the FastAPI server:
uvicorn app.main:app --reload
The backend will be available at http://localhost:8000.
Frontend Setup
Navigate to the frontend directory:
cd frontendInstall dependencies:
npm installStart the development server:
npm run dev
The frontend will be available at http://localhost:5173.
How Runtime Config Injection Works
This project implements a secure pattern for handling configuration:
- The backend reads sensitive information from environment variables at startup
- The frontend makes an initial request to
/config.jsonto obtain the API key - The frontend stores this API key in memory (not localStorage or cookies)
- Subsequent API calls include the API key in the
x-api-keyheader - Protected endpoints verify this API key before returning sensitive configuration
This approach ensures:
- No secrets are embedded in frontend builds
- API keys are delivered at runtime, not build time
- Configuration can be updated without rebuilding the frontend
- The API is protected from unauthorized access
Security Considerations
Why Secrets Should Never Be Stored in Frontend Build
Frontend code is always accessible to users through browser dev tools. Any secret baked into a frontend build (whether in JS bundles, environment variables loaded at build time, or hardcoded values) can be extracted by users. This project demonstrates the correct pattern: retrieving sensitive information at runtime from a protected API.
API Key Injection at Runtime
Instead of hardcoding the API key in the frontend, this application:
- Exposes a public endpoint (
/config.json) that provides the API key - Injects this key into the HTML response dynamically from server environment variables
- The frontend fetches this configuration on startup and uses it for subsequent API calls
This approach allows the API key to be rotated without rebuilding the frontend.
CORS Protection
Cross-Origin Resource Sharing (CORS) protection is implemented to prevent browsers from making requests to the API from unauthorized origins. The backend only accepts requests from the specified frontend origin (e.g., http://localhost:5173 for development), blocking requests from malicious sites.
Debugging with VSCode
This project includes VSCode launch configurations for debugging both the backend and frontend simultaneously:
- Open the project in VSCode
- Navigate to the Debug panel (Ctrl+Shift+D or Cmd+Shift+D)
- Select "Debug Full Stack" from the dropdown
- Press F5 to start debugging
You can set breakpoints in both Python and TypeScript code, and VSCode will stop at both.
To debug individually:
- Select "Debug Backend" to debug only the FastAPI backend
- Select "Debug Frontend" to debug only the React frontend
Troubleshooting
404 Not Found for /config.json
If you encounter a 404 error when the frontend tries to fetch /config.json:
- Check if the backend is running: Ensure the FastAPI server is running on port 8000.
- Verify CORS settings: Make sure the
FRONTEND_ORIGINin your backend.envfile matches your frontend URL (default:http://localhost:5173). - Check Vite proxy settings: The
/config.jsonendpoint should be proxied to the backend in development. - Try direct access: Open
http://localhost:8000/config.jsonin your browser to see if the backend serves it correctly. - Environment variables: Ensure the API_KEY is properly set in your backend
.envfile.
API Key Issues
If you successfully get /config.json but subsequent API calls fail:
- Check browser console: Look for error messages related to the API key.
- Verify headers: Make sure the
x-api-keyheader is being correctly added to requests. - Backend logs: Check the backend terminal for any authorization errors.
Backend Connection Issues
If the frontend can't connect to the backend:
- Port conflicts: Make sure no other services are using port 8000.
- Proxy settings: Verify that Vite is correctly proxying requests to the backend.
- NetworkError: If you see network errors, ensure the backend is running and accessible.
General Debugging Steps
- Clear browser cache: Try hard-refreshing the page (Ctrl+F5).
- Check browser console: Look for error messages or failed network requests.
- Inspect network traffic: Use the Network tab in browser dev tools to see the actual requests and responses.
- Restart services: Sometimes restarting both frontend and backend resolves issues.
- Check package versions: Ensure your Node.js and Python versions are compatible with the project requirements.
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 368 | 5/20/2025 |