Skip to content

Commit 4e6f3fd

Browse files
committed
Fix session logout when cookie not provided
1 parent 3672831 commit 4e6f3fd

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

futuramaapi/routers/services/auth/logout_cookie_session_user.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from futuramaapi.routers.services import BaseSessionService
99

1010

11+
class CookieAuthKeyIsNotDefinedError(Exception): ...
12+
13+
1114
class LogoutCookieSessionUserService(BaseSessionService[RedirectResponse]):
1215
cookie_auth_key: ClassVar[str] = "Authorization"
1316

@@ -23,11 +26,22 @@ def request(self) -> Request:
2326

2427
@property
2528
def _expire_session_statement(self) -> Update:
26-
key: str = self.request.cookies[self.cookie_auth_key]
29+
try:
30+
key: str = self.request.cookies[self.cookie_auth_key]
31+
except KeyError:
32+
raise CookieAuthKeyIsNotDefinedError() from None
33+
2734
return update(AuthSessionModel).where(AuthSessionModel.key == key).values(expired=True)
2835

2936
async def process(self, *args, **kwargs) -> RedirectResponse:
30-
await self.session.execute(self._expire_session_statement)
37+
try:
38+
await self.session.execute(self._expire_session_statement)
39+
except CookieAuthKeyIsNotDefinedError:
40+
return RedirectResponse(
41+
"/auth",
42+
status_code=status.HTTP_302_FOUND,
43+
)
44+
3145
await self.session.commit()
3246

3347
response: RedirectResponse = RedirectResponse(

0 commit comments

Comments
 (0)