From 338fe5d710a4c60de71525810912e6b14665ce27 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 25 Feb 2024 20:09:19 +0000 Subject: [PATCH] Fix htmx rendering the login page in frame on session logout Signed-off-by: Yarden Shoham --- modules/context/base.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/context/base.go b/modules/context/base.go index fa05850a166b..ddd04f476745 100644 --- a/modules/context/base.go +++ b/modules/context/base.go @@ -265,6 +265,14 @@ func (b *Base) Redirect(location string, status ...int) { // So in this case, we should remove the session cookie from the response header removeSessionCookieHeader(b.Resp) } + // in case the request is made by htmx, have it redirect the browser instead of trying to follow the redirect inside htmx + if b.Req.Header.Get("HX-Request") == "true" { + b.Resp.Header().Set("HX-Redirect", location) + // we have to return a non-redirect status code so XMLHTTPRequest will not immediately follow the redirect + // so as to give htmx redirect logic a chance to run + b.Status(http.StatusNoContent) + return + } http.Redirect(b.Resp, b.Req, location, code) }