session

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Expiration:   24 * time.Hour,
	KeyLookup:    "cookie:session_id",
	KeyGenerator: utils.UUIDv4,
	// contains filtered or unexported fields
}

ConfigDefault is the default config

View Source
var ErrEmptySessionID = errors.New("session id cannot be empty")

ErrEmptySessionID is an error that occurs when the session ID is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Storage interface to store the session data
	// Optional. Default value memory.New()
	Storage fiber.Storage

	// KeyGenerator generates the session key.
	// Optional. Default value utils.UUIDv4
	KeyGenerator func() string

	// KeyLookup is a string in the form of "<source>:<name>" that is used
	// to extract session id from the request.
	// Possible values: "header:<name>", "query:<name>" or "cookie:<name>"
	// Optional. Default value "cookie:session_id".
	KeyLookup string

	// Domain of the cookie.
	// Optional. Default value "".
	CookieDomain string

	// Path of the cookie.
	// Optional. Default value "".
	CookiePath string

	// Value of SameSite cookie.
	// Optional. Default value "Lax".
	CookieSameSite string

	// Allowed session duration
	// Optional. Default value 24 * time.Hour
	Expiration time.Duration

	// Indicates if cookie is secure.
	// Optional. Default value false.
	CookieSecure bool

	// Indicates if cookie is HTTP only.
	// Optional. Default value false.
	CookieHTTPOnly bool

	// Decides whether cookie should last for only the browser sesison.
	// Ignores Expiration if set to true
	// Optional. Default value false.
	CookieSessionOnly bool
	// contains filtered or unexported fields
}

Config defines the config for middleware.

type Session

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

func (*Session) Delete

func (s *Session) Delete(key string)

Delete will delete the value

func (*Session) Destroy

func (s *Session) Destroy() error

Destroy will delete the session from Storage and expire session cookie

func (*Session) Fresh

func (s *Session) Fresh() bool

Fresh is true if the current session is new

func (*Session) Get

func (s *Session) Get(key string) any

Get will return the value

func (*Session) ID

func (s *Session) ID() string

ID returns the session id

func (*Session) Keys

func (s *Session) Keys() []string

Keys will retrieve all keys in current session

func (*Session) Regenerate

func (s *Session) Regenerate() error

Regenerate generates a new session id and delete the old one from Storage

func (*Session) Reset

func (s *Session) Reset() error

Reset generates a new session id, deletes the old one from storage, and resets the associated data

func (*Session) Save

func (s *Session) Save() error

Save will update the storage and client cookie

sess.Save() will save the session data to the storage and update the client cookie, and it will release the session after saving.

It's not safe to use the session after calling Save().

func (*Session) Set

func (s *Session) Set(key string, val any)

Set will update or create a new key value

func (*Session) SetExpiry

func (s *Session) SetExpiry(exp time.Duration)

SetExpiry sets a specific expiration for this session

type Source

type Source string
const (
	SourceCookie   Source = "cookie"
	SourceHeader   Source = "header"
	SourceURLQuery Source = "query"
)

type Store

type Store struct {
	Config
}

func New

func New(config ...Config) *Store

New creates a new session store with the provided configuration.

func (*Store) Delete

func (s *Store) Delete(id string) error

Delete deletes a session by its ID.

func (*Store) Get

func (s *Store) Get(c fiber.Ctx) (*Session, error)

Get retrieves or creates a session for the given context.

func (*Store) RegisterType

func (*Store) RegisterType(i any)

RegisterType registers a custom type for encoding/decoding into any storage provider.

func (*Store) Reset

func (s *Store) Reset() error

Reset deletes all sessions from the storage.

Jump to

Keyboard shortcuts

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