user

package
v2.0.0-...-549b09c Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package user provides services for managing users.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidEmail = errors.New("invalid email")

ErrInvalidEmail is returned when the email is invalid.

View Source
var ErrInvalidSession = errors.New("invalid session")

ErrInvalidSession is returned when the session is invalid, either because it is unknown or expired.

View Source
var ErrPasswordTooShort = errors.New("password too short")

ErrPasswordTooShort is returned when the password is too short.

View Source
var ErrUnknownUser = errors.New("unknown user")

ErrUnknownUser is returned when the user is unknown.

View Source
var NullUserID = UserID{}

NullUserID is a zero value for UserID.

Functions

This section is empty.

Types

type Locale

type Locale string

Locale is a user's preferred languages. It is used for localization. The format of the string is specified by RFC 2616 but is validated by language.ParseAcceptLanguage, which is more lax.

func ParseLocale

func ParseLocale(locale string) (Locale, error)

ParseLocale parses a locale string into a Locale type.

func (Locale) String

func (l Locale) String() string

String implements the fmt.Stringer interface.

func (Locale) Tags

func (l Locale) Tags() []language.Tag

Tags returns the Locale as a list of language tags. If l is empty or invalid, then this function returns one language.Und. The returned list is never empty.

func (Locale) Validate

func (l Locale) Validate() error

Validate checks if the Locale is valid.

type SessionToken

type SessionToken struct {
	UserID UserID
	// contains filtered or unexported fields
}

SessionToken is a token that represents a user session. The type represents an already validated token. The random part is not exposed to the user except via [String].

func ParseSessionToken

func ParseSessionToken(token string) (SessionToken, error)

ParseSessionToken parses a token string into a SessionToken.

func (SessionToken) String

func (t SessionToken) String() string

String returns the token as a string.

type User

type User struct {
	ID        UserID `json:"id"`
	Email     string `json:"email"`
	Name      string `json:"name"`
	Locale    Locale `json:"locale"`
	HasAvatar bool   `json:"hasAvatar,omitempty"`
}

User is a user in the system.

type UserAvatarStorage

type UserAvatarStorage interface {
	// UserAvatar returns the user's avatar.
	// The returned asset is an open reader that must be closed by the caller.
	UserAvatar(ctx context.Context, id UserID) (asset.CompressedAsset[io.ReadCloser], error)
	// SetUserAvatar sets the user's avatar.
	// The caller must still close the given reader.
	SetUserAvatar(ctx context.Context, id UserID, a asset.CompressedAsset[io.Reader]) error
}

type UserID

type UserID xid.ID

UserID is a unique identifier for a user.

func (UserID) String

func (id UserID) String() string

String formats the user ID into a string.

type UserPassword

type UserPassword struct {
	UserID   UserID
	Passhash []byte
}

type UserService

type UserService struct {
	// contains filtered or unexported fields
}

UserService is a service for managing users.

func NewUserService

func NewUserService(storage UserStorage, avatar UserAvatarStorage, session UserSessionStorage) UserService

func (UserService) CreateUser

func (s UserService) CreateUser(ctx context.Context, email, password, name string) (User, error)

func (UserService) RegisterSession

func (s UserService) RegisterSession(ctx context.Context, id UserID, userAgent string) (SessionToken, error)

func (UserService) SetUserAvatar

func (s UserService) SetUserAvatar(ctx context.Context, id UserID, a asset.CompressedAsset[io.Reader]) error

func (UserService) UpdateUserEmailPassword

func (s UserService) UpdateUserEmailPassword(ctx context.Context, id UserID, email, password string) error

func (UserService) UpdateUserLocale

func (s UserService) UpdateUserLocale(ctx context.Context, id UserID, locale Locale) error

func (UserService) UpdateUserName

func (s UserService) UpdateUserName(ctx context.Context, id UserID, name string) error

func (UserService) User

func (s UserService) User(ctx context.Context, id UserID) (User, error)

func (UserService) UserAvatar

func (UserService) ValidateSession

func (s UserService) ValidateSession(ctx context.Context, token string) (User, error)

func (UserService) ValidateUserEmailPassword

func (s UserService) ValidateUserEmailPassword(ctx context.Context, email, password string) (UserID, error)

type UserSessionStorage

type UserSessionStorage interface {
	// RegisterSession registers a session for a user. The token is generated by
	// [UserService]. The userAgent is optional.
	RegisterSession(ctx context.Context, id UserID, token []byte, userAgent string) error
	// ValidateSession validates a session for a user. The user that the session
	// belongs to is returned.
	ValidateSession(ctx context.Context, token []byte) (User, error)
}

type UserStorage

type UserStorage interface {
	CreateUser(ctx context.Context, id UserID, email string, passhash []byte, name string) (User, error)
	User(ctx context.Context, id UserID) (User, error)
	UserPasswordFromEmail(ctx context.Context, email string) (UserPassword, error)
	UpdateUserEmailPassword(ctx context.Context, id UserID, email string, passhash []byte) error
	UpdateUserName(ctx context.Context, id UserID, name string) error
	UpdateUserLocale(ctx context.Context, id UserID, locale Locale) error
}

Jump to

Keyboard shortcuts

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