Automated Action f19a6fea04 Build complete Personal Task Management API with FastAPI
- Implemented user authentication with JWT tokens
- Created comprehensive task management with CRUD operations
- Added category system for task organization
- Set up SQLite database with SQLAlchemy ORM
- Configured Alembic for database migrations
- Added API documentation with OpenAPI/Swagger
- Implemented proper authorization and user scoping
- Created health check and root endpoints
- Updated README with complete documentation
2025-06-21 16:16:40 +00:00

60 lines
1.9 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import HTTPBearer
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.core.security import verify_password, get_password_hash, create_access_token
from app.core.deps import get_current_active_user
from app.models.user import User as UserModel
from app.schemas.user import User, UserCreate, Token
router = APIRouter()
security = HTTPBearer()
@router.post("/register", response_model=User)
def register(
user: UserCreate,
db: Session = Depends(get_db)
):
# Check if user already exists
db_user = db.query(UserModel).filter(UserModel.email == user.email).first()
if db_user:
raise HTTPException(
status_code=400,
detail="Email already registered"
)
# Create new user
hashed_password = get_password_hash(user.password)
db_user = UserModel(
email=user.email,
hashed_password=hashed_password,
full_name=user.full_name,
is_active=user.is_active
)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
@router.post("/login", response_model=Token)
def login(
email: str,
password: str,
db: Session = Depends(get_db)
):
user = db.query(UserModel).filter(UserModel.email == email).first()
if not user or not verify_password(password, user.hashed_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Bearer"},
)
if not user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
access_token = create_access_token(subject=user.id)
return {"access_token": access_token, "token_type": "bearer"}
@router.get("/me", response_model=User)
def read_users_me(current_user: UserModel = Depends(get_current_active_user)):
return current_user