Skip to main content
Platforma’s session domain provides database-backed session management with automatic expiration tracking and user association.

Overview

The session package provides:
  • Session creation and retrieval
  • User association
  • Expiration tracking
  • Database persistence
  • Integration with the auth domain

Session Model

Sessions are represented by the Session struct:
Each session:
  • Has a unique ID (UUID)
  • Is associated with a user ID
  • Tracks creation time
  • Has an expiration timestamp

Checking Expiration

The Session model includes a helper method:

Setting Up Sessions

1

Create database connection

2

Create session domain

The New function creates both the repository and service.
3

Register repository

This enables automatic migration of the sessions table.
4

Run migrations

Service Methods

The session.Service provides methods for session management:

Create Session

Create a new session manually:

Create Session for User

Create a session for a user (automatically sets ID, timestamps):
Default expiration is 100 days from creation:

Get Session

Retrieve a session by ID:

Get Session by User ID

Retrieve a session by user ID:

Get User ID from Session

Extract the user ID from a session:
This is commonly used by the auth middleware to identify the current user.

Delete Session

Delete a specific session:

Delete All User Sessions

Delete all sessions for a user (useful for logout from all devices):

Integration with Auth

The session service is designed to work with the auth domain. The auth service uses it to manage authentication sessions:
The auth service uses these session methods:
  • CreateSessionForUser - When logging in
  • GetUserIdFromSessionId - In authentication middleware
  • DeleteSession - When logging out
  • DeleteSessionsByUserId - When deleting a user account

Domain Structure

The session.Domain contains both repository and service:
Access them via:

Repository Implementation

The repository handles database operations:
It implements the migrator interface:
Migrations are embedded from session/migrations/*.sql.

Database Schema

The sessions table structure:
Note: The user column is quoted because user is a reserved word in PostgreSQL.

Custom Expiration

To customize session expiration, create sessions manually:
Or modify the service method (requires forking or wrapping).

Session Cleanup

Expired sessions are not automatically deleted from the database. You should periodically clean them up:

Session Validation

Always check session expiration when validating:

Best Practices

Set reasonable expiration

Balance security and UX: short expiration (hours/days) for sensitive apps, longer (weeks/months) for convenience.

Clean up expired sessions

Schedule periodic cleanup to prevent the sessions table from growing indefinitely.

Invalidate on logout

Always delete sessions when users explicitly log out.

Rotate session IDs

Consider rotating session IDs after privileged operations to prevent session fixation attacks.

Complete Example