Skip to content

Commit a3f24b5

Browse files
Allow custom response in authentication logout (#914)
1 parent 2004c0d commit a3f24b5

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

docs/authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The class `AuthenticationBackend` has three methods you need to override:
1010

1111
* `authenticate`: Will be called for validating each incoming request.
1212
* `login`: Will be called only in the login page to validate username/password.
13-
* `logout`: Will be called only for the logout, usually clearin the session.
13+
* `logout`: Will be called only for the logout, usually clearing the session.
1414

1515
```python
1616
from sqladmin import Admin

sqladmin/application.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,11 @@ async def login(self, request: Request) -> Response:
638638
async def logout(self, request: Request) -> Response:
639639
assert self.authentication_backend is not None
640640

641-
await self.authentication_backend.logout(request)
641+
response = await self.authentication_backend.logout(request)
642+
643+
if isinstance(response, Response):
644+
return response
645+
642646
return RedirectResponse(request.url_for("admin:index"), status_code=302)
643647

644648
async def ajax_lookup(self, request: Request) -> Response:

sqladmin/authentication.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ async def login(self, request: Request) -> bool:
2929
"""
3030
raise NotImplementedError()
3131

32-
async def logout(self, request: Request) -> bool:
32+
async def logout(self, request: Request) -> Response | bool:
3333
"""Implement logout logic here.
3434
This will usually clear the session with `request.session.clear()`.
35+
36+
If a `Response` or `RedirectResponse` is returned,
37+
that response is returned to the user,
38+
otherwise the user will be redirected to the index page.
3539
"""
3640
raise NotImplementedError()
3741

@@ -40,7 +44,7 @@ async def authenticate(self, request: Request) -> Response | bool:
4044
This method will be called for each incoming request
4145
to validate the authentication.
4246
43-
If a 'Response' or `RedirectResponse` is returned,
47+
If a `Response` or `RedirectResponse` is returned,
4448
that response is returned to the user,
4549
otherwise a True/False is expected.
4650
"""

0 commit comments

Comments
 (0)