sessions

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Sessions

HTTP session cookie management for Go. It allows you to set both data that persists between requests (session data), and data that persists until the next request (flash data).

Unlike typical session libraries for Go, sessions uses the request's context for storage within the same request liftime, allowing you to access the session between multiple handlers or HTTP middleware, as well as within test cases that do not use an HTTP server.

Usage

Install the package with go get:

$ go get github.com/bentranter/sessions

Then you can set session and flash data:

// TODO

Documentation

Overview

Package sessions implements HTTP sessions.

(Description TODO)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomKey

func GenerateRandomKey(length int) []byte

GenerateRandomKey creates a random key with the given length in bytes. On failure, returns nil.

Note that keys created using `GenerateRandomKey()` are not automatically persisted. New keys will be created when the application is restarted, and previously issued cookies will not be able to be decoded.

Callers should explicitly check for the possibility of a nil return, treat it as a failure of the system random number generator, and not continue.

This function is an alias of securecookie.GenerateRandomKey, and is provided as a convenience method to avoid the additional import of the securecookie library.

Types

type Options

type Options struct {
	// The name of the cookie (default is "_session").
	Name string

	// MaxAge of the cookie before expiry (default is 365 days). Set it to
	// -1 for no expiry.
	MaxAge int
}

Options to customize the behaviour of the session.

type Session

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

A Session manages setting and getting data from the cookie that stores the session data.

func New

func New(secret []byte, opts ...Options) *Session

New creates a new session manager with the given key.

func (*Session) Delete

func (s *Session) Delete(w http.ResponseWriter, r *http.Request, key string) interface{}

Delete deletes and returns the session value with the given key.

func (*Session) Flash

func (s *Session) Flash(w http.ResponseWriter, r *http.Request, key string, value interface{})

Flash sets a flash message on a request.

func (*Session) Flashes

func (s *Session) Flashes(w http.ResponseWriter, r *http.Request) map[string]interface{}

Flashes returns all flash messages, clearing all saved flashes.

func (*Session) Get

func (s *Session) Get(r *http.Request, key string) interface{}

Session creates a new session from the given HTTP request. If the request already has a cookie with an associated session, the session data is created from the cookie. If not, a new session is created.

func (*Session) List

func (s *Session) List(r *http.Request) map[string]interface{}

List returns all key value pairs of session data from the given request.

func (*Session) Reset

func (s *Session) Reset(w http.ResponseWriter, r *http.Request)

Reset resets the session, deleting all values.

func (*Session) Set

func (s *Session) Set(w http.ResponseWriter, r *http.Request, key string, value interface{})

Set sets or updates the given value on the session.

Jump to

Keyboard shortcuts

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