account/internal/handler/response.go
GnomeZworc f862abe5a4
add owner handle
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-06-14 13:40:51 +02:00

28 lines
694 B
Go

package handler
import (
"encoding/json"
"net/http"
"git.g3e.fr/H6N/account/internal/auth"
)
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(v)
}
func writeError(w http.ResponseWriter, status int, msg string) {
writeJSON(w, status, map[string]string{"error": msg})
}
// requireOwner extrait l'owner du contexte ou écrit 401 et retourne false.
func requireOwner(w http.ResponseWriter, r *http.Request) (int32, bool) {
id, ok := auth.OwnerFromContext(r.Context())
if !ok {
writeError(w, http.StatusUnauthorized, "missing owner")
return 0, false
}
return id, true
}