fix: align session error responses
This commit is contained in:
@@ -109,6 +109,12 @@ func writeJSON(w http.ResponseWriter, v any) {
|
|||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
json.NewEncoder(w).Encode(v)
|
json.NewEncoder(w).Encode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeJSONError(w http.ResponseWriter, status int, message string) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]string{"error": message})
|
||||||
|
}
|
||||||
func qpath(r *http.Request) string {
|
func qpath(r *http.Request) string {
|
||||||
return strings.Trim(strings.ReplaceAll(r.URL.Query().Get("filepath"), "\\", "/"), "/")
|
return strings.Trim(strings.ReplaceAll(r.URL.Query().Get("filepath"), "\\", "/"), "/")
|
||||||
}
|
}
|
||||||
@@ -263,9 +269,13 @@ func (s *Server) session(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, pwd := r.FormValue("user"), r.FormValue("pwd")
|
user, pwd := r.FormValue("user"), r.FormValue("pwd")
|
||||||
|
if user == "" && pwd == "" {
|
||||||
|
writeJSONError(w, http.StatusBadRequest, "invalid parameter")
|
||||||
|
return
|
||||||
|
}
|
||||||
a := s.Auth.Authenticate(user, pwd)
|
a := s.Auth.Authenticate(user, pwd)
|
||||||
if a == nil {
|
if a == nil {
|
||||||
http.Error(w, "invalid account", http.StatusUnauthorized)
|
writeJSONError(w, http.StatusNotFound, "user or password is wrong")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
token := auth.NewToken()
|
token := auth.NewToken()
|
||||||
|
|||||||
Reference in New Issue
Block a user