sessions

package
v0.0.0-...-60192f8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package sessions provides a cookie based session manager. It's heavily based on gorilla session but with a structured session payload that can be serialized to json.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaxAge

func MaxAge(val int) func(s *Handler)

MaxAge sets the session handler's max age.

func Path

func Path(val string) func(s *Handler)

Path sets the session handler's path.

Types

type FlashMessage

type FlashMessage struct {
	Type    string `json:"t"`
	Message string `json:"m"`
}

FlashMessage is a message stored in the session.

type Handler

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

Handler is the session handler with its global options.

func NewHandler

func NewHandler(name string, hashKey, blockKey []byte, options ...func(s *Handler)) *Handler

NewHandler creates a session handler.

func (*Handler) New

func (s *Handler) New(r *http.Request) (*Session, error)

New creates or retrieves a session.

func (*Handler) Save

func (s *Handler) Save(r *http.Request, w http.ResponseWriter, session *Session) error

Save encodes the session's data and encloses it in a cookie that's written on the HTTP response.

type Payload

type Payload struct {
	Seed    int            `json:"s"`
	User    int            `json:"u"`
	Flashes []FlashMessage `json:"f"`
}

Payload contains session values.

type Session

type Session struct {
	Payload *Payload
	IsNew   bool
	MaxAge  int
	// contains filtered or unexported fields
}

Session is a unique session.

func (*Session) AddFlash

func (s *Session) AddFlash(typ, msg string)

AddFlash add a new flash message to the session.

func (*Session) Flashes

func (s *Session) Flashes() []FlashMessage

Flashes retrieves the flash messages from the session and flushes them. The session is not saved, it's up to the code (middleware) that calls this function to save the session.

func (*Session) Save

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

Save sends the session's cookie to the HTTP response.

Jump to

Keyboard shortcuts

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