A FastAPI-based authentication system with Firebase integration, providing user registration, login, and token-based authentication.
- π Firebase Authentication integration
- π User registration and login
- π JWT token-based authentication
- π Token refresh functionality
- π‘οΈ Role-based access control
- π Auto-generated API documentation
- π CORS support
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI application
β βββ auth/
β βββ __init__.py
β βββ models.py # Pydantic models
β βββ firebase_auth.py # Firebase authentication service
β βββ dependencies.py # Authentication dependencies
β βββ routes.py # API routes
βββ run.py # Application entry point
βββ requirements.txt # Python dependencies
βββ env.example # Environment variables template
βββ README.md # This file
pip install -r requirements.txt
- Create a Firebase project at Firebase Console
- Enable Authentication in your Firebase project
- Create a service account:
- Go to Project Settings > Service Accounts
- Click "Generate new private key"
- Download the JSON file
Copy env.example
to .env
and configure the variables:
cp env.example .env
Required environment variables:
# Firebase Configuration (choose one option)
# Option 1: Firebase credentials as JSON string
FIREBASE_CREDENTIALS={"type":"service_account","project_id":"your-project-id",...}
# Option 2: Path to Firebase service account JSON file
FIREBASE_SERVICE_ACCOUNT_PATH=./firebase-service-account.json
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
# Application Configuration
ENVIRONMENT=development
DEBUG=true
LOG_LEVEL=info
# Server Configuration
HOST=0.0.0.0
PORT=8000
# CORS Configuration
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:8080
python run.py
The API will be available at:
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Method | Endpoint | Description |
---|---|---|
POST | /auth/signup |
Register a new user |
POST | /auth/login |
Login user |
POST | /auth/refresh |
Refresh access token |
GET | /auth/me |
Get current user info |
POST | /auth/logout |
Logout user |
GET | /auth/verify |
Verify token validity |
POST /auth/signup
Content-Type: application/json
{
"email": "[email protected]",
"password": "securepassword123",
"first_name": "John",
"last_name": "Doe"
}
Response:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"token_type": "bearer",
"user": {
"id": "firebase-user-id",
"email": "[email protected]",
"first_name": "John",
"last_name": "Doe",
"is_active": true,
"created_at": "2024-01-01T00:00:00Z"
}
}
POST /auth/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "securepassword123"
}
POST /auth/refresh
Content-Type: application/json
{
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}
GET /auth/me
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...
The authentication system provides several dependency functions for protecting routes:
from app.auth.dependencies import get_current_user
@app.get("/protected")
async def protected_route(current_user = Depends(get_current_user)):
return {"message": f"Hello {current_user['email']}"}
from app.auth.dependencies import get_current_active_user
@app.get("/active-only")
async def active_user_route(current_user = Depends(get_current_active_user)):
return {"message": "Active user only"}
from app.auth.dependencies import require_admin, require_user
@app.get("/admin-only")
async def admin_route(current_user = Depends(require_admin)):
return {"message": "Admin only"}
@app.get("/user-route")
async def user_route(current_user = Depends(require_user)):
return {"message": "User or admin"}
The API returns appropriate HTTP status codes and error messages:
400 Bad Request
: Invalid request data401 Unauthorized
: Invalid or missing authentication403 Forbidden
: Insufficient permissions500 Internal Server Error
: Server-side errors
- JWT Secret: Use a strong, unique secret key in production
- Firebase Credentials: Keep service account credentials secure
- CORS: Configure allowed origins properly for production
- Password Policy: Implement strong password requirements
- Rate Limiting: Consider adding rate limiting for auth endpoints
- HTTPS: Always use HTTPS in production
python run.py
The server will run with auto-reload enabled.
You can test the API using the interactive documentation at http://localhost:8000/docs
For development, you can use the default values in env.example
. Make sure to:
- Set up a Firebase project
- Configure the Firebase credentials
- Generate a secure JWT secret
- Set
ENVIRONMENT=production
- Configure proper CORS origins
- Use environment-specific Firebase credentials
- Set up proper logging
- Use a production WSGI server like Gunicorn
- Configure reverse proxy (nginx)
- Set up SSL/TLS certificates
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.