From 4dc2559431400ca70b088b5308ae1ddaeb7ec416 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Mon, 5 Dec 2022 14:04:37 +0100 Subject: [PATCH] api/login: Use 'See Other' return code --- internal/dis/component/control/info/info.go | 12 ++++----- pkg/httpx/redirect.go | 27 --------------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/internal/dis/component/control/info/info.go b/internal/dis/component/control/info/info.go index fd13537..244c552 100644 --- a/internal/dis/component/control/info/info.go +++ b/internal/dis/component/control/info/info.go @@ -75,28 +75,28 @@ func (info *Info) Handler(ctx context.Context, route string, progress io.Writer) Template: instanceTemplate, }) - router.Path(route + "api/login").Handler(httpx.ClientSideRedirect(func(r *http.Request) (string, error) { + router.Path(route + "api/login").Handler(httpx.RedirectHandler(func(r *http.Request) (string, int, error) { // enforce POST if r.Method != http.MethodPost { - return "", httpx.ErrMethodNotAllowed + return "", 0, httpx.ErrMethodNotAllowed } // parse the form if err := r.ParseForm(); err != nil { - return "", err + return "", 0, err } // get the instance instance, err := info.Instances.WissKI(r.Context(), r.PostFormValue("slug")) if err != nil { - return "", httpx.ErrNotFound + return "", 0, httpx.ErrNotFound } target, err := instance.Users().Login(r.Context(), nil, r.PostFormValue("user")) if err != nil { - return "", err + return "", 0, err } - return target.String(), err + return target.String(), http.StatusSeeOther, err })) return diff --git a/pkg/httpx/redirect.go b/pkg/httpx/redirect.go index 8422eb9..b5090b1 100644 --- a/pkg/httpx/redirect.go +++ b/pkg/httpx/redirect.go @@ -2,7 +2,6 @@ package httpx import ( "net/http" - "text/template" ) // RedirectHandler represents a handler that redirects the user to the address returned @@ -21,29 +20,3 @@ func (rh RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // do the redirect http.Redirect(w, r, url, code) } - -type ClientSideRedirect func(r *http.Request) (string, error) - -var htmlTemplate = template.Must(template.New("").Parse(` - - -Redirecting - -You should be redirected to {{ . }} -`)) - -// ServeHTTP calls r(r) and returns json -func (rh ClientSideRedirect) ServeHTTP(w http.ResponseWriter, r *http.Request) { - // call the function - url, err := rh(r) - - // intercept the errors - if htmlInterceptor.Intercept(w, r, err) { - return - } - - // write out the response as json - w.Header().Set("Content-Type", "text/html") - w.WriteHeader(http.StatusOK) - htmlTemplate.Execute(w, url) -}