Automated Action 48f0debd7b Implement user authentication flow with FastAPI
- Set up FastAPI application with SQLite database
- Create User model with email and password fields
- Implement JWT token-based authentication
- Add user registration and login endpoints
- Create protected user profile endpoints
- Configure Alembic for database migrations
- Add password hashing with bcrypt
- Include CORS middleware and health endpoint
- Update README with setup and usage instructions

Environment variables required:
- SECRET_KEY: JWT secret key for token signing
2025-06-27 09:18:50 +00:00

1.9 KiB

User Authentication Service

A FastAPI-based user authentication service with JWT token authentication and SQLite database.

Features

  • User registration and login
  • JWT token-based authentication
  • Password hashing with bcrypt
  • SQLite database with SQLAlchemy ORM
  • Database migrations with Alembic
  • CORS enabled for all origins
  • Health check endpoint
  • Auto-generated API documentation

Environment Variables

Set the following environment variables before running the application:

  • SECRET_KEY: JWT secret key for token signing (required for production)

Installation

  1. Install dependencies:
pip install -r requirements.txt
  1. Run database migrations:
alembic upgrade head
  1. Start the application:
uvicorn main:app --reload

API Endpoints

Public Endpoints

  • GET / - Service information
  • GET /health - Health check
  • POST /api/v1/auth/register - User registration
  • POST /api/v1/auth/login - User login

Protected Endpoints (require Bearer token)

  • GET /api/v1/users/me - Get current user info
  • GET /api/v1/users/profile - Get user profile

API Documentation

Once the application is running, visit:

Usage Example

  1. Register a new user:
curl -X POST "http://localhost:8000/api/v1/auth/register" \
     -H "Content-Type: application/json" \
     -d '{"email": "user@example.com", "password": "password123"}'
  1. Login to get access token:
curl -X POST "http://localhost:8000/api/v1/auth/login" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "username=user@example.com&password=password123"
  1. Access protected endpoint:
curl -X GET "http://localhost:8000/api/v1/users/me" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN"