sessions

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sessions stores per-user data in an encrypted cookie:

ternApp(kit.WithPlugins(sessions.New(os.Getenv("SESSION_SECRET"))))

func Load(ctx *kit.Ctx) (Data, error) {
	s := sessions.Get(ctx)
	user := s.GetString("user")
	notice := s.Flash("notice") // read once
	...
}

Values are JSON-encoded and sealed with AES-GCM, so users can neither read nor change them. Keep sessions small: cookies are limited to about 4 KB. Changes are saved automatically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Plugin)

Option configures the plugin.

func CookieName

func CookieName(name string) Option

CookieName changes the cookie from "session".

func MaxAge

func MaxAge(d time.Duration) Option

MaxAge sets how long sessions last. The default is 30 days.

func OldSecrets

func OldSecrets(secrets ...string) Option

OldSecrets keeps sessions sealed with previous secrets readable, so the secret can be rotated without signing everyone out.

type Plugin

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

Plugin loads and saves sessions. Create it with New.

func New

func New(secret string, opts ...Option) *Plugin

New returns the sessions plugin. secret must be at least 32 characters and stay the same across restarts and servers.

func (*Plugin) Name

func (*Plugin) Name() string

Name implements kit.Plugin.

func (*Plugin) Setup

func (p *Plugin) Setup(s *kit.Setup) error

Setup implements kit.Plugin.

type Session

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

Session is one user's session data.

func Get

func Get(ctx *kit.Ctx) *Session

Get returns the request's session. It panics if the sessions plugin is not installed, which is a programming error.

func (*Session) AddFlash

func (s *Session) AddFlash(key, message string) error

AddFlash stores a message to show once, typically after a redirect.

func (*Session) Clear

func (s *Session) Clear()

Clear removes everything, for example when signing out.

func (*Session) Delete

func (s *Session) Delete(key string) error

Delete removes key.

func (*Session) Flash

func (s *Session) Flash(key string) string

Flash returns a message stored with AddFlash and removes it.

func (*Session) GetString

func (s *Session) GetString(key string) string

GetString returns the string stored at key, or "".

func (*Session) Set

func (s *Session) Set(key string, v any) error

Set stores v (any JSON-encodable value) at key.

func (*Session) Value

func (s *Session) Value(key string, dst any) bool

Value decodes the value stored at key into dst and reports whether it was set.

Jump to

Keyboard shortcuts

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