working on auth.

mailer, basic setup with html template and a dev treansport
This commit is contained in:
2024-11-15 21:42:15 +05:30
parent b0db98452a
commit 26a00c9f7c
45 changed files with 923 additions and 252 deletions

View File

@@ -12,7 +12,7 @@ import (
pgx "github.com/jackc/pgx/v5/stdlib"
"gitserver.in/patialtech/rano/config"
"gitserver.in/patialtech/rano/db/ent"
"gitserver.in/patialtech/rano/pkg/logger"
"gitserver.in/patialtech/rano/util/logger"
)
type connector struct {
@@ -56,17 +56,20 @@ func Client() *ent.Client {
// A AuditHook is an example for audit-log hook.
func AuditHook(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (v ent.Value, err error) {
start := time.Now()
defer func() {
saveAudit(ctx, m.Type(), m.Op().String(), time.Since(start))
saveAudit(ctx, m.Type(), m.Op().String(), time.Since(start), err)
}()
return next.Mutate(ctx, m)
v, err = next.Mutate(ctx, m)
logger.Info("** %v", v)
return
})
}
func saveAudit(_ context.Context, entT, op string, d time.Duration) {
logger.Info("audit %s %s %s", entT, op, d)
func saveAudit(_ context.Context, entT, op string, d time.Duration, err error) {
logger.Info("audit: %s %s %s %v", entT, op, d, err)
// ml.SetCreatedAt(time.Now())
// if usr := auth.CtxUser(ctx); usr != nil {
// ml.SetByID(usr.ID)

View File

@@ -99,10 +99,10 @@ var (
{Name: "updated_at", Type: field.TypeTime},
{Name: "email", Type: field.TypeString, Unique: true},
{Name: "email_verified", Type: field.TypeBool, Default: false},
{Name: "phone", Type: field.TypeString, Size: 20},
{Name: "phone", Type: field.TypeString, Nullable: true, Size: 20},
{Name: "phone_verified", Type: field.TypeBool, Default: false},
{Name: "pwd_salt", Type: field.TypeString},
{Name: "pwd_hash", Type: field.TypeString},
{Name: "pwd_salt", Type: field.TypeString, Size: 250},
{Name: "pwd_hash", Type: field.TypeString, Size: 250},
{Name: "login_failed_count", Type: field.TypeUint8, Nullable: true, Default: 0},
{Name: "login_attempt_on", Type: field.TypeTime, Nullable: true},
{Name: "login_locked_until", Type: field.TypeTime, Nullable: true},

View File

@@ -2509,9 +2509,22 @@ func (m *UserMutation) OldPhone(ctx context.Context) (v string, err error) {
return oldValue.Phone, nil
}
// ClearPhone clears the value of the "phone" field.
func (m *UserMutation) ClearPhone() {
m.phone = nil
m.clearedFields[user.FieldPhone] = struct{}{}
}
// PhoneCleared returns if the "phone" field was cleared in this mutation.
func (m *UserMutation) PhoneCleared() bool {
_, ok := m.clearedFields[user.FieldPhone]
return ok
}
// ResetPhone resets all changes to the "phone" field.
func (m *UserMutation) ResetPhone() {
m.phone = nil
delete(m.clearedFields, user.FieldPhone)
}
// SetPhoneVerified sets the "phone_verified" field.
@@ -3358,6 +3371,9 @@ func (m *UserMutation) AddField(name string, value ent.Value) error {
// mutation.
func (m *UserMutation) ClearedFields() []string {
var fields []string
if m.FieldCleared(user.FieldPhone) {
fields = append(fields, user.FieldPhone)
}
if m.FieldCleared(user.FieldLoginFailedCount) {
fields = append(fields, user.FieldLoginFailedCount)
}
@@ -3381,6 +3397,9 @@ func (m *UserMutation) FieldCleared(name string) bool {
// error if the field is not defined in the schema.
func (m *UserMutation) ClearField(name string) error {
switch name {
case user.FieldPhone:
m.ClearPhone()
return nil
case user.FieldLoginFailedCount:
m.ClearLoginFailedCount()
return nil

View File

@@ -113,11 +113,39 @@ func init() {
// userDescPwdSalt is the schema descriptor for pwd_salt field.
userDescPwdSalt := userFields[7].Descriptor()
// user.PwdSaltValidator is a validator for the "pwd_salt" field. It is called by the builders before save.
user.PwdSaltValidator = userDescPwdSalt.Validators[0].(func(string) error)
user.PwdSaltValidator = func() func(string) error {
validators := userDescPwdSalt.Validators
fns := [...]func(string) error{
validators[0].(func(string) error),
validators[1].(func(string) error),
}
return func(pwd_salt string) error {
for _, fn := range fns {
if err := fn(pwd_salt); err != nil {
return err
}
}
return nil
}
}()
// userDescPwdHash is the schema descriptor for pwd_hash field.
userDescPwdHash := userFields[8].Descriptor()
// user.PwdHashValidator is a validator for the "pwd_hash" field. It is called by the builders before save.
user.PwdHashValidator = userDescPwdHash.Validators[0].(func(string) error)
user.PwdHashValidator = func() func(string) error {
validators := userDescPwdHash.Validators
fns := [...]func(string) error{
validators[0].(func(string) error),
validators[1].(func(string) error),
}
return func(pwd_hash string) error {
for _, fn := range fns {
if err := fn(pwd_hash); err != nil {
return err
}
}
return nil
}
}()
// userDescLoginFailedCount is the schema descriptor for login_failed_count field.
userDescLoginFailedCount := userFields[9].Descriptor()
// user.DefaultLoginFailedCount holds the default value on creation for the login_failed_count field.

View File

@@ -21,10 +21,10 @@ func (User) Fields() []ent.Field {
fieldUpdated,
field.String("email").Unique().NotEmpty(),
field.Bool("email_verified").Default(false),
field.String("phone").MaxLen(20),
field.String("phone").MaxLen(20).Optional(),
field.Bool("phone_verified").Default(false),
field.String("pwd_salt").NotEmpty(),
field.String("pwd_hash").NotEmpty(),
field.String("pwd_salt").MaxLen(250).NotEmpty(),
field.String("pwd_hash").MaxLen(250).NotEmpty(),
field.Uint8("login_failed_count").Optional().Default(0),
field.Time("login_attempt_on").Optional().Nillable(),
field.Time("login_locked_until").Optional().Nillable(),

View File

@@ -335,6 +335,16 @@ func PhoneHasSuffix(v string) predicate.User {
return predicate.User(sql.FieldHasSuffix(FieldPhone, v))
}
// PhoneIsNil applies the IsNil predicate on the "phone" field.
func PhoneIsNil() predicate.User {
return predicate.User(sql.FieldIsNull(FieldPhone))
}
// PhoneNotNil applies the NotNil predicate on the "phone" field.
func PhoneNotNil() predicate.User {
return predicate.User(sql.FieldNotNull(FieldPhone))
}
// PhoneEqualFold applies the EqualFold predicate on the "phone" field.
func PhoneEqualFold(v string) predicate.User {
return predicate.User(sql.FieldEqualFold(FieldPhone, v))

View File

@@ -76,6 +76,14 @@ func (uc *UserCreate) SetPhone(s string) *UserCreate {
return uc
}
// SetNillablePhone sets the "phone" field if the given value is not nil.
func (uc *UserCreate) SetNillablePhone(s *string) *UserCreate {
if s != nil {
uc.SetPhone(*s)
}
return uc
}
// SetPhoneVerified sets the "phone_verified" field.
func (uc *UserCreate) SetPhoneVerified(b bool) *UserCreate {
uc.mutation.SetPhoneVerified(b)
@@ -292,9 +300,6 @@ func (uc *UserCreate) check() error {
if _, ok := uc.mutation.EmailVerified(); !ok {
return &ValidationError{Name: "email_verified", err: errors.New(`ent: missing required field "User.email_verified"`)}
}
if _, ok := uc.mutation.Phone(); !ok {
return &ValidationError{Name: "phone", err: errors.New(`ent: missing required field "User.phone"`)}
}
if v, ok := uc.mutation.Phone(); ok {
if err := user.PhoneValidator(v); err != nil {
return &ValidationError{Name: "phone", err: fmt.Errorf(`ent: validator failed for field "User.phone": %w`, err)}

View File

@@ -78,6 +78,12 @@ func (uu *UserUpdate) SetNillablePhone(s *string) *UserUpdate {
return uu
}
// ClearPhone clears the value of the "phone" field.
func (uu *UserUpdate) ClearPhone() *UserUpdate {
uu.mutation.ClearPhone()
return uu
}
// SetPhoneVerified sets the "phone_verified" field.
func (uu *UserUpdate) SetPhoneVerified(b bool) *UserUpdate {
uu.mutation.SetPhoneVerified(b)
@@ -425,6 +431,9 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
if value, ok := uu.mutation.Phone(); ok {
_spec.SetField(user.FieldPhone, field.TypeString, value)
}
if uu.mutation.PhoneCleared() {
_spec.ClearField(user.FieldPhone, field.TypeString)
}
if value, ok := uu.mutation.PhoneVerified(); ok {
_spec.SetField(user.FieldPhoneVerified, field.TypeBool, value)
}
@@ -625,6 +634,12 @@ func (uuo *UserUpdateOne) SetNillablePhone(s *string) *UserUpdateOne {
return uuo
}
// ClearPhone clears the value of the "phone" field.
func (uuo *UserUpdateOne) ClearPhone() *UserUpdateOne {
uuo.mutation.ClearPhone()
return uuo
}
// SetPhoneVerified sets the "phone_verified" field.
func (uuo *UserUpdateOne) SetPhoneVerified(b bool) *UserUpdateOne {
uuo.mutation.SetPhoneVerified(b)
@@ -1002,6 +1017,9 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error)
if value, ok := uuo.mutation.Phone(); ok {
_spec.SetField(user.FieldPhone, field.TypeString, value)
}
if uuo.mutation.PhoneCleared() {
_spec.ClearField(user.FieldPhone, field.TypeString)
}
if value, ok := uuo.mutation.PhoneVerified(); ok {
_spec.SetField(user.FieldPhoneVerified, field.TypeBool, value)
}