...

Package user

import "jayps.com/go-docs/user"
Overview
Index

Overview ▾

Package user provides HTTP handlers and data structures for user management operations.

Package user provides data models for user management.

Package user provides password hashing and verification utilities.

Package user provides in-memory storage for user data.

func CheckPassword

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

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.

type Handler

Handler handles HTTP requests for user operations.

type Handler struct {
    Store *InMemoryStore // Store provides data persistence for users
}

func NewHandler

func NewHandler(store *InMemoryStore) *Handler

NewHandler creates a new Handler with the provided store.

func (*Handler) CreateUser

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.

type InMemoryStore

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

func NewInMemoryStore() *InMemoryStore

NewInMemoryStore creates and initializes a new empty InMemoryStore.

func (*InMemoryStore) CreateUser

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.

type User

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)
}