diff --git a/endpoints/logout.post.py b/endpoints/logout.post.py index e69de29..a8f5744 100644 --- a/endpoints/logout.post.py +++ b/endpoints/logout.post.py @@ -0,0 +1,31 @@ +# Entity: Auth + +```python +from fastapi import APIRouter, Depends, HTTPException, status +from fastapi.security import OAuth2PasswordBearer +from core.database import get_db +from sqlalchemy.orm import Session +from models.auth import Auth +from schemas.auth import AuthResponse + +router = APIRouter() +oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") + +@router.post("/logout", status_code=200, response_model=AuthResponse) +async def logout( + token: str = Depends(oauth2_scheme), + db: Session = Depends(get_db) +): + """Logout user and invalidate token""" + auth = db.query(Auth).filter(Auth.token == token).first() + if not auth: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid token" + ) + + db.delete(auth) + db.commit() + + return {"message": "Successfully logged out"} +``` \ No newline at end of file