A FastAPI backend for the Sadqa Tracker application with Google OAuth authentication.
- 🔐 Google OAuth2 authentication
- 👤 User management with JWT tokens
- 📊 Sadqa (charity) entry CRUD operations
- 📈 Statistics and analytics
- 🔒 Secure API with rate limiting
- 📖 Auto-generated OpenAPI documentation
- 🗄️ PostgreSQL database with async SQLAlchemy
- 🔄 Database migrations with Alembic
- Python 3.8+
- PostgreSQL database
- Google OAuth2 credentials
- Create and activate virtual environment:
cd backend
python -m venv venv
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Setup environment variables:
cp .env.example .env
# Edit .env with your actual values-
Configure Google OAuth:
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URIs:
http://localhost:8000/auth/google/callback(for development)- Your production callback URL
- Copy Client ID and Secret to
.envfile
-
Setup database:
# Create database (if not exists)
createdb sadqa_tracker_db
# Run migrations
alembic upgrade head- Start the server:
python main.pyThe API will be available at:
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Required |
SECRET_KEY |
JWT secret key | Required |
GOOGLE_OAUTH_CLIENT_ID |
Google OAuth client ID | Required |
GOOGLE_OAUTH_CLIENT_SECRET |
Google OAuth client secret | Required |
GOOGLE_OAUTH_REDIRECT_URI |
OAuth callback URL | http://localhost:8000/auth/google/callback |
ALLOWED_ORIGINS |
CORS allowed origins | http://localhost:3000 |
DEBUG |
Enable debug mode | True |
GET /api/v1/auth/google- Get Google OAuth URLPOST /api/v1/auth/google/callback- Handle OAuth callbackPOST /api/v1/auth/logout- Logout
GET /api/v1/users/me- Get current userPUT /api/v1/users/me- Update current userDELETE /api/v1/users/me- Delete current user
POST /api/v1/sadqa/- Create new sadqa entryGET /api/v1/sadqa/- Get sadqa entries (with filtering)GET /api/v1/sadqa/recent- Get recent entriesGET /api/v1/sadqa/stats- Get statisticsGET /api/v1/sadqa/{id}- Get specific entryPUT /api/v1/sadqa/{id}- Update entryDELETE /api/v1/sadqa/{id}- Delete entry
# Create a new migration
alembic revision --autogenerate -m "Description of changes"
# Apply migrations
alembic upgrade head
# Rollback migration
alembic downgrade -1pytestblack .
isort .- Set
DEBUG=Falsein environment - Use a proper SECRET_KEY
- Configure production database
- Set up proper CORS origins
- Use a production WSGI server like Gunicorn:
pip install gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker- JWT tokens expire after 30 minutes by default
- Rate limiting is applied to authentication endpoints
- CORS is configured for specified origins only
- All API endpoints require authentication except auth endpoints
- Google OAuth provides secure authentication without handling passwords
For issues and questions, please refer to the project documentation or create an issue in the repository.