users

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

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation = "activation"
	Authentication  = "authentication"
)

Define constants for the token scope. For now we just define the scope "activation" but we'll add additional scopes later in the book.

Variables

View Source
var AnonymousUser = &User{}

Declare a new AnonymousUser variable.

View Source
var (
	ErrorDuplicateEmail = errors.New("duplicate email")
)

Define a custom ErrorDuplicateEmail error.

Functions

func ValidateEmail

func ValidateEmail(v *validator.Validator, email string)

func ValidatePasswordPlainText

func ValidatePasswordPlainText(v *validator.Validator, password string)

func ValidateTokenPlaintext

func ValidateTokenPlaintext(v *validator.Validator, tokenPlaintext string)

Check that the plaintext token has been provided and is exactly 26 bytes long.

func ValidateUser

func ValidateUser(v *validator.Validator, user *User)

Types

type Token

type Token struct {
	Plaintext string    `json:"token"`
	Email     string    `json:"email"`
	Hash      []byte    `json:"-"`
	Expiry    time.Time `json:"expiry"`
	Scope     string    `json:"-"`
}

type TokenKeyBasedStruct

type TokenKeyBasedStruct struct {
	Hash []byte `dynamodbav:"hash"`
}

type TokenService

type TokenService struct {
	Store     *clients.DynamoDbClientWrapper
	TableName string
}

func NewTokenSrv

func NewTokenSrv(store *clients.DynamoDbClientWrapper, tableName string) *TokenService

func (*TokenService) Get

func (s *TokenService) Get(tokenPlaintext string) (*Token, error)

func (*TokenService) Insert

func (s *TokenService) Insert(token *Token) error

func (*TokenService) New

func (s *TokenService) New(email string, ttl time.Duration, scope string) (*Token, error)

type User

type User struct {
	Email     string   `json:"email"`
	Name      string   `json:"name"`
	CreatedAt string   `json:"created_at"`
	Password  password `json:"-"`
	Activated bool     `json:"activated"`
	Version   int      `json:"-"`
}

func NewUser

func NewUser(email, name, createdAt string, password_hash []byte, version int, activated bool) *User

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

Check if a User instance is the AnonymousUser.

type UserKeyBasedStruct

type UserKeyBasedStruct struct {
	Email string `dynamodbav:"email"`
}

type UserService

type UserService struct {
	Store     *clients.DynamoDbClientWrapper
	TableName string
}

func New

func New(store *clients.DynamoDbClientWrapper, tableName string) *UserService

func (UserService) GetByEmail

func (s UserService) GetByEmail(email string) (*User, error)

Retrieve the User details from the database based on the user's email address. Because we have a UNIQUE constraint on the email column, this SQL query will only return one record (or none at all, in which case we return a ErrorRecordNotFound error).

func (UserService) Insert

func (s UserService) Insert(user *User) error

func (UserService) Update

func (s UserService) Update(user *User) error

Update the details for a specific user. Notice that we check against the version field to help prevent any race conditions during the request cycle, just like we did when updating a movie. And we also check for a violation of the "users_email_key" constraint when performing the update, just like we did when inserting the user record originally.

Jump to

Keyboard shortcuts

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