users

package
v0.0.0-...-cbd5fac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound is returned when the user can't be found

Functions

This section is empty.

Types

type Credentials

type Credentials struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

Credentials represents user sign-in credentials

type MySQLStore

type MySQLStore struct {
	Client *sql.DB
}

MySQLStore represents a users.Store backed by MySQL.

func NewMySQLStore

func NewMySQLStore(client *sql.DB) *MySQLStore

NewMySQLStore constructs a new MySQLStore

func (*MySQLStore) Delete

func (ms *MySQLStore) Delete(id int64) error

Delete deletes the user with the given ID

func (*MySQLStore) GetByEmail

func (ms *MySQLStore) GetByEmail(email string) (*User, error)

GetByEmail returns the User with the given email

func (*MySQLStore) GetByID

func (ms *MySQLStore) GetByID(id int64) (*User, error)

GetByID returns the User with the given ID

func (*MySQLStore) GetByUserName

func (ms *MySQLStore) GetByUserName(username string) (*User, error)

GetByUserName returns the User with the given Username

func (*MySQLStore) Insert

func (ms *MySQLStore) Insert(user *User) (*User, error)

Insert inserts the user into the database, and returns the newly-inserted User, complete with the DBMS-assigned ID

func (*MySQLStore) LogUser

func (ms *MySQLStore) LogUser(id int64, time time.Time, clientIP string) error

LogUser logs a successful sign-in by a user with the user ID, curent time, and user IP address

func (*MySQLStore) Update

func (ms *MySQLStore) Update(id int64, updates *Updates) (*User, error)

Update applies UserUpdates to the given user ID and returns the newly-updated user

type NewUser

type NewUser struct {
	Email        string `json:"email"`
	Password     string `json:"password"`
	PasswordConf string `json:"passwordConf"`
	UserName     string `json:"userName"`
	FirstName    string `json:"firstName"`
	LastName     string `json:"lastName"`
}

NewUser represents a new user signing up for an account

func (*NewUser) ToUser

func (nu *NewUser) ToUser() (*User, error)

ToUser converts the NewUser to a User, setting the PhotoURL and PassHash fields appropriately

func (*NewUser) Validate

func (nu *NewUser) Validate() error

Validate validates the new user and returns an error if any of the validation rules fail, or nil if its valid

type Store

type Store interface {
	// GetByID returns the User with the given ID
	GetByID(id int64) (*User, error)

	// GetByEmail returns the User with the given email
	GetByEmail(email string) (*User, error)

	// GetByUserName returns the User with the given Username
	GetByUserName(username string) (*User, error)

	// Insert inserts the user into the database, and returns
	// the newly-inserted User, complete with the DBMS-assigned ID
	Insert(user *User) (*User, error)

	// Update applies UserUpdates to the given user ID
	// and returns the newly-updated user
	Update(id int64, updates *Updates) (*User, error)

	// Delete deletes the user with the given ID
	Delete(id int64) error

	// LogUser logs a successful sign-in by a user with the user ID, curent time,
	// and user IP address
	LogUser(id int64, time time.Time, clientIP string) error
}

Store represents a store for Users

type TestUserStore

type TestUserStore struct {
	Client string
}

TestUserStore represents a UserStore with mock data

func NewTestUserStore

func NewTestUserStore(client string) *TestUserStore

NewTestUserStore creates a NewTestUserStore

func (*TestUserStore) Delete

func (client *TestUserStore) Delete(id int64) error

Delete deletes the user with the given ID

func (*TestUserStore) GetByEmail

func (client *TestUserStore) GetByEmail(email string) (*User, error)

GetByEmail returns the User with the given email

func (*TestUserStore) GetByID

func (client *TestUserStore) GetByID(id int64) (*User, error)

GetByID returns the User with the given ID

func (*TestUserStore) GetByUserName

func (client *TestUserStore) GetByUserName(username string) (*User, error)

GetByUserName returns the User with the given Username

func (*TestUserStore) Insert

func (client *TestUserStore) Insert(user *User) (*User, error)

Insert inserts the user into the database, and returns the newly-inserted User, complete with the DBMS-assigned ID

func (*TestUserStore) LogUser

func (client *TestUserStore) LogUser(id int64, time time.Time, clientIP string) error

LogUser logs a successful sign-in by a user with the user ID, curent time, and user IP address

func (*TestUserStore) Update

func (client *TestUserStore) Update(id int64, updates *Updates) (*User, error)

Update applies UserUpdates to the given user ID and returns the newly-updated user

type Updates

type Updates struct {
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
}

Updates represents allowed updates to a user profile

type User

type User struct {
	ID        int64  `json:"id"`
	Email     string `json:"-"` //never JSON encoded/decoded
	PassHash  []byte `json:"-"` //never JSON encoded/decoded
	UserName  string `json:"userName"`
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
	PhotoURL  string `json:"photoURL"`
}

User represents a user account in the database

func (*User) ApplyUpdates

func (u *User) ApplyUpdates(updates *Updates) error

ApplyUpdates applies the updates to the user. An error is returned if the updates are invalid

func (*User) Authenticate

func (u *User) Authenticate(password string) error

Authenticate compares the plaintext password against the stored hash and returns an error if they don't match, or nil if they do

func (*User) FullName

func (u *User) FullName() string

FullName returns the user's full name, in the form:

"<FirstName> <LastName>"

If either first or last name is an empty string, no space is put between the names. If both are missing, this returns an empty string

func (*User) SetPassword

func (u *User) SetPassword(password string) error

SetPassword hashes the password and stores it in the PassHash field

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL