Documentation
¶
Overview ¶
Package user provides services for managing users.
Index ¶
- Variables
- type Locale
- type SessionToken
- type User
- type UserAvatarStorage
- type UserID
- type UserPassword
- type UserService
- func (s UserService) CreateUser(ctx context.Context, email, password, name string) (User, error)
- func (s UserService) RegisterSession(ctx context.Context, id UserID, userAgent string) (SessionToken, error)
- func (s UserService) SetUserAvatar(ctx context.Context, id UserID, a asset.CompressedAsset[io.Reader]) error
- func (s UserService) UpdateUserEmailPassword(ctx context.Context, id UserID, email, password string) error
- func (s UserService) UpdateUserLocale(ctx context.Context, id UserID, locale Locale) error
- func (s UserService) UpdateUserName(ctx context.Context, id UserID, name string) error
- func (s UserService) User(ctx context.Context, id UserID) (User, error)
- func (s UserService) UserAvatar(ctx context.Context, id UserID) (asset.CompressedAsset[io.ReadCloser], error)
- func (s UserService) ValidateSession(ctx context.Context, token string) (User, error)
- func (s UserService) ValidateUserEmailPassword(ctx context.Context, email, password string) (UserID, error)
- type UserSessionStorage
- type UserStorage
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidEmail = errors.New("invalid email")
ErrInvalidEmail is returned when the email is invalid.
var ErrInvalidSession = errors.New("invalid session")
ErrInvalidSession is returned when the session is invalid, either because it is unknown or expired.
var ErrPasswordTooShort = errors.New("password too short")
ErrPasswordTooShort is returned when the password is too short.
var ErrUnknownUser = errors.New("unknown user")
ErrUnknownUser is returned when the user is unknown.
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 ¶
ParseLocale parses a locale string into a Locale type.
func (Locale) String ¶
String implements the fmt.Stringer interface.
func (Locale) Tags ¶
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.
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 UserPassword ¶
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 (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 (UserService) UpdateUserLocale ¶
func (UserService) UpdateUserName ¶
func (UserService) UserAvatar ¶
func (s UserService) UserAvatar(ctx context.Context, id UserID) (asset.CompressedAsset[io.ReadCloser], error)
func (UserService) ValidateSession ¶
func (UserService) ValidateUserEmailPassword ¶
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 }