added WithConfig factory to all forms
This commit is contained in:
@@ -7,24 +7,54 @@ import (
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
"github.com/go-ozzo/ozzo-validation/v4/is"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"github.com/pocketbase/pocketbase/mails"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
// UserPasswordResetRequest defines a user password reset request form.
|
||||
// UserPasswordResetRequest specifies a user password reset request form.
|
||||
type UserPasswordResetRequest struct {
|
||||
app core.App
|
||||
resendThreshold float64
|
||||
config UserPasswordResetRequestConfig
|
||||
|
||||
Email string `form:"email" json:"email"`
|
||||
}
|
||||
|
||||
// NewUserPasswordResetRequest creates new user password reset request form.
|
||||
// UserPasswordResetRequestConfig is the [UserPasswordResetRequest]
|
||||
// factory initializer config.
|
||||
//
|
||||
// NB! App is required struct member.
|
||||
type UserPasswordResetRequestConfig struct {
|
||||
App core.App
|
||||
TxDao *daos.Dao
|
||||
ResendThreshold float64 // in seconds
|
||||
}
|
||||
|
||||
// NewUserPasswordResetRequest creates a new [UserPasswordResetRequest]
|
||||
// form with initializer config created from the provided [core.App] instance.
|
||||
//
|
||||
// If you want to submit the form as part of another transaction, use
|
||||
// [NewUserPasswordResetRequestWithConfig] with explicitly set TxDao.
|
||||
func NewUserPasswordResetRequest(app core.App) *UserPasswordResetRequest {
|
||||
return &UserPasswordResetRequest{
|
||||
app: app,
|
||||
resendThreshold: 120, // 2 min
|
||||
return NewUserPasswordResetRequestWithConfig(UserPasswordResetRequestConfig{
|
||||
App: app,
|
||||
ResendThreshold: 120, // 2 min
|
||||
})
|
||||
}
|
||||
|
||||
// NewUserPasswordResetRequestWithConfig creates a new [UserPasswordResetRequest]
|
||||
// form with the provided config or panics on invalid configuration.
|
||||
func NewUserPasswordResetRequestWithConfig(config UserPasswordResetRequestConfig) *UserPasswordResetRequest {
|
||||
form := &UserPasswordResetRequest{config: config}
|
||||
|
||||
if form.config.App == nil {
|
||||
panic("Missing required config.App instance.")
|
||||
}
|
||||
|
||||
if form.config.TxDao == nil {
|
||||
form.config.TxDao = form.config.App.Dao()
|
||||
}
|
||||
|
||||
return form
|
||||
}
|
||||
|
||||
// Validate makes the form validatable by implementing [validation.Validatable] interface.
|
||||
@@ -48,23 +78,23 @@ func (form *UserPasswordResetRequest) Submit() error {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := form.app.Dao().FindUserByEmail(form.Email)
|
||||
user, err := form.config.TxDao.FindUserByEmail(form.Email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
lastResetSentAt := user.LastResetSentAt.Time()
|
||||
if now.Sub(lastResetSentAt).Seconds() < form.resendThreshold {
|
||||
if now.Sub(lastResetSentAt).Seconds() < form.config.ResendThreshold {
|
||||
return errors.New("You've already requested a password reset.")
|
||||
}
|
||||
|
||||
if err := mails.SendUserPasswordReset(form.app, user); err != nil {
|
||||
if err := mails.SendUserPasswordReset(form.config.App, user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// update last sent timestamp
|
||||
user.LastResetSentAt = types.NowDateTime()
|
||||
|
||||
return form.app.Dao().SaveUser(user)
|
||||
return form.config.TxDao.SaveUser(user)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user