Documentation
¶
Index ¶
- func Handler(verificationKey string) routing.Handler
- func MockAuthHandler(c *routing.Context) error
- func MockAuthHeader() http.Header
- func RegisterHandlers(rg *routing.RouteGroup, service Service, logger log.Logger)
- func WithUser(ctx context.Context, id, name string) context.Context
- type Identity
- type Repository
- type Service
- type SignUpRequest
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MockAuthHandler ¶
MockAuthHandler creates a mock authentication middleware for testing purpose. If the request contains an Authorization header whose value is "TEST", then it considers the user is authenticated as "Tester" whose ID is "100". It fails the authentication otherwise.
func MockAuthHeader ¶
MockAuthHeader returns an HTTP header that can pass the authentication check by MockAuthHandler.
func RegisterHandlers ¶
func RegisterHandlers(rg *routing.RouteGroup, service Service, logger log.Logger)
RegisterHandlers registers handlers for different HTTP requests.
Types ¶
type Identity ¶
type Identity interface { // GetID returns the user ID. GetID() string // GetName returns the user name. GetName() string }
Identity represents an authenticated user identity.
func CurrentUser ¶
CurrentUser returns the user identity from the given context. Nil is returned if no user identity is found in the context.
type Repository ¶
type Repository interface { FindUserByName(ctx context.Context, username string) (entity.User, error) // Create saves a new user in the storage. Create(ctx context.Context, user entity.User) error HashPassword(password string) string CheckPasswordHash(password, hash string) bool }
Repository todo
func NewRepository ¶
func NewRepository(db *dbcontext.DB, logger log.Logger) Repository
NewRepository creates a new album repository
type Service ¶
type Service interface { // authenticate authenticates a user using username and password. // It returns a JWT token if authentication succeeds. Otherwise, an error is returned. Login(ctx context.Context, username, password string) (string, error) SignUp(ctx context.Context, req SignUpRequest) (User, error) }
Service encapsulates the authentication logic.
func NewService ¶
NewService creates a new authentication service.
type SignUpRequest ¶
func (SignUpRequest) Validate ¶
func (r SignUpRequest) Validate() error
Validate validates the CreateAlbumRequest fields.