api/login: Use 'See Other' return code
This commit is contained in:
parent
e9b88b9991
commit
4dc2559431
2 changed files with 6 additions and 33 deletions
|
|
@ -75,28 +75,28 @@ func (info *Info) Handler(ctx context.Context, route string, progress io.Writer)
|
||||||
Template: instanceTemplate,
|
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
|
// enforce POST
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
return "", httpx.ErrMethodNotAllowed
|
return "", 0, httpx.ErrMethodNotAllowed
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse the form
|
// parse the form
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
return "", err
|
return "", 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the instance
|
// get the instance
|
||||||
instance, err := info.Instances.WissKI(r.Context(), r.PostFormValue("slug"))
|
instance, err := info.Instances.WissKI(r.Context(), r.PostFormValue("slug"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", httpx.ErrNotFound
|
return "", 0, httpx.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
target, err := instance.Users().Login(r.Context(), nil, r.PostFormValue("user"))
|
target, err := instance.Users().Login(r.Context(), nil, r.PostFormValue("user"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", 0, err
|
||||||
}
|
}
|
||||||
return target.String(), err
|
return target.String(), http.StatusSeeOther, err
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package httpx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RedirectHandler represents a handler that redirects the user to the address returned
|
// 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
|
// do the redirect
|
||||||
http.Redirect(w, r, url, code)
|
http.Redirect(w, r, url, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientSideRedirect func(r *http.Request) (string, error)
|
|
||||||
|
|
||||||
var htmlTemplate = template.Must(template.New("").Parse(`
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<title>Redirecting</title>
|
|
||||||
<meta http-equiv="refresh" content="0; url={{ . }}" />
|
|
||||||
You should be redirected to <a href="{{ . }}">{{ . }}</a>
|
|
||||||
`))
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue