Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return user to login page on unauthenticated HTMX request #971

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+unauthenticated-htmx-full-redirect.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return user to login page on unauthenticated HTMX request
7 changes: 6 additions & 1 deletion src/argus/htmx/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.template import loader
from django.utils.deprecation import MiddlewareMixin
from django.utils.encoding import force_str
from django_htmx.http import HttpResponseClientRedirect


class LoginRequiredMiddleware:
Expand Down Expand Up @@ -43,7 +44,11 @@ def process_view(self, request, view_func, _view_args, _view_kwargs):
return None

# Redirect unauthenticated users to login page
return redirect_to_login(request.get_full_path(), self.login_url, "next")
response = redirect_to_login(request.get_full_path(), self.login_url, "next")
if getattr(request, "htmx", False):
response = HttpResponseClientRedirect(response.url)

return response


class HtmxMessageMiddleware(MiddlewareMixin):
Expand Down
Loading