session

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CookieStore

type CookieStore struct {
	Key []byte
	// contains filtered or unexported fields
}

CookieStore stores session data encrypted in the cookie value itself. Requires a 32-byte key (AES-256-GCM).

func NewCookieStore

func NewCookieStore(key []byte) (*CookieStore, error)

NewCookieStore creates a cookie-based session store with the given AES-256 key.

func (*CookieStore) Destroy

func (s *CookieStore) Destroy(id string) error

func (*CookieStore) GC

func (s *CookieStore) GC(maxLifetime time.Duration) error

func (*CookieStore) Read

func (s *CookieStore) Read(id string) (map[string]interface{}, error)

func (*CookieStore) Write

func (s *CookieStore) Write(id string, data map[string]interface{}, lifetime time.Duration) error

type DBStore

type DBStore struct {
	DB *gorm.DB
}

DBStore stores sessions in a database table via GORM.

func (*DBStore) Destroy

func (s *DBStore) Destroy(id string) error

func (*DBStore) GC

func (s *DBStore) GC(maxLifetime time.Duration) error

func (*DBStore) Read

func (s *DBStore) Read(id string) (map[string]interface{}, error)

func (*DBStore) Write

func (s *DBStore) Write(id string, data map[string]interface{}, lifetime time.Duration) error

type FileStore

type FileStore struct {
	Path string
}

FileStore stores one JSON file per session in the configured directory.

func (*FileStore) Destroy

func (s *FileStore) Destroy(id string) error

func (*FileStore) GC

func (s *FileStore) GC(maxLifetime time.Duration) error

func (*FileStore) Read

func (s *FileStore) Read(id string) (map[string]interface{}, error)

func (*FileStore) Write

func (s *FileStore) Write(id string, data map[string]interface{}, lifetime time.Duration) error

type Manager

type Manager struct {
	Store      Store
	CookieName string
	Lifetime   time.Duration
	Path       string
	Domain     string
	Secure     bool
	HTTPOnly   bool
	SameSite   http.SameSite
}

Manager ties together the store, cookie handling, and ID generation.

func NewManager

func NewManager(store Store) *Manager

NewManager creates a session manager from environment config.

func (*Manager) Destroy

func (m *Manager) Destroy(w http.ResponseWriter, id string) error

Destroy removes a session and clears the cookie.

func (*Manager) Flash

func (m *Manager) Flash(data map[string]interface{}, key string, value interface{})

Flash writes a flash message that will be available on the next request only.

func (*Manager) FlashErrors

func (m *Manager) FlashErrors(data map[string]interface{}, errors map[string][]string)

FlashErrors stores validation errors for the next request.

func (*Manager) FlashOldInput

func (m *Manager) FlashOldInput(data map[string]interface{}, input map[string]string)

FlashOldInput stores form input for re-populating after validation failure.

func (*Manager) GetFlash

func (m *Manager) GetFlash(data map[string]interface{}, key string) (interface{}, bool)

GetFlash reads and removes a flash message.

func (*Manager) Save

func (m *Manager) Save(w http.ResponseWriter, id string, data map[string]interface{}) error

Save persists session data and writes the session cookie.

func (*Manager) Start

func (m *Manager) Start(r *http.Request) (string, map[string]interface{}, error)

Start retrieves or creates a session for the request.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore stores sessions in memory. For development and testing only.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory session store.

func (*MemoryStore) Destroy

func (s *MemoryStore) Destroy(id string) error

func (*MemoryStore) GC

func (s *MemoryStore) GC(maxLifetime time.Duration) error

func (*MemoryStore) Read

func (s *MemoryStore) Read(id string) (map[string]interface{}, error)

func (*MemoryStore) Write

func (s *MemoryStore) Write(id string, data map[string]interface{}, lifetime time.Duration) error

type RedisStore

type RedisStore struct {
	Client *redis.Client
	Prefix string
}

RedisStore stores sessions in Redis.

func NewRedisStore

func NewRedisStore(client *redis.Client, prefix string) *RedisStore

NewRedisStore creates a Redis-backed session store from the given client.

func (*RedisStore) Destroy

func (s *RedisStore) Destroy(id string) error

func (*RedisStore) GC

func (s *RedisStore) GC(maxLifetime time.Duration) error

func (*RedisStore) Read

func (s *RedisStore) Read(id string) (map[string]interface{}, error)

func (*RedisStore) Write

func (s *RedisStore) Write(id string, data map[string]interface{}, lifetime time.Duration) error

type SessionRecord

type SessionRecord struct {
	ID         string    `gorm:"primaryKey;size:255"`
	Data       string    `gorm:"type:text;not null"`
	UserID     *uint     `gorm:"index"`
	IPAddress  *string   `gorm:"size:45"`
	UserAgent  *string   `gorm:"type:text"`
	LastActive time.Time `gorm:"autoUpdateTime"`
	CreatedAt  time.Time
}

SessionRecord represents a row in the sessions table.

func (SessionRecord) TableName

func (SessionRecord) TableName() string

TableName returns the database table name.

type Store

type Store interface {
	// Read returns session data for the given ID, or empty map if not found.
	Read(id string) (map[string]interface{}, error)

	// Write persists session data with the given ID and lifetime.
	Write(id string, data map[string]interface{}, lifetime time.Duration) error

	// Destroy removes a session by ID.
	Destroy(id string) error

	// GC removes sessions older than the given max lifetime.
	GC(maxLifetime time.Duration) error
}

Store defines the contract every session backend must satisfy.

func NewStore

func NewStore(db *gorm.DB) (Store, error)

NewStore resolves the correct session backend from SESSION_DRIVER.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL