
- Set up FastAPI application with MongoDB Motor driver - Implemented user registration, login, and logout with HTTP-only cookies - Added JWT token authentication and password hashing - Created user management endpoints for username updates and password changes - Structured application with proper separation of concerns (models, schemas, services, routes) - Added CORS configuration and health endpoints - Documented API endpoints and environment variables in README
User Authentication Service
A FastAPI application with MongoDB using Motor for user authentication with HTTP-only cookies and CRUD operations.
Features
- User registration and login with email/password
- HTTP-only cookie authentication
- Username updates
- Password changes
- MongoDB with Motor async driver
- JWT token-based session management
Environment Variables
Create a .env
file in the root directory with the following variables:
MONGODB_URL=mongodb://localhost:27017
SECRET_KEY=your-secret-key-here-change-in-production
Installation
- Install dependencies:
pip install -r requirements.txt
-
Start MongoDB (make sure MongoDB is running on your system)
-
Copy environment variables:
cp .env.example .env
- Update the
.env
file with your actual values
Running the Application
uvicorn main:app --reload
The application will be available at:
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- Health check: http://localhost:8000/health
API Endpoints
Authentication
POST /api/auth/register
- Register a new userPOST /api/auth/login
- Login user (sets HTTP-only cookie)POST /api/auth/logout
- Logout user (clears cookie)GET /api/auth/me
- Get current user info
User Management
PUT /api/users/username
- Update usernamePUT /api/users/password
- Change password
Project Structure
.
├── app/
│ ├── db/
│ │ ├── __init__.py
│ │ └── connection.py
│ ├── models/
│ │ ├── __init__.py
│ │ └── user.py
│ ├── routes/
│ │ ├── __init__.py
│ │ ├── auth.py
│ │ └── users.py
│ ├── schemas/
│ │ ├── __init__.py
│ │ └── user.py
│ ├── services/
│ │ ├── __init__.py
│ │ └── user_service.py
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── dependencies.py
│ │ └── security.py
│ └── __init__.py
├── main.py
├── requirements.txt
├── .env.example
└── README.md
Description
Languages
Python
100%