Session
The Session middleware is used for the Fiber framework for session management.
Note: This middleware uses our Storage package to support various databases through a unified interface. The default configuration of this middleware stores data in memory. For examples of other databases, please see the following examples.
Signature
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
func (s *Session) Keys() []string
Note: The storage of "interface{}" values is limited to built-in Go types.
Example
Import the middleware package for the Fiber Web framework.
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
)
After initializing the Fiber application, you can use the following features:
// Initialize with default configuration
// This stores all your application sessions
store := session.New()
app.Get("/", func(c *fiber.Ctx) error {
// Get the 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")
// Get all keys
keys := sess.Keys()
// Delete key
sess.Delete("name")
// Destroy the session
if err := sess.Destroy(); err != nil {
panic(err)
}
// Set a specific expiration time for this session
sess.SetExpiry(time.Second * 2)
// Save the session
if err := sess.Save(); err != nil {
panic(err)
}
return c.SendString(fmt.Sprintf("Welcome %v", name))
})
Configuration
Property | Type | Description | Default |
---|---|---|---|
Expiration | time.Duration |
Allowed session duration | 24 * time.Hour |
Storage | fiber.Storage |
Storage interface to store session data | memory.New() |
KeyLookup | string |
KeyLookup is a string like ": " to extract the session ID from the request |
"cookie:session_id" |
CookieDomain | string |
Domain of the cookie | "" |
CookiePath | string |
Path of the cookie | "" |
CookieSecure | bool |
Whether the cookie is secure | false |
CookieHTTPOnly | bool |
Whether the cookie is HTTP only | false |
CookieSameSite | string |
Value of SameSite cookie | "Lax" |
CookieSessionOnly | bool |
Determines if the cookie is valid only for browser sessions. If set to true, it ignores the value of Expiration | false |
KeyGenerator | func() string |
KeyGenerator generates session key | utils.UUIDv4 |
CookieName (deprecated) | string |
Deprecated: use KeyLookup. Name of the session | "" |
Default Configuration
var ConfigDefault = Config{
Expiration: 24 * time.Hour,
KeyLookup: "cookie:session_id",
KeyGenerator: utils.UUIDv4,
source: "cookie",
sessionName: "session_id",
}
Constants
const (
SourceCookie Source = "cookie"
SourceHeader Source = "header"
SourceURLQuery Source = "query"
)
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,
})