Add POST endpoint for /logout
This commit is contained in:
parent
604afcc574
commit
96a0bbe860
@ -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"}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user