session

package
v1.14.7-0...-1b5f2a9 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: MIT Imports: 8 Imported by: 0

README

Session

Session middleware for Fiber.

NOTE: This middleware uses our Storage package to support various databases through a single interface. The default configuration for this middleware saves data to memory, see the examples below for other databases.

Table of Contents

Signatures

func New(config ...Config) *Store
func (s *Store) RegisterType(i interface{})
func (s *Store) Get(c *fiber.Ctx) (*Session, error)
func (s *Store) Reset() error

func (s *Session) Get(key string) interface{}
func (s *Session) Set(key string, val interface{})
func (s *Session) Delete(key string)
func (s *Session) Destroy() error
func (s *Session) Regenerate() error
func (s *Session) Save() error
func (s *Session) Fresh() bool
func (s *Session) ID() string

Storing interface{} values are limited to built-ins Go types

Examples

Import the middleware package that is part of the Fiber web framework

import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/session"
)

Then create a Fiber app with app := fiber.New().

Default Configuration
// This stores all of your app's sessions
// Default middleware config
store := session.New()

// This panic will be catch by the middleware
app.Get("/", func(c *fiber.Ctx) error {
	// get session from storage
	sess, err := store.Get(c)
	if err != nil {
		panic(err)
	}

	// Get value
	name := sess.Get("name")

	// Set key/value
	sess.Set("name", "john")

	// Delete key
	sess.Delete("name")

	// Destry session
	if err := sess.Destroy(); err != nil {
		panic(err)
	}

	// save session
	if err := sess.Save(); err != nil {
		panic(err)
	}

	return c.SendString(fmt.Sprintf("Welcome %v", name))
})
Custom Storage/Database

You can use any storage from our storage package.

storage := sqlite3.New() // From github.com/gofiber/storage/sqlite3
store := session.New(session.Config{
	Storage: storage,
})

To use the the store, see the above example.

Config

// Config defines the config for middleware.
type Config struct {
	// Allowed session duration
	// Optional. Default value 24 * time.Hour
	Expiration time.Duration

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

	// Name of the session cookie. This cookie will store session key.
	// Optional. Default value "session_id".
	CookieName string

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

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

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

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

	// Indicates if CSRF cookie is HTTP only.
	// Optional. Default value false.
	CookieSameSite string

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

Default Config

var ConfigDefault = Config{
	Expiration:   24 * time.Hour,
	CookieName:   "session_id",
	KeyGenerator: utils.UUID,
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Expiration:   24 * time.Hour,
	CookieName:   "session_id",
	KeyGenerator: utils.UUIDv4,
}

ConfigDefault is the default config

Functions

This section is empty.

Types

type Config

type Config struct {
	// Allowed session duration
	// Optional. Default value 24 * time.Hour
	Expiration time.Duration

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

	// Name of the session cookie. This cookie will store session key.
	// Optional. Default value "session_id".
	CookieName string

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

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

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

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

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

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

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) interface{}

Get will return the value

func (*Session) ID

func (s *Session) ID() string

ID returns the session id

func (*Session) Regenerate

func (s *Session) Regenerate() error

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

func (*Session) Save

func (s *Session) Save() error

Save will update the storage and client cookie

func (*Session) Set

func (s *Session) Set(key string, val interface{})

Set will update or create a new key value

type Store

type Store struct {
	Config
}

func New

func New(config ...Config) *Store

func (*Store) Get

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

Get will get/create a session

func (*Store) RegisterType

func (s *Store) RegisterType(i interface{})

RegisterType will allow you to encode/decode custom types into any Storage provider

func (*Store) Reset

func (s *Store) Reset() error

Reset will delete all session from the storage

Jump to

Keyboard shortcuts

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