2022-07-07 05:19:05 +08:00
|
|
|
package forms
|
|
|
|
|
|
|
|
import (
|
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
|
|
|
"github.com/go-ozzo/ozzo-validation/v4/is"
|
|
|
|
"github.com/pocketbase/pocketbase/core"
|
2022-08-07 20:38:21 +08:00
|
|
|
"github.com/pocketbase/pocketbase/daos"
|
2022-07-07 05:19:05 +08:00
|
|
|
"github.com/pocketbase/pocketbase/models"
|
|
|
|
)
|
|
|
|
|
2022-08-07 20:38:21 +08:00
|
|
|
// UserEmailLogin specifies a user email/pass login form.
|
2022-07-07 05:19:05 +08:00
|
|
|
type UserEmailLogin struct {
|
2022-08-07 20:38:21 +08:00
|
|
|
config UserEmailLoginConfig
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
Email string `form:"email" json:"email"`
|
|
|
|
Password string `form:"password" json:"password"`
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:38:21 +08:00
|
|
|
// UserEmailLoginConfig is the [UserEmailLogin] factory initializer config.
|
|
|
|
//
|
|
|
|
// NB! App is required struct member.
|
|
|
|
type UserEmailLoginConfig struct {
|
|
|
|
App core.App
|
|
|
|
TxDao *daos.Dao
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserEmailLogin creates a new [UserEmailLogin] form with
|
|
|
|
// initializer config created from the provided [core.App] instance.
|
|
|
|
//
|
|
|
|
// This factory method is used primarily for convenience (and backward compatibility).
|
|
|
|
// If you want to submit the form as part of another transaction, use
|
|
|
|
// [NewUserEmailLoginWithConfig] with explicitly set TxDao.
|
2022-07-07 05:19:05 +08:00
|
|
|
func NewUserEmailLogin(app core.App) *UserEmailLogin {
|
2022-08-07 20:38:21 +08:00
|
|
|
return NewUserEmailLoginWithConfig(UserEmailLoginConfig{
|
|
|
|
App: app,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserEmailLoginWithConfig creates a new [UserEmailLogin]
|
|
|
|
// form with the provided config or panics on invalid configuration.
|
|
|
|
func NewUserEmailLoginWithConfig(config UserEmailLoginConfig) *UserEmailLogin {
|
|
|
|
form := &UserEmailLogin{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()
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return form
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate makes the form validatable by implementing [validation.Validatable] interface.
|
|
|
|
func (form *UserEmailLogin) Validate() error {
|
|
|
|
return validation.ValidateStruct(form,
|
|
|
|
validation.Field(&form.Email, validation.Required, validation.Length(1, 255), is.Email),
|
|
|
|
validation.Field(&form.Password, validation.Required, validation.Length(1, 255)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submit validates and submits the form.
|
|
|
|
// On success returns the authorized user model.
|
|
|
|
func (form *UserEmailLogin) Submit() (*models.User, error) {
|
|
|
|
if err := form.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:38:21 +08:00
|
|
|
user, err := form.config.TxDao.FindUserByEmail(form.Email)
|
2022-07-07 05:19:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.ValidatePassword(form.Password) {
|
|
|
|
return nil, validation.NewError("invalid_login", "Invalid login credentials.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|