session

package
v0.82.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSession       = errors.New("no session")
	ErrDeletingSession = errors.New("error deleting session")
)

Functions

func SetSessionMetadataInContext added in v0.76.0

func SetSessionMetadataInContext(ctx context.Context, metadata SessionMetadata) context.Context

SetSessionMetadataInContext sets session metadata in context It accepts a SessionMetadata struct but stores it as a map with the same structure to avoid layer violations in repositories

Types

type Repository

type Repository interface {
	Set(ctx context.Context, session *Session) error
	Get(ctx context.Context, id uuid.UUID) (*Session, error)
	Delete(ctx context.Context, id uuid.UUID) error
	DeleteExpiredSessions(ctx context.Context) error
	UpdateValidity(ctx context.Context, id uuid.UUID, validity time.Duration) error
	List(ctx context.Context, userID string) ([]*Session, error)
	UpdateSessionMetadata(ctx context.Context, id uuid.UUID, metadata SessionMetadata, updatedAt time.Time) error
}

type Service

type Service struct {
	Now func() time.Time
	// contains filtered or unexported fields
}

func NewService

func NewService(logger log.Logger, repo Repository, validity time.Duration) *Service

func (Service) Close

func (s Service) Close() error

func (Service) Create

func (s Service) Create(ctx context.Context, userID string, metadata SessionMetadata) (*Session, error)

func (Service) Delete

func (s Service) Delete(ctx context.Context, sessionID uuid.UUID) error

Delete marks a session as deleted without removing it from the database

func (Service) ExtractFromContext

func (s Service) ExtractFromContext(ctx context.Context) (*Session, error)

func (Service) Get added in v0.75.0

func (s Service) Get(ctx context.Context, sessionID uuid.UUID) (*Session, error)

func (Service) GetByID added in v0.75.0

func (s Service) GetByID(ctx context.Context, sessionID uuid.UUID) (*Session, error)

GetSession retrieves a session by its ID

func (Service) InitSessions

func (s Service) InitSessions(ctx context.Context) error

InitSessions Initiates CronJob to delete expired sessions from the database

func (Service) List added in v0.75.0

func (s Service) List(ctx context.Context, userID string) ([]*Session, error)

ListSessions returns all active sessions for a user

func (Service) Ping added in v0.75.0

func (s Service) Ping(ctx context.Context, sessionID uuid.UUID, metadata SessionMetadata) error

func (Service) Refresh

func (s Service) Refresh(ctx context.Context, sessionID uuid.UUID) error

Refresh extends validity of session

type Session

type Session struct {
	ID uuid.UUID

	// UserID is a unique identifier for logged in users
	UserID string

	// AuthenticatedAt is set when a user is successfully authn
	AuthenticatedAt time.Time

	// ExpiresAt is ideally now() + lifespan of session, e.g. 7 days
	ExpiresAt time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time // Soft delete timestamp (nil = not deleted)

	Metadata SessionMetadata
}

Session is created on successful authentication of users

func (Session) IsValid

func (s Session) IsValid(now time.Time) bool

type SessionMetadata added in v0.75.0

type SessionMetadata struct {
	IpAddress string
	Location  struct {
		Country   string
		City      string
		Latitude  string
		Longitude string
	}
	OperatingSystem string
	Browser         string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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