Force user email to lower case. Include action on all forms.

This commit is contained in:
mikestefanello 2021-12-25 21:30:22 -05:00
parent 1ccb0cad1a
commit eafde27809
14 changed files with 100 additions and 50 deletions

View file

@ -1,8 +1,13 @@
package schema
import (
"context"
"strings"
"time"
ge "goweb/ent"
"goweb/ent/hook"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
@ -37,3 +42,20 @@ func (User) Edges() []ent.Edge {
Ref("user"),
}
}
func (User) Hooks() []ent.Hook {
return []ent.Hook{
hook.On(
func(next ent.Mutator) ent.Mutator {
return hook.UserFunc(func(ctx context.Context, m *ge.UserMutation) (ent.Value, error) {
if v, exists := m.Email(); exists {
m.SetEmail(strings.ToLower(v))
}
return next.Mutate(ctx, m)
})
},
// Limit the hook only for these operations.
ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne,
),
}
}