Skip to main content

Overview

The session package provides session management functionality for authentication systems. It handles session creation, retrieval, and deletion with database persistence.

Domain

session.Domain

The main domain aggregate for session management.
*Repository
Database repository for session persistence
*Service
Business logic service for session operations

Constructor

db
required
Database interface (sqlx-compatible)
*Domain
Configured session domain with repository and service

Service

session.Service

Core business logic for session management.

Methods

CreateSessionForUser

Creates a new session for a user with a 100-day expiration.
string
required
User ID to associate with the session
string
Generated UUID for the new session
error
Error if session creation fails
session/service.go:46

GetUserIdFromSessionId

Retrieves the user ID associated with a session.
string
required
Session ID to lookup
string
User ID associated with the session
error
Error if session not found
session/service.go:37

Get

Retrieves a session by ID.
string
required
Session ID to retrieve
*Session
Session object if found
error
Error if session not found
session/service.go:25

GetByUserId

Retrieves a session by user ID.
string
required
User ID to lookup
*Session
Session object if found
error
Error if session not found
session/service.go:29

DeleteSession

Deletes a session by ID.
string
required
Session ID to delete
error
Error if deletion fails
session/service.go:33

DeleteSessionsByUserId

Deletes all sessions for a user (used during account deletion).
string
required
User ID whose sessions to delete
error
Error if deletion fails
session/service.go:62

Create

Creates a session with custom parameters.
*Session
required
Session object to create
error
Error if creation fails
session/service.go:21

Repository

session.Repository

Database operations for session persistence.

Methods

Get

Retrieves a session from the sessions table by session ID. Query: SELECT * FROM sessions WHERE id = $1 session/repository.go:36

GetByUserId

Retrieves a session from the sessions table by user ID. Query: SELECT * FROM sessions WHERE "user" = $1 session/repository.go:45

Create

Inserts a new session into the sessions table. Query:
session/repository.go:54

Delete

Deletes a session by ID. Query: DELETE FROM sessions WHERE id = $1 session/repository.go:66

DeleteByUserId

Deletes all sessions for a user. Query: DELETE FROM sessions WHERE "user" = $1 session/repository.go:77

Migrations

Returns embedded SQL migrations for the sessions table. session/repository.go:31

Models

Session

string
UUID primary key for the session
string
Foreign key to user ID
time.Time
Timestamp when session was created
time.Time
Timestamp when session expires (default: 100 days from creation)
session/model.go:5

IsExpired

Checks if the session has expired by comparing the expiration time with the current time.
bool
true if session is expired, false otherwise
session/model.go:12

Session Storage

Sessions are stored in a PostgreSQL sessions table with the following schema:
Storage characteristics:
  • Session IDs are UUIDs generated with uuid.NewString()
  • Sessions expire after 100 days by default
  • Sessions are deleted on user logout or account deletion
  • Multiple sessions per user are supported

Usage Example

Integration with Auth

The session package is designed to work seamlessly with the auth package:

Session Lifecycle

  1. Creation: When user logs in, CreateSessionForUser generates a UUID and stores it with 100-day expiration
  2. Validation: On each protected request, middleware calls GetUserIdFromSessionId to validate session and retrieve user
  3. Expiration: Sessions automatically expire after 100 days (can be checked with IsExpired())
  4. Deletion: Sessions are deleted on logout or account deletion via DeleteSession or DeleteSessionsByUserId

Database Interface

The repository requires a database interface with these methods:
This interface is compatible with sqlx.DB from the github.com/jmoiron/sqlx package.