Automated Action aae9527254 Set up user authentication flow with FastAPI and SQLite
- Created user model with SQLAlchemy ORM
- Implemented authentication with JWT tokens (access and refresh tokens)
- Added password hashing with bcrypt
- Created API endpoints for registration, login, and user management
- Set up Alembic for database migrations
- Added health check endpoint
- Created role-based access control (standard users and superusers)
- Added comprehensive documentation
2025-06-10 15:58:57 +00:00

51 lines
1.4 KiB
Python

from typing import Any
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.crud.user import create_user, get_user_by_email, get_user_by_username
from app.db.session import get_db
from app.schemas.user import User, UserCreate
router = APIRouter()
@router.post("/", response_model=User)
def register_user(
*,
db: Session = Depends(get_db),
user_in: UserCreate,
) -> Any:
"""
Register a new user.
Args:
db: Database session
user_in: User data for registration
Returns:
Registered user
"""
# Check if user with this email already exists
user = get_user_by_email(db, email=user_in.email)
if user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="A user with this email already exists",
)
# Check if user with this username already exists
user = get_user_by_username(db, username=user_in.username)
if user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="A user with this username already exists",
)
# Create user (non-superuser by default)
user_in_data = user_in.dict()
user_in_data["is_superuser"] = False
user = create_user(db, obj_in=UserCreate(**user_in_data))
return user