diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-04-03 12:36:34 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-04-03 12:36:34 +0200 |
commit | 0d987f5c97cc8c0e205193ef8c67745ac981d5bf (patch) | |
tree | 8cee10db15c6b36abee89663fe1c7159a6b4d658 /pkg/utils.go | |
parent | a9b84f3f3b1d92335188d43048587e32e0921079 (diff) |
Fix login and register
Diffstat (limited to 'pkg/utils.go')
-rw-r--r-- | pkg/utils.go | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/pkg/utils.go b/pkg/utils.go index f5a443e..9246854 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -1,6 +1,11 @@ package utils -import "golang.org/x/crypto/bcrypt" +import ( + "encoding/json" + "net/http" + + "golang.org/x/crypto/bcrypt" +) func HashPassword(password string) (string, error) { bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) @@ -11,3 +16,20 @@ func CheckPasswordHash(password, hash string) bool { err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) return err == nil } + +// Set a JSON response with status code 400 +func JsonError(w *http.ResponseWriter, error string) { + payloadMap := map[string]string{"error": error} + + (*w).Header().Set("Content-Type", "application/json") + (*w).WriteHeader(http.StatusBadRequest) + + payload, err := json.Marshal(payloadMap) + + if err != nil { + (*w).WriteHeader(http.StatusBadGateway) + (*w).Write([]byte(err.Error())) + } else { + (*w).Write(payload) + } +} |