func CheckPassword(hashed []byte, plain string) error
CheckPassword verifies a plaintext password against its bcrypt hash. Returns nil if the password matches, or an error if verification fails.
func HashPassword(pw string) ([]byte, error)
HashPassword generates a bcrypt hash from a plaintext password using the default cost. Returns the hashed password as bytes or an error if hashing fails.
Handler handles HTTP requests for user operations.
type Handler struct { Store *InMemoryStore // Store provides data persistence for users }
func NewHandler(store *InMemoryStore) *Handler
NewHandler creates a new Handler with the provided store.
func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request)
CreateUser handles POST requests to create a new user. It validates the request, hashes the password, and stores the user. Returns 201 Created on success, or appropriate error status codes.
InMemoryStore provides thread-safe in-memory storage for users. It maintains two maps: one for user lookup by ID and another for email uniqueness.
type InMemoryStore struct {
// contains filtered or unexported fields
}
func NewInMemoryStore() *InMemoryStore
NewInMemoryStore creates and initializes a new empty InMemoryStore.
func (s *InMemoryStore) CreateUser(username, email string, hashedPassword []byte) (*User, error)
CreateUser creates a new user with the provided credentials. It generates a unique UUID for the user and ensures email uniqueness. Returns the created user or an error if the email is already registered.
User represents a user in the system with authentication credentials.
type User struct { ID string `json:"id"` // Unique identifier for the user Username string `json:"username"` // Display name for the user Email string `json:"email"` // Email address (must be unique) HashedPassword []byte `json:"-"` // Bcrypt-hashed password (excluded from JSON) }