Skip to content

Commit

Permalink
Make login_required work with Fastapi routes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmvp committed Dec 10, 2024
1 parent 9b08745 commit 0bdafa5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sqladmin/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ def login_required(func: Callable[..., Any]) -> Callable[..., Any]:

@functools.wraps(func)
async def wrapper_decorator(*args: Any, **kwargs: Any) -> Any:
view, request = args[0], args[1]
view = args[0]

if len(args) > 1:
request = args[1]
elif isinstance(kwargs.get("request", None), Request):
request = kwargs["request"]
else:
# It might not be called request
request = next(
filter(lambda kwarg: isinstance(kwarg, Request), kwargs.values())
)

admin = getattr(view, "_admin_ref", view)
auth_backend = getattr(admin, "authentication_backend", None)
if auth_backend is not None:
Expand Down

0 comments on commit 0bdafa5

Please sign in to comment.