Under Reviewv0.1.0-alpha
Authentication Module
The Authentication module provides secure user authentication, registration, and session token generation for AxCom.
Overview
- User Registration: Create new user accounts (defaults to the
customerrole if unspecified) with secure validation and password hashing. - Secure Password Hashing: Passwords are encrypted using
bcryptbefore storage. - User Login: Validate user credentials and return an active JWT session.
- Role-Based Tokens: Session tokens embed user roles to authorize operations downstream.
- Input Validation: Enforce validation rules (valid email format, minimum 8-character password with at least one letter and one number).
- Custom Sentinel Errors: Distinguish error conditions clearly (
ErrEmailAlreadyExists,ErrInvalidCredentials,ErrUserNotFound). - Activity Logging: Track request activities, successes, and failures using structured log levels.
Architecture
Auth Handlerreceives credentials and validates payloads.Auth Servicecontains business rules for registration, login, token refresh, password recovery, and logout.UserRepositoryandTokenRepositoryare storage contracts for user and token persistence.JWT Managerhandles signing and verifying access tokens.
Module Structure
| File | Role |
|---|---|
handler.go | HTTP controllers — validates requests, encodes responses |
service.go | Core business logic; exposes the Service interface |
model.go | Data schemas: User, Session |
repository.go | UserRepository storage contract |
errors.go | Domain-specific sentinel errors |
Database Design
What this module needs
- A
UserRepositoryimplementation to persist and retrieve users. - A
TokenRepositoryimplementation for refresh tokens and password reset tokens. - A secure password hashing mechanism (
bcryptor equivalent). - A JWT manager for generating and validating access tokens.
- Structured application errors for invalid credentials, unauthorized access, and duplicate accounts.
- Request validation for email format, password strength, and required fields.
Usage
Handlers rely on the Service interface for all tasks, allowing the service layer to be mocked or replaced easily.
authService := auth.NewAuthService(userRepo, jwtManager)
authHandler := auth.NewAuthHandler(authService)