<filename>src/api/api_v1/endpoints/auth.py
from datetime import timedelta

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session

from src import crud, models, schemas
from src.api import deps
from src.core import security
from src.core.config import settings

router = APIRouter()


@router.post("/signup")
def create_user(
    user: schemas.UserCreate,
    db: Session = Depends(deps.get_db)
):
    db_user = crud.get_user_by_username(db, username=user.username)

    if db_user:
        raise HTTPException(
            status_code=400,
            detail="Username already registered"
        )

    crud.create_user(db=db, user=user)


@router.post("/login", response_model=schemas.Token)
def login(
    form_data: OAuth2PasswordRequestForm = Depends(),
    db: Session = Depends(deps.get_db)
):

    user = crud.user.authenticate_user(
        db,
        username=form_data.username,
        password=<PASSWORD>
    )

    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )

    access_token_expires = timedelta(
        minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
    )
    access_token = security.create_access_token(
        data={"sub": user.username},
        expires_delta=access_token_expires
    )

    return {"access_token": access_token, "token_type": "bearer"}


@router.post("/test-token", response_model=schemas.User)
def test_token(
    current_user: models.User = Depends(deps.get_current_user)
):
    return current_user

<filename>src/api/api_v1/endpoints/auth.py
from datetime import timedelta

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session

from src import crud, models, schemas
from src.api import deps
from src.core import security
from src.core.config import settings

router = APIRouter()


@router.post("/signup")
def create_user(
    user: schemas.UserCreate,
    db: Session = Depends(deps.get_db)
):
    db_user = crud.get_user_by_username(db, username=user.username)

    if db_user:
        raise HTTPException(
            status_code=400,
            detail="Username already registered"
        )

    crud.create_user(db=db, user=user)


@router.post("/login", response_model=schemas.Token)
def login(
    form_data: OAuth2PasswordRequestForm = Depends(),
    db: Session = Depends(deps.get_db)
):

    user = crud.user.authenticate_user(
        db,
        username=form_data.username,
        password=<PASSWORD>
    )

    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )

    access_token_expires = timedelta(
        minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
    )
    access_token = security.create_access_token(
        data={"sub": user.username},
        expires_delta=access_token_expires
    )

    return {"access_token": access_token, "token_type": "bearer"}


@router.post("/test-token", response_model=schemas.User)
def test_token(
    current_user: models.User = Depends(deps.get_current_user)
):
    return current_user
