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

  1. func New(config ...Config) *Store
  2. func (s *Store) RegisterType(i interface{})
  3. func (s *Store) Get(c *fiber.Ctx) (*Session, error)
  4. func (s *Store) Reset() error
  5. func (s *Session) Get(key string) interface{}
  6. func (s *Session) Set(key string, val interface{})
  7. func (s *Session) Delete(key string)
  8. func (s *Session) Destroy() error
  9. func (s *Session) Regenerate() error
  10. func (s *Session) Save() error
  11. func (s *Session) Fresh() bool
  12. func (s *Session) ID() string
  13. func (s *Session) Keys() []string

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

Examples

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

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/session"
  4. )

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

Default Configuration

  1. // This stores all of your app's sessions
  2. // Default middleware config
  3. store := session.New()
  4. // This panic will be catch by the middleware
  5. app.Get("/", func(c *fiber.Ctx) error {
  6. // Get session from storage
  7. sess, err := store.Get(c)
  8. if err != nil {
  9. panic(err)
  10. }
  11. // Get value
  12. name := sess.Get("name")
  13. // Set key/value
  14. sess.Set("name", "john")
  15. // Get all Keys
  16. keys := sess.Keys()
  17. // Delete key
  18. sess.Delete("name")
  19. // Destroy session
  20. if err := sess.Destroy(); err != nil {
  21. panic(err)
  22. }
  23. // Save session
  24. if err := sess.Save(); err != nil {
  25. panic(err)
  26. }
  27. return c.SendString(fmt.Sprintf("Welcome %v", name))
  28. })

Custom Storage/Database

You can use any storage from our storage package.

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

To use the the store, see the above example.

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Allowed session duration
  4. // Optional. Default value 24 * time.Hour
  5. Expiration time.Duration
  6. // Storage interface to store the session data
  7. // Optional. Default value memory.New()
  8. Storage fiber.Storage
  9. // Name of the session cookie. This cookie will store session key.
  10. // Optional. Default value "session_id".
  11. CookieName string
  12. // Domain of the CSRF cookie.
  13. // Optional. Default value "".
  14. CookieDomain string
  15. // Path of the CSRF cookie.
  16. // Optional. Default value "".
  17. CookiePath string
  18. // Indicates if CSRF cookie is secure.
  19. // Optional. Default value false.
  20. CookieSecure bool
  21. // Indicates if CSRF cookie is HTTP only.
  22. // Optional. Default value false.
  23. CookieHTTPOnly bool
  24. // Indicates if CSRF cookie is HTTP only.
  25. // Optional. Default value false.
  26. CookieSameSite string
  27. // KeyGenerator generates the session key.
  28. // Optional. Default value utils.UUID
  29. KeyGenerator func() string
  30. }

Default Config

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