Skip to main content

Overview

The auth package provides a full-featured authentication domain with user registration, login/logout, password management, and session-based authentication middleware.

Domain

auth.Domain

The main domain aggregate that bundles all auth components.
*Repository
Database repository for user persistence
*Service
Business logic service for authentication operations
*httpserver.HandlerGroup
Pre-configured HTTP handlers for auth endpoints
httpserver.Middleware
Authentication middleware for protecting routes

Constructor

db
required
Database interface (sqlx-compatible)
authStorage
required
Session storage interface (typically session.Service)
Name of the HTTP cookie for session ID
func(string) error
Custom username validation function (optional, uses default if nil)
func(string) error
Custom password validation function (optional, uses default if nil)
cleanupEnqueuer
Queue enqueuer for user cleanup jobs (optional)

Service

auth.Service

Core business logic for authentication and user management.

Methods

Register

Creates a new user account with username and password. Validates inputs, generates salt, hashes password with bcrypt.
string
required
Username for the new account (validated by usernameValidator)
string
required
Password for the new account (validated by passwordValidator)
error
Returns ErrInvalidUsername, ErrInvalidPassword, or creation error
Default validation rules:
  • Username: 5-20 characters
  • Password: 8-100 characters

Login

Authenticates user credentials and creates a new session.
string
required
Username to authenticate
string
required
Password to verify
string
New session ID on success
error
Returns ErrWrongUserOrPassword if credentials invalid

Logout

Deletes a session from storage.
string
required
Session ID to delete

Get User

Retrieves a user by ID.
Retrieves a user from a session ID.

Change Password

Changes the authenticated user’s password. User must be in context (requires middleware).
string
required
Current password for verification
string
required
New password (validated by passwordValidator)
error
Returns ErrCurrentPasswordIncorrect, ErrInvalidPassword, or update error

Delete User

Deletes the authenticated user and all their sessions. User must be in context (requires middleware). Optionally enqueues cleanup job.

Repository

auth.Repository

Database operations for user persistence.

Methods

Middleware

auth.AuthenticationMiddleware

HTTP middleware that validates sessions and injects users into request context.
Behavior:
  1. Reads session cookie from request
  2. Validates session with auth storage
  3. Retrieves user from session
  4. Injects user into request context
  5. Returns 401 Unauthorized if session invalid
Usage:

HTTP Handlers

All handlers are pre-configured in Domain.HandleGroup with these routes:

POST /register

Request:
Response:
  • 201 Created - User created successfully
  • 400 Bad Request - Invalid username or password
  • 500 Internal Server Error - Creation failed

POST /login

Request:
Response:
  • 200 OK - Sets session cookie
  • 401 Unauthorized - Invalid credentials
  • 500 Internal Server Error - Session creation failed

POST /logout

Response:
  • 200 OK - Clears session cookie
  • 500 Internal Server Error - Session deletion failed

GET /me

Response:
  • 200 OK - Returns user info
  • 401 Unauthorized - No valid session
  • 500 Internal Server Error - Retrieval failed

POST /change-password

Request:
Response:
  • 200 OK - Password changed
  • 400 Bad Request - Invalid new password
  • 401 Unauthorized - Current password incorrect
  • 500 Internal Server Error - Update failed

DELETE /me

Response:
  • 200 OK - User deleted
  • 401 Unauthorized - No valid session
  • 500 Internal Server Error - Deletion failed

Models

User

string
UUID primary key
string
Unique username
string
Bcrypt-hashed password with salt
string
Random UUID used in password hashing
time.Time
Account creation timestamp
time.Time
Last update timestamp
Status
Account status: “active”, “inactive”, or “deleted”

Status

Context Helpers

UserFromContext

Retrieves the authenticated user from request context. Returns nil if no user in context. Usage:

Errors

Complete Example

Authentication Flow

  1. Registration: User submits username/password → Service validates → Repository creates user with hashed password
  2. Login: User submits credentials → Service verifies password → Session created → Cookie set
  3. Protected Request: Request with cookie → Middleware validates session → User injected into context → Handler executes
  4. Logout: Logout request → Service deletes session → Cookie cleared
  5. Password Change: Authenticated user submits old/new password → Service verifies old password → Updates with new hashed password
  6. Account Deletion: Authenticated user requests deletion → Service deletes all sessions → Deletes user → Enqueues cleanup job