forked from benavlabs/FastAPI-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogout.py
More file actions
31 lines (23 loc) · 1002 Bytes
/
logout.py
File metadata and controls
31 lines (23 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from typing import Optional
import jwt
from fastapi import APIRouter, Cookie, Depends, Response
from sqlalchemy.ext.asyncio import AsyncSession
from ...core.db.database import async_get_db
from ...core.exceptions.http_exceptions import UnauthorizedException
from ...core.security import blacklist_tokens, oauth2_scheme
router = APIRouter(tags=["login"])
@router.post("/logout")
async def logout(
response: Response,
access_token: str = Depends(oauth2_scheme),
refresh_token: Optional[str] = Cookie(None, alias="refresh_token"),
db: AsyncSession = Depends(async_get_db),
) -> dict[str, str]:
try:
if not refresh_token:
raise UnauthorizedException("Refresh token not found")
await blacklist_tokens(access_token=access_token, refresh_token=refresh_token, db=db)
response.delete_cookie(key="refresh_token")
return {"message": "Logged out successfully"}
except jwt.PyJWTError:
raise UnauthorizedException("Invalid token.")