Add dynamic admin panel for managing entities (#108)
This commit is contained in:
parent
60009df0bf
commit
1a6874fd82
47 changed files with 2173 additions and 320 deletions
|
|
@ -1,11 +1,15 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
ge "github.com/mikestefanello/pagoda/ent"
|
||||
"github.com/mikestefanello/pagoda/ent/hook"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// PasswordToken holds the schema definition for the PasswordToken entity.
|
||||
|
|
@ -16,9 +20,10 @@ type PasswordToken struct {
|
|||
// Fields of the PasswordToken.
|
||||
func (PasswordToken) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("hash").
|
||||
field.String("token").
|
||||
Sensitive().
|
||||
NotEmpty(),
|
||||
field.Int("user_id"),
|
||||
field.Time("created_at").
|
||||
Default(time.Now),
|
||||
}
|
||||
|
|
@ -28,7 +33,30 @@ func (PasswordToken) Fields() []ent.Field {
|
|||
func (PasswordToken) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("user", User.Type).
|
||||
Field("user_id").
|
||||
Required().
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
// Hooks of the PasswordToken.
|
||||
func (PasswordToken) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.On(
|
||||
func(next ent.Mutator) ent.Mutator {
|
||||
return hook.PasswordTokenFunc(func(ctx context.Context, m *ge.PasswordTokenMutation) (ent.Value, error) {
|
||||
if v, exists := m.Token(); exists {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(v), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m.SetToken(string(hash))
|
||||
}
|
||||
return next.Mutate(ctx, m)
|
||||
})
|
||||
},
|
||||
// Limit the hook only for these operations.
|
||||
ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ package schema
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/mail"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
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"
|
||||
|
|
@ -25,12 +27,18 @@ func (User) Fields() []ent.Field {
|
|||
NotEmpty(),
|
||||
field.String("email").
|
||||
NotEmpty().
|
||||
Unique(),
|
||||
Unique().
|
||||
Validate(func(s string) error {
|
||||
_, err := mail.ParseAddress(s)
|
||||
return err
|
||||
}),
|
||||
field.String("password").
|
||||
Sensitive().
|
||||
NotEmpty(),
|
||||
field.Bool("verified").
|
||||
Default(false),
|
||||
field.Bool("admin").
|
||||
Default(false),
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
Immutable(),
|
||||
|
|
@ -54,6 +62,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)
|
||||
})
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue