added loggin to api endpoint
All checks were successful
Deploy FluentGerman.ai / deploy (push) Successful in 49s
All checks were successful
Deploy FluentGerman.ai / deploy (push) Successful in 49s
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
"""FluentGerman.ai — Auth router."""
|
"""FluentGerman.ai — Auth router."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
@@ -9,21 +11,32 @@ from app.database import get_db
|
|||||||
from app.models import User
|
from app.models import User
|
||||||
from app.schemas import LoginRequest, Token, UserOut
|
from app.schemas import LoginRequest, Token, UserOut
|
||||||
|
|
||||||
|
logger = logging.getLogger("fluentgerman.auth")
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
||||||
|
|
||||||
|
|
||||||
@router.post("/login", response_model=Token)
|
@router.post("/login", response_model=Token)
|
||||||
async def login(body: LoginRequest, db: AsyncSession = Depends(get_db)):
|
async def login(body: LoginRequest, db: AsyncSession = Depends(get_db)):
|
||||||
|
logger.info("Login attempt: username=%s", body.username)
|
||||||
|
|
||||||
result = await db.execute(select(User).where(User.username == body.username))
|
result = await db.execute(select(User).where(User.username == body.username))
|
||||||
user = result.scalar_one_or_none()
|
user = result.scalar_one_or_none()
|
||||||
|
|
||||||
if not user or not verify_password(body.password, user.hashed_password):
|
if not user:
|
||||||
|
logger.warning("Login failed: user '%s' not found", body.username)
|
||||||
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
|
||||||
|
|
||||||
|
if not verify_password(body.password, user.hashed_password):
|
||||||
|
logger.warning("Login failed: wrong password for '%s'", body.username)
|
||||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
|
||||||
|
|
||||||
if not user.is_active:
|
if not user.is_active:
|
||||||
|
logger.warning("Login failed: account '%s' is disabled", body.username)
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Account disabled")
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Account disabled")
|
||||||
|
|
||||||
token = create_access_token({"sub": str(user.id)})
|
token = create_access_token({"sub": str(user.id)})
|
||||||
|
logger.info("Login success: user=%s admin=%s", user.username, user.is_admin)
|
||||||
return Token(access_token=token)
|
return Token(access_token=token)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user