Automated Action f8bb3dd21d Implement Task Management Tool with FastAPI and SQLite
- Set up FastAPI project structure with API versioning
- Create database models for users and tasks
- Implement SQLAlchemy ORM with SQLite database
- Initialize Alembic for database migrations
- Create API endpoints for task management (CRUD)
- Create API endpoints for user management
- Add JWT authentication and authorization
- Add health check endpoint
- Add comprehensive README.md with API documentation
2025-06-02 20:40:57 +00:00

55 lines
1.6 KiB
Python

from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from pydantic import ValidationError
from sqlalchemy.orm import Session
from app.core.config import settings
from app.core.security import ALGORITHM
from app.crud.user import get as get_user
from app.db.session import get_db
from app.models.user import User
from app.schemas.token import TokenPayload
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/users/login")
def get_current_user(
db: Session = Depends(get_db), token: str = Depends(oauth2_scheme)
) -> User:
"""
Get the current user from the token
"""
try:
payload = jwt.decode(
token, settings.SECRET_KEY, algorithms=[ALGORITHM]
)
token_data = TokenPayload(**payload)
except (JWTError, ValidationError):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
user = get_user(db, user_id=int(token_data.sub))
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return user
def get_current_active_user(current_user: User = Depends(get_current_user)) -> User:
"""
Get the current active user
"""
if not current_user.is_active:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Inactive user"
)
return current_user