serversession

package
v0.0.0-...-1d36bab Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package session implements a server-side session management strategy for managing user sessions.

The implementation uses a PostgreSQL database to store session data.

Index

Constants

View Source
const (
	DefaultCookieName = "usid"
	DefaultExpiresIn  = time.Hour * 12
)

Variables

This section is empty.

Functions

func New

func New[U, S any](
	pool *pgxpool.Pool,
	config *Config[U, S],
) shieldsession.Authenticator[U, S]

New creates a new session authenticator.

The session authenticator uses a DB to store sessions and a cookie to store the session ID.

func WithHooker

func WithHooker[U, S any](h Hooker[U, S]) func(*Config[U, S])

WithHooker sets a session hooker for a given config.

Types

type Config

type Config[U, S any] struct {
	Logger *slog.Logger

	Hooker Hooker[U, S]

	CookieName string        // optional (default: "usid")
	ExpiresIn  time.Duration // optional (default: 12h)
}

func NewConfig

func NewConfig[U, S any](opts ...func(*Config[U, S])) *Config[U, S]

NewConfig creates a new session configuration.

type Hooker

type Hooker[U, S any] interface {
	OnSessionIssue(
		context.Context,
		shield.User[U],
		shieldsession.Session[S],
		pgx.Tx,
	) (shieldsession.Session[S], error)

	// OnSessionAuthenticate allows to hook into the session authentication process.
	OnSessionAuthenticate(
		context.Context,
		shieldsession.Session[S],
		pgx.Tx,
	) (shieldsession.Session[S], error)

	// OnLogout allows to hook into the session logout process.
	OnLogout(
		ctx context.Context,
		userID, sessionID typeid.TypeID,
		tx pgx.Tx,
	) error

	// OnExpireSessions allows to hook into session expiration process.
	OnExpireSessions(
		ctx context.Context,
		userID, sessionID typeid.TypeID,
	) error
}

type LogoutHandler

type LogoutHandler[U, S any] struct {
	// contains filtered or unexported fields
}

LogoutHandler is a handler that logs out the user and deletes the session.

func NewLogoutHandler

func NewLogoutHandler[U, S any](
	pool *pgxpool.Pool,
	config *Config[U, S],
) *LogoutHandler[U, S]

func (*LogoutHandler[U, S]) Logout

func (h *LogoutHandler[U, S]) Logout(
	w http.ResponseWriter,
	r *http.Request,
) error

Logout logs out the user and deletes the session.

Jump to

Keyboard shortcuts

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