@@ -65,6 +65,7 @@ func PageMiddleware(level AccessLevel, loginPath string) router.Middleware {
6565
6666 // Unauthenticated — redirect to login
6767 handleUnauthorized (ctx , loginPath )
68+
6869 return nil , nil
6970
7071 default :
@@ -83,11 +84,13 @@ func RequireRole(role string) router.Middleware {
8384 user := UserFromContext (ctx .Context ())
8485 if ! user .Authenticated () {
8586 ctx .ResponseWriter .WriteHeader (http .StatusUnauthorized )
87+
8688 return g .Text ("401 - Unauthorized" ), nil
8789 }
8890
8991 if ! user .HasRole (role ) {
9092 ctx .ResponseWriter .WriteHeader (http .StatusForbidden )
93+
9194 return html .Div (
9295 html .Class ("p-6 text-center" ),
9396 html .H2 (html .Class ("text-xl font-semibold text-destructive mb-2" ), g .Text ("Access Denied" )),
@@ -107,11 +110,13 @@ func RequireScope(scope string) router.Middleware {
107110 user := UserFromContext (ctx .Context ())
108111 if ! user .Authenticated () {
109112 ctx .ResponseWriter .WriteHeader (http .StatusUnauthorized )
113+
110114 return g .Text ("401 - Unauthorized" ), nil
111115 }
112116
113117 if ! user .HasScope (scope ) {
114118 ctx .ResponseWriter .WriteHeader (http .StatusForbidden )
119+
115120 return html .Div (
116121 html .Class ("p-6 text-center" ),
117122 html .H2 (html .Class ("text-xl font-semibold text-destructive mb-2" ), g .Text ("Access Denied" )),
@@ -130,17 +135,19 @@ func RequireScope(scope string) router.Middleware {
130135// For normal requests it sends a standard HTTP 302 redirect.
131136func handleUnauthorized (ctx * router.PageContext , loginPath string ) {
132137 currentPath := ctx .Request .URL .Path
138+
133139 redirectURL := loginPath
134140 if currentPath != "" && currentPath != loginPath {
135141 redirectURL += "?redirect=" + url .QueryEscape (currentPath )
136142 }
137143
138- isHTMX := ctx .Request .Header .Get ("HX -Request" ) != ""
144+ isHTMX := ctx .Request .Header .Get ("Hx -Request" ) != ""
139145
140146 if isHTMX {
141147 // HTMX partial request: use HX-Redirect header for client-side redirect.
142- ctx .ResponseWriter .Header ().Set ("HX -Redirect" , redirectURL )
148+ ctx .ResponseWriter .Header ().Set ("Hx -Redirect" , redirectURL )
143149 ctx .ResponseWriter .WriteHeader (http .StatusUnauthorized )
150+
144151 return
145152 }
146153
0 commit comments