Hashing new password on update

Using postgres' pgcrypt
This commit is contained in:
cdricms
2025-01-17 10:57:59 +01:00
parent fdc3122b68
commit 5405cc50d9

View File

@@ -49,6 +49,7 @@ func HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
for i := 0; i < val.NumField(); i++ { for i := 0; i < val.NumField(); i++ {
field := val.Field(i) field := val.Field(i)
fieldname := typ.Field(i).Name
tag := typ.Field(i).Tag.Get("bun") tag := typ.Field(i).Tag.Get("bun")
if tag == "" { if tag == "" {
@@ -57,7 +58,11 @@ func HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
// Only add fields that are non-nil and non-zero // Only add fields that are non-nil and non-zero
if field.IsValid() && !field.IsNil() && !field.IsZero() { if field.IsValid() && !field.IsNil() && !field.IsZero() {
updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface()) if fieldname == "Password" {
updateQuery.Set(fmt.Sprintf("%s = crypt(?, gen_salt('bf'))", strings.Split(tag, ",")[0]), field.Interface())
} else {
updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface())
}
} }
} }