Overview
Theauth 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)
string
required
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
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- Username: 5-20 characters
- Password: 8-100 characters
Login
string
required
Username to authenticate
string
required
Password to verify
string
New session ID on success
error
Returns
ErrWrongUserOrPassword if credentials invalidLogout
string
required
Session ID to delete
Get User
Change Password
string
required
Current password for verification
string
required
New password (validated by passwordValidator)
error
Returns
ErrCurrentPasswordIncorrect, ErrInvalidPassword, or update errorDelete User
Repository
auth.Repository
Database operations for user persistence.Methods
Middleware
auth.AuthenticationMiddleware
HTTP middleware that validates sessions and injects users into request context.- Reads session cookie from request
- Validates session with auth storage
- Retrieves user from session
- Injects user into request context
- Returns 401 Unauthorized if session invalid
HTTP Handlers
All handlers are pre-configured inDomain.HandleGroup with these routes:
POST /register
Request:201 Created- User created successfully400 Bad Request- Invalid username or password500 Internal Server Error- Creation failed
POST /login
Request:200 OK- Sets session cookie401 Unauthorized- Invalid credentials500 Internal Server Error- Session creation failed
POST /logout
Response:200 OK- Clears session cookie500 Internal Server Error- Session deletion failed
GET /me
Response:200 OK- Returns user info401 Unauthorized- No valid session500 Internal Server Error- Retrieval failed
POST /change-password
Request:200 OK- Password changed400 Bad Request- Invalid new password401 Unauthorized- Current password incorrect500 Internal Server Error- Update failed
DELETE /me
Response:200 OK- User deleted401 Unauthorized- No valid session500 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
nil if no user in context.
Usage:
Errors
Complete Example
Authentication Flow
- Registration: User submits username/password → Service validates → Repository creates user with hashed password
- Login: User submits credentials → Service verifies password → Session created → Cookie set
- Protected Request: Request with cookie → Middleware validates session → User injected into context → Handler executes
- Logout: Logout request → Service deletes session → Cookie cleared
- Password Change: Authenticated user submits old/new password → Service verifies old password → Updates with new hashed password
- Account Deletion: Authenticated user requests deletion → Service deletes all sessions → Deletes user → Enqueues cleanup job