31 lines
866 B
Python
31 lines
866 B
Python
# 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"}
|
|
``` |