Support basic auth for login and registration

This commit is contained in:
Teajey 2025-12-07 21:40:03 +09:00
parent 9c9e0ebaff
commit 1ad2b3e097
Signed by: Teajey
GPG Key ID: 970E790FE834A713
2 changed files with 23 additions and 14 deletions

View File

@ -33,13 +33,16 @@ func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
}
func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
err := r.ParseForm()
if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")
}
username, password, ok := r.BasicAuth()
if !ok {
err := r.ParseForm()
if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")
}
username := r.Form.Get("username")
password := r.Form.Get("password")
username = r.Form.Get("username")
password = r.Form.Get("password")
}
props := api.NewLoginProps(username, password)

View File

@ -29,14 +29,20 @@ func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Re
}
func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
err := r.ParseForm()
if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")
}
var confirmPassword string
username, newPassword, ok := r.BasicAuth()
if ok {
confirmPassword = newPassword
} else {
err := r.ParseForm()
if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")
}
username := r.Form.Get("username")
newPassword := r.Form.Get("newPassword")
confirmPassword := r.Form.Get("confirmPassword")
username = r.Form.Get("username")
newPassword = r.Form.Get("newPassword")
confirmPassword = r.Form.Get("confirmPassword")
}
props := api.NewRegisterProps(username, newPassword, confirmPassword)
@ -49,7 +55,7 @@ func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Resp
return rsvp.SeeOther(r.URL.Path, props)
}
_, err = lishwist.Register(username, newPassword)
_, err := lishwist.Register(username, newPassword)
if err != nil {
if errors.Is(err, lishwist.ErrorUsernameTaken) {
props.Username.Error = "Username is taken"