user
v1.1.1
Documentation
pkg.go.dev ↗Package user provides the database implementation for the User entity.
Variables
MaxAge (4 declarations)
var (
MaxAge = 5 * 365 * 86400
ErrAuth = errors.New("invalid credentials")
ErrExists = errors.New("user exists")
ErrTokenExpired = errors.New("token expired")
)
Functions
Model
func Model(uu []*User) func(int) database.Model
Model is called along with database.ModelSlice to convert the given slice of User models to a slice of database.Model interfaces.
Select
func Select(col string, opts ...query.Option) query.Query
Select returns a query that selects the given column from the users table, with each given query.Option applied to the returned query.
WhereHandle
func WhereHandle(handle string) query.Option
WhereHandle returns a query.Option that when applied to a query will add two WHERE clauses that will check the given handle against the email column or the username column.
Types
DeleteForm
type DeleteForm struct {
Password string `schema:"delete_password"`
}
DeleteForm.Fields
func (f DeleteForm) Fields() map[string]string
DeleteForm.Validate
func (f DeleteForm) Validate() error
EmailForm
type EmailForm struct {
User *User `schema:"-"`
Users *Store `schema:"-"`
Email string `schema:"email"`
VerifyPassword string `schema:"verify_password"`
}
EmailForm.Fields
func (f EmailForm) Fields() map[string]string
Fields returns a map containing the Email field of the current EmailForm.
EmailForm.Validate
func (f EmailForm) Validate() error
Validate checks to see if the Email field is present, valid, and unique. The uniqueness checks will be skipped if the email matches the existing one. The Password field is checked for presence, then compared against the one in the database for authentication.
LoginForm
type LoginForm struct {
Handle string `schema:"handle"`
Password string `schema:"password"`
RedirectURI string `schema:"redirect_uri"`
}
LoginForm.Fields
func (f LoginForm) Fields() map[string]string
Fields returns a map containing the Handle field of the current LoginForm.
LoginForm.Validate
func (f LoginForm) Validate() error
Validate checks to see if the current LoginForm has a present Handle, and Password field.
NewPasswordForm
type NewPasswordForm struct {
Token string `schema:"token"`
Password string `schema:"password"`
VerifyPassword string `schema:"verify_password"`
}
NewPasswordForm.Fields
func (f NewPasswordForm) Fields() map[string]string
NewPasswordForm.Validate
func (f NewPasswordForm) Validate() error
PasswordForm
type PasswordForm struct {
User *User `schema:"-"`
Users *Store `schema:"-"`
OldPassword string `schema:"old_password"`
NewPassword string `schema:"new_password"`
VerifyPassword string `schema:"verify_password"`
}
PasswordForm.Fields
func (PasswordForm) Fields() map[string]string
Fields will return an empty map of strings.
PasswordForm.Validate
func (f PasswordForm) Validate() error
Validate the current PasswordForm. This checks for the presence of the old, new, and current password fields, as well as if the current password is valid based on what is in the database.
PasswordResetForm
type PasswordResetForm struct {
Email string `schema:"email"`
}
PasswordResetForm.Fields
func (f PasswordResetForm) Fields() map[string]string
PasswordResetForm.Validate
func (f PasswordResetForm) Validate() error
RegisterForm
type RegisterForm struct {
Users *Store
Email string `schema:"email"`
Username string `schema:"username"`
Password string `schema:"password"`
VerifyPassword string `schema:"verify_password"`
}
RegisterForm.Fields
func (f RegisterForm) Fields() map[string]string
Fields returns a map of the Email and Username fields.
RegisterForm.Validate
func (f RegisterForm) Validate() error
Validate checks to see if the Email field is present and valid. An email is considered valid if it is less than 254 characters in length, and contains an @ character. The Username field is checked for presence, uniqueness, and validitity. A username must be between 3 and 64 characters, and only contain letters, numbers, dashes, and dots. The Password field is checked for presence, and length. It should be between 6 and 60 characters.
Store
type Store struct {
database.Store
}
Store is the type for creating and modifying User models in the database.
NewStore
func NewStore(db *sqlx.DB, mm ...database.Model) *Store
NewStore returns a new Store for querying the users table. Each database passed to this function will be bound to the returned Store.
Store.All
func (s *Store) All(opts ...query.Option) ([]*User, error)
All returns a slice of User models, applying each query.Option that is given.
Store.Auth
func (s *Store) Auth(handle, password string) (*User, error)
Auth looks up the user by the given handle, and checks that the given password matches the hash in the database.
Store.Bind
func (s *Store) Bind(_ ...database.Model)
Bind implements the database.Model interface. This does nothing.
Store.Create
func (s *Store) Create(email, username string, password []byte) (*User, string, error)
Create creates a new user with the given email, username and password. The given password is hashed via bcrypt using the default cost.
Store.Delete
func (s *Store) Delete(id int64, password []byte) error
Delete the user with the given id. This will set the deleted_at field in the table to the time at which this method was called.
Store.Get
func (s *Store) Get(opts ...query.Option) (*User, error)
Get returns a single User database, applying each query.Option that is given.
Store.Load
func (s *Store) Load(key string, vals []interface{}, load database.LoaderFunc) error
Load loads in a slice of User models where the given key is in the list of given vals. Each database is loaded individually via a call to the given load callback.
Store.New
func (*Store) New() *User
New returns a new zero-value User model.
Store.RequestVerify
func (s *Store) RequestVerify(id int64) (string, error)
Store.ResetPassword
func (s *Store) ResetPassword(id int64) (string, error)
Store.Update
func (s *Store) Update(id int64, email string, cleanup bool, password []byte) error
Update sets the email, cleanup, and password fields for the given user to the given values. If the given password is nil, then this will not be updated, otherwise a new hash is generated for it.
Store.UpdatePassword
func (s *Store) UpdatePassword(tok string, password []byte) error
Store.Verify
func (s *Store) Verify(tok string) error
User
type User struct {
ID int64 `db:"id"`
Email string `db:"email"`
Username string `db:"username"`
Password []byte `db:"password"`
Verified bool `db:"verified"`
Cleanup bool `db:"cleanup"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt sql.NullTime `db:"deleted_at"`
Permissions map[string]struct{} `db:"-"`
}
User represents a user account in the database. This will either be created through registration, or sign-on via an OAuth provider.
FromContext
func FromContext(ctx context.Context) (*User, bool)
FromContext returns the *User database from the given context value, if any.
User.Bind
func (*User) Bind(_ ...database.Model)
Bind implements the database.Model interface. This does nothing.
User.Endpoint
func (*User) Endpoint(_ ...string) string
Endpoint implements the database.Model interface. This returns an empty string.
User.IsZero
func (u *User) IsZero() bool
IsZero implements the database.Model interface.
User.JSON
func (u *User) JSON(_ string) map[string]interface{}
JSON implements the database.Model interface. This will return a map with the values of the current user under each key. This will not include the password field.
User.Primary
func (u *User) Primary() (string, int64)
Primary implements the database.Model interface.
User.SetPermission
func (u *User) SetPermission(perm string)
SetPermission set's the given permission in the underlying Permissions map of the current User. If the map is nil then it will be initialized.
User.SetPrimary
func (u *User) SetPrimary(id int64)
SetPrimary implements the database.Model interface.
User.Values
func (u *User) Values() map[string]interface{}
Values implements the databae.Model interface. This will return a map with the following values, email, username, password, updated_at, and deleted_at.