mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-11-22 04:13:46 +00:00
remove unnecessary code, added switch fallthroughs for cleanup. remove status code from response (is in header)
This commit is contained in:
parent
295360fd05
commit
473597682c
3 changed files with 8 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
package util
|
||||
|
||||
// ValidateQuery does nothing.
|
||||
func ValidateQuery(request string) (string, error) {
|
||||
return "", nil
|
||||
}
|
Loading…
Reference in a new issue