Skip to main content
Platforma’s auth domain provides a complete authentication system with user management, password hashing, session handling, and HTTP middleware for route protection.

Overview

The auth package includes:
  • User registration and login
  • Secure password hashing with bcrypt and salt
  • Session-based authentication
  • Middleware for protecting routes
  • Password change and account deletion
  • Context helpers for accessing the current user

Setting Up Authentication

1

Create database and session domains

2

Create auth repository and service

3

Run migrations

Service Methods

The auth.Service provides methods for user management:

Register User

Default validation rules:
  • Username: 5-20 characters
  • Password: 8-100 characters

Login

Logout

Change Password

Delete Account

Middleware for Route Protection

The AuthenticationMiddleware validates sessions and injects the user into the request context:
The middleware:
  1. Reads the session cookie
  2. Validates the session
  3. Retrieves the user from the database
  4. Injects the user into the context
  5. Returns 401 if authentication fails

Accessing the Current User

Use auth.UserFromContext to get the authenticated user:
The user is automatically injected by the authentication middleware.

HTTP Handlers

The auth package includes built-in HTTP handlers:

Register Handler

Expects JSON:
Returns:
  • 201 Created on success
  • 400 Bad Request if validation fails
  • 500 Internal Server Error on database errors

Login Handler

Expects JSON:
Returns:
  • 200 OK and sets session cookie on success
  • 401 Unauthorized for invalid credentials

Other Handlers

The package also provides:
  • NewLogoutHandler - Deletes session and clears cookie
  • NewChangePasswordHandler - Changes user password (requires auth)
  • NewGetHandler - Returns current user info (requires auth)
  • NewDeleteHandler - Deletes user account (requires auth)
See auth/handler_*.go in the source code for implementation details.

Custom Validation

Provide custom validators when creating the service:

Password Security

Platforma uses bcrypt with salt for secure password storage:
Each user gets a unique salt, and passwords are never stored in plain text.

User Model

The User struct represents a user:

Error Types

The auth package defines these error types:
  • ErrUserNotFound - User doesn’t exist
  • ErrInvalidUsername - Username validation failed
  • ErrInvalidPassword - Password validation failed
  • ErrWrongUserOrPassword - Login failed
  • ErrCurrentPasswordIncorrect - Password change failed
  • ErrShortUsername / ErrLongUsername - Length validation
  • ErrShortPassword / ErrLongPassword - Length validation

Complete Example

Repository Interface

The Repository implements the migrator interface and provides database operations:
Migrations are embedded from auth/migrations/*.sql and automatically applied when you call db.Migrate(ctx).

Best Practices

Use HTTPS

Always serve your application over HTTPS to protect session cookies from interception.

Set cookie flags

Use HttpOnly and SameSite flags on session cookies to prevent XSS and CSRF attacks.

Validate on both sides

Validate input on both client and server side for better UX and security.

Log auth events

Log login attempts, password changes, and account deletions for security auditing.