Overview
Thesession 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
string
required
User ID to associate with the session
string
Generated UUID for the new session
error
Error if session creation fails
GetUserIdFromSessionId
string
required
Session ID to lookup
string
User ID associated with the session
error
Error if session not found
Get
string
required
Session ID to retrieve
*Session
Session object if found
error
Error if session not found
GetByUserId
string
required
User ID to lookup
*Session
Session object if found
error
Error if session not found
DeleteSession
string
required
Session ID to delete
error
Error if deletion fails
DeleteSessionsByUserId
string
required
User ID whose sessions to delete
error
Error if deletion fails
Create
*Session
required
Session object to create
error
Error if creation fails
Repository
session.Repository
Database operations for session persistence.Methods
Get
Retrieves a session from thesessions table by session ID.
Query: SELECT * FROM sessions WHERE id = $1
session/repository.go:36
GetByUserId
Retrieves a session from thesessions table by user ID.
Query: SELECT * FROM sessions WHERE "user" = $1
session/repository.go:45
Create
Inserts a new session into thesessions table.
Query:
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:31Models
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)
IsExpired
bool
true if session is expired, false otherwiseSession Storage
Sessions are stored in a PostgreSQLsessions table with the following schema:
- 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
- Creation: When user logs in,
CreateSessionForUsergenerates a UUID and stores it with 100-day expiration - Validation: On each protected request, middleware calls
GetUserIdFromSessionIdto validate session and retrieve user - Expiration: Sessions automatically expire after 100 days (can be checked with
IsExpired()) - Deletion: Sessions are deleted on logout or account deletion via
DeleteSessionorDeleteSessionsByUserId
Database Interface
The repository requires a database interface with these methods:sqlx.DB from the github.com/jmoiron/sqlx package.