From e0cab8c4176f7f2149a3bc8b59eff6933ecf7e2b Mon Sep 17 00:00:00 2001 From: Automated Action Date: Tue, 24 Jun 2025 18:40:18 +0000 Subject: [PATCH] Add comprehensive error handling and debugging for signup issues - Enhanced CORS configuration with explicit methods and headers - Added detailed logging for registration attempts and errors - Added test endpoints for debugging connectivity issues - Improved error handling in registration with proper rollback - Added startup logging for better debugging --- app/routes/auth.py | 69 ++++++++++++++++++++++++++++++++-------------- main.py | 36 ++++++++++++++++++++---- 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/app/routes/auth.py b/app/routes/auth.py index fdf691c..8c9226c 100644 --- a/app/routes/auth.py +++ b/app/routes/auth.py @@ -1,13 +1,25 @@ from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from pydantic import BaseModel, EmailStr +import logging from app.db.session import get_db from app.models.user import User from app.utils.auth import get_password_hash, verify_password, create_access_token +logger = logging.getLogger(__name__) + router = APIRouter() +@router.get("/test") +async def test_auth(): + logger.info("Auth test endpoint called") + return { + "message": "Auth router is working", + "status": "success" + } + + class UserCreate(BaseModel): email: EmailStr password: str @@ -34,27 +46,44 @@ class UserResponse(BaseModel): @router.post("/register", response_model=UserResponse) async def register(user: UserCreate, db: Session = Depends(get_db)): - db_user = db.query(User).filter(User.email == user.email).first() - if db_user: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Email already registered" + try: + logger.info(f"Registration attempt for email: {user.email}") + + # Check if user already exists + db_user = db.query(User).filter(User.email == user.email).first() + if db_user: + logger.warning(f"Registration failed - email already exists: {user.email}") + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Email already registered" + ) + + # Hash password and create user + hashed_password = get_password_hash(user.password) + db_user = User( + email=user.email, + password_hash=hashed_password + ) + db.add(db_user) + db.commit() + db.refresh(db_user) + + logger.info(f"User registered successfully: {user.email}") + return UserResponse( + id=db_user.id, + email=db_user.email, + created_at=str(db_user.created_at) + ) + + except HTTPException: + raise + except Exception as e: + logger.error(f"Registration error for {user.email}: {str(e)}") + db.rollback() + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Internal server error during registration" ) - - hashed_password = get_password_hash(user.password) - db_user = User( - email=user.email, - password_hash=hashed_password - ) - db.add(db_user) - db.commit() - db.refresh(db_user) - - return UserResponse( - id=db_user.id, - email=db_user.email, - created_at=str(db_user.created_at) - ) @router.post("/login", response_model=Token) diff --git a/main.py b/main.py index 2f8e61d..cf4b52e 100644 --- a/main.py +++ b/main.py @@ -2,19 +2,32 @@ from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager +import logging from app.db.session import engine from app.db.base import Base from app.routes import auth, videos, transcription, translation, voice_cloning, video_processing +# Configure logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + @asynccontextmanager async def lifespan(app: FastAPI): - # Create database tables - storage_dir = Path("/app/storage/db") - storage_dir.mkdir(parents=True, exist_ok=True) - Base.metadata.create_all(bind=engine) - yield + try: + # Create database tables + logger.info("Starting application initialization...") + storage_dir = Path("/app/storage/db") + storage_dir.mkdir(parents=True, exist_ok=True) + logger.info(f"Created storage directory: {storage_dir}") + + Base.metadata.create_all(bind=engine) + logger.info("Database tables created successfully") + yield + except Exception as e: + logger.error(f"Error during application startup: {e}") + raise app = FastAPI( @@ -28,8 +41,9 @@ app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, - allow_methods=["*"], + allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], allow_headers=["*"], + expose_headers=["*"] ) # Include routers @@ -59,6 +73,16 @@ async def health_check(): } +@app.get("/test") +async def test_endpoint(): + logger.info("Test endpoint called") + return { + "message": "API is working correctly", + "timestamp": "2024-01-01T00:00:00Z", + "status": "success" + } + + if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file