
- 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
93 lines
2.1 KiB
Markdown
93 lines
2.1 KiB
Markdown
# 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:
|
|
|
|
```bash
|
|
MONGODB_URL=mongodb://localhost:27017
|
|
SECRET_KEY=your-secret-key-here-change-in-production
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Start MongoDB (make sure MongoDB is running on your system)
|
|
|
|
3. Copy environment variables:
|
|
```bash
|
|
cp .env.example .env
|
|
```
|
|
|
|
4. Update the `.env` file with your actual values
|
|
|
|
## Running the Application
|
|
|
|
```bash
|
|
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 user
|
|
- `POST /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 username
|
|
- `PUT /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
|
|
```
|