users

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleAdmin role = "admin"
	RoleUser  role = "user"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateUserInput

type CreateUserInput struct {
	Fullname        string
	Username        string
	Birthdate       string
	Email           string
	Password        string
	ConfirmPassword string
}

CreateUserInput represents the input data for creating a user

type DefaultService

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

func New

func New(logger *zap.Logger, jwtSigningKey string, repo repo, opts ...ServiceOption) *DefaultService

New instantiates a new users service

func (*DefaultService) Create

func (s *DefaultService) Create(ctx context.Context, in CreateUserInput) (*User, error)

Create creates a new user and returns the created user

func (*DefaultService) Delete

func (s *DefaultService) Delete(ctx context.Context, id string) error

func (*DefaultService) FetchByID

func (s *DefaultService) FetchByID(ctx context.Context, id string) (*User, error)

FetchByID fetches a user by id and returns the user

func (*DefaultService) GenerateToken

func (s *DefaultService) GenerateToken(ctx context.Context, email, password string) (string, error)

GenerateToken generates a JWT token for the user

func (*DefaultService) SendEmailVerification

func (s *DefaultService) SendEmailVerification(ctx context.Context, userID, username, to string) error

func (*DefaultService) VerifyToken

func (s *DefaultService) VerifyToken(ctx context.Context, token string) (*VerifyTokenResponse, error)

VerifyToken verifies a JWT token and returns the authentication data

type E

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

func (E) Error

func (e E) Error() string

type MockService

type MockService struct {
	CreateFunc                func(ctx context.Context, in CreateUserInput) (*User, error)
	DeleteFunc                func(ctx context.Context, id string) error
	FetchByIDFunc             func(ctx context.Context, id string) (*User, error)
	GenerateTokenFunc         func(ctx context.Context, email, password string) (string, error)
	VerifyTokenFunc           func(ctx context.Context, token string) (*VerifyTokenResponse, error)
	SendEmailVerificationFunc func(ctx context.Context, userID, username, to string) error
}

func (*MockService) Create

func (m *MockService) Create(ctx context.Context, in CreateUserInput) (*User, error)

func (*MockService) Delete

func (m *MockService) Delete(ctx context.Context, id string) error

func (*MockService) FetchByID

func (m *MockService) FetchByID(ctx context.Context, id string) (*User, error)

func (*MockService) GenerateToken

func (m *MockService) GenerateToken(ctx context.Context, email, password string) (string, error)

func (*MockService) SendEmailVerification

func (m *MockService) SendEmailVerification(ctx context.Context, userID, username, to string) error

func (*MockService) VerifyToken

func (m *MockService) VerifyToken(ctx context.Context, token string) (*VerifyTokenResponse, error)

type ParsableError

type ParsableError interface {
	Error() string
}

type Service

type Service interface {
	// Create creates a new user and returns the created user with its ID and "user" role
	Create(ctx context.Context, in CreateUserInput) (*User, error)

	// Delete soft deletes a user by id
	Delete(ctx context.Context, id string) error

	// FetchByID fetches a non-deleted user by id and returns the user
	FetchByID(ctx context.Context, id string) (*User, error)

	// GenerateToken generates a JWT token for the user
	GenerateToken(ctx context.Context, email, password string) (string, error)

	// VerifyToken verifies a JWT token and returns the user username, id and role
	VerifyToken(ctx context.Context, token string) (*VerifyTokenResponse, error)

	// SendEmailVerification sends an email verification to the user.
	// The user must be created before calling this method.
	SendEmailVerification(ctx context.Context, userID, username, to string) error
}

Service defines the service interface

type ServiceOption added in v0.0.4

type ServiceOption func(*DefaultService)

func WithEmailVerification added in v0.0.4

func WithEmailVerification(fromName, fromAddr, endpoint string, emailer emailer) ServiceOption

type User

type User struct {
	ID            string
	Fullname      string
	Username      string
	Birthdate     string
	Email         string
	EmailVerified bool
	Role          role
	CreatedAt     time.Time
	UpdatedAt     time.Time
}

User represents a user domain model

type VerifyTokenResponse

type VerifyTokenResponse struct {
	ID, Username, Role string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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