users

package
v0.0.0-...-9ee207d Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: MIT Imports: 10 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 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 SQLStore

type SQLStore struct {
	DB *sql.DB
}

SQLStore is db used for updating sql database

func NewSQLStore

func NewSQLStore(d *sql.DB) *SQLStore

NewSQLStore method

func (*SQLStore) Delete

func (ss *SQLStore) Delete(id int64) error

Delete deletes the user with the given ID

func (*SQLStore) GetByEmail

func (ss *SQLStore) GetByEmail(email string) (*User, error)

GetByEmail returns the User with the given email

func (*SQLStore) GetByID

func (ss *SQLStore) GetByID(id int64) (*User, error)

GetByID returns the User with the given ID

func (*SQLStore) GetByUserName

func (ss *SQLStore) GetByUserName(username string) (*User, error)

GetByUserName returns the User with the given Username

func (*SQLStore) Insert

func (ss *SQLStore) 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 (*SQLStore) Log

func (ss *SQLStore) Log(userid int64, dt time.Time, ip string) error

Log inserts the user into the database=

func (*SQLStore) Update

func (ss *SQLStore) Update(id int64, updates *Updates) (*User, error)

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

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

	//Logs user
	Log(userid int64, dt time.Time, ip string) error
}

Store represents a store for Users

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