Automated Action 50b3fc513b Implement FastAPI user authentication service with MongoDB
- 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
2025-06-20 14:19:13 +00:00
2025-06-20 13:03:33 +00:00

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

  1. Install dependencies:
pip install -r requirements.txt
  1. Start MongoDB (make sure MongoDB is running on your system)

  2. Copy environment variables:

cp .env.example .env
  1. Update the .env file with your actual values

Running the Application

uvicorn main:app --reload

The application will be available at:

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
Description
Project: User Authentication Service
Readme 37 KiB
Languages
Python 100%