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 { func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.Response {
err := r.ParseForm() username, password, ok := r.BasicAuth()
if err != nil { if !ok {
return response.Error(http.StatusBadRequest, "Failed to parse form") err := r.ParseForm()
} if err != nil {
return response.Error(http.StatusBadRequest, "Failed to parse form")
}
username := r.Form.Get("username") username = r.Form.Get("username")
password := r.Form.Get("password") password = r.Form.Get("password")
}
props := api.NewLoginProps(username, 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 { func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
err := r.ParseForm() var confirmPassword string
if err != nil { username, newPassword, ok := r.BasicAuth()
return response.Error(http.StatusBadRequest, "Failed to parse form") 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") username = r.Form.Get("username")
newPassword := r.Form.Get("newPassword") newPassword = r.Form.Get("newPassword")
confirmPassword := r.Form.Get("confirmPassword") confirmPassword = r.Form.Get("confirmPassword")
}
props := api.NewRegisterProps(username, newPassword, 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) return rsvp.SeeOther(r.URL.Path, props)
} }
_, err = lishwist.Register(username, newPassword) _, err := lishwist.Register(username, newPassword)
if err != nil { if err != nil {
if errors.Is(err, lishwist.ErrorUsernameTaken) { if errors.Is(err, lishwist.ErrorUsernameTaken) {
props.Username.Error = "Username is taken" props.Username.Error = "Username is taken"