remove unnecessary code, added switch fallthroughs for cleanup. remove status code from response (is in header)

This commit is contained in:
grumbulon 2023-02-01 19:50:41 -05:00
parent 295360fd05
commit 473597682c
3 changed files with 8 additions and 12 deletions

View file

@ -74,7 +74,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
db.Where("username = ?", username).First(&result)
if result.Username == "" {
APIError(w, r, genericResponseFields{"message": "login failed", "status": http.StatusUnauthorized, "Realm": "authentication"})
APIError(w, r, genericResponseFields{"message": "login failed", "status": http.StatusUnauthorized, "realm": "authentication"})
return
}
@ -82,7 +82,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
err = bcrypt.CompareHashAndPassword([]byte(result.HashedPassword), []byte(password))
if err != nil {
APIError(w, r, genericResponseFields{"message": "login failed", "status": http.StatusUnauthorized, "Realm": "authentication"})
APIError(w, r, genericResponseFields{"message": "login failed", "status": http.StatusUnauthorized, "realm": "authentication"})
return
}

View file

@ -44,16 +44,19 @@ func APIError[T map[string]any](w http.ResponseWriter, r *http.Request, v map[st
logHandler(v)
switch v["realm"] {
case nil:
w.Header().Add("API Error", v["message"].(string))
default:
w.Header().Add("WWW-Authenticate", fmt.Sprintf(`realm="%s"`, v["realm"].(string)))
fallthrough
case nil:
w.Header().Add("API Error", v["message"].(string))
}
w.WriteHeader(v["status"].(int))
// remove unnecessary items from response
delete(v, "error")
delete(v, "status")
render.JSON(w, r, v)
}
@ -66,10 +69,9 @@ func logHandler(v map[string]any) {
switch v["error"] {
default:
logger.Error().Msg(v["error"].(string))
fallthrough
case nil:
logger.Info().Msg(v["message"].(string))
}
}

View file

@ -1,6 +0,0 @@
package util
// ValidateQuery does nothing.
func ValidateQuery(request string) (string, error) {
return "", nil
}