Support basic auth for login and registration
This commit is contained in:
parent
9c9e0ebaff
commit
1ad2b3e097
|
|
@ -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 {
|
||||||
|
username, password, ok := r.BasicAuth()
|
||||||
|
if !ok {
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return response.Error(http.StatusBadRequest, "Failed to parse form")
|
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)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
var confirmPassword string
|
||||||
|
username, newPassword, ok := r.BasicAuth()
|
||||||
|
if ok {
|
||||||
|
confirmPassword = newPassword
|
||||||
|
} else {
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return response.Error(http.StatusBadRequest, "Failed to parse form")
|
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"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue