Support auto-hashing fields via hooks.

This commit is contained in:
mikestefanello 2025-04-12 16:19:18 -04:00
parent 56f8a619a5
commit 7ff1e3f9d2
6 changed files with 49 additions and 36 deletions

View file

@ -8,6 +8,7 @@ import (
ge "github.com/mikestefanello/pagoda/ent"
"github.com/mikestefanello/pagoda/ent/hook"
"golang.org/x/crypto/bcrypt"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
@ -59,6 +60,14 @@ func (User) Hooks() []ent.Hook {
if v, exists := m.Email(); exists {
m.SetEmail(strings.ToLower(v))
}
if v, exists := m.Password(); exists {
hash, err := bcrypt.GenerateFromPassword([]byte(v), bcrypt.DefaultCost)
if err != nil {
return "", err
}
m.SetPassword(string(hash))
}
return next.Mutate(ctx, m)
})
},