Documentation ¶
Index ¶
- func BearerHandler(verificationKey string) routing.Handler
- func JWTHandler(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
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BearerHandler ¶
BearerHandler returns a Bearer token-based authentication middleware.
func JWTHandler ¶
JWTHandler returns a JWT-based authentication middleware.
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 // GetEmail returns the user email. GetEmail() 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 { // GetUserByEmail passes the email to the database and returns the user GetUserByEmail(ctx context.Context, email string) (entity.User, error) // GetActiveUserByEmail passes the email to the database and returns the active user GetActiveUserByEmail(ctx context.Context, email string) (entity.User, error) // CreateUser stores a new user in the database CreateUser(ctx context.Context, user entity.User) error }
Repository encapsulates the logic to access info from the data source.
func NewRepository ¶
func NewRepository(db *dbcontext.DB, logger log.Logger) Repository
NewRepository creates a new auth repository
type Service ¶
type Service interface { // authenticate authenticates a user using email and password. // It returns a JWT token if authentication succeeds. Otherwise, an error is returned. Login(ctx context.Context, email, password string) (string, error) // Register registers a new user. Register(ctx context.Context, name, email, password string) error }
Service encapsulates the authentication logic.
func NewService ¶
NewService creates a new authentication service.