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
​
6
func (s *Session) Get(key string) interface{}
7
func (s *Session) Set(key string, val interface{})
8
func (s *Session) Delete(key string)
9
func (s *Session) Destroy() error
10
func (s *Session) Regenerate() error
11
func (s *Session) Save() error
12
func (s *Session) Fresh() bool
13
func (s *Session) ID() string
14
func (s *Session) Keys() []string
Copied!
⚠ 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
)
Copied!
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
​
5
// This panic will be catch by the middleware
6
app.Get("/", func(c *fiber.Ctx) error {
7
// Get session from storage
8
sess, err := store.Get(c)
9
if err != nil {
10
panic(err)
11
}
12
​
13
// Get value
14
name := sess.Get("name")
15
​
16
// Set key/value
17
sess.Set("name", "john")
18
​
19
// Get all Keys
20
keys := sess.Keys()
21
​
22
// Delete key
23
sess.Delete("name")
24
​
25
// Destroy session
26
if err := sess.Destroy(); err != nil {
27
panic(err)
28
}
29
​
30
// Save session
31
if err := sess.Save(); err != nil {
32
panic(err)
33
}
34
​
35
return c.SendString(fmt.Sprintf("Welcome %v", name))
36
})
Copied!

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
})
Copied!
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
​
7
// Storage interface to store the session data
8
// Optional. Default value memory.New()
9
Storage fiber.Storage
10
​
11
// Name of the session cookie. This cookie will store session key.
12
// Optional. Default value "session_id".
13
CookieName string
14
​
15
// Domain of the CSRF cookie.
16
// Optional. Default value "".
17
CookieDomain string
18
​
19
// Path of the CSRF cookie.
20
// Optional. Default value "".
21
CookiePath string
22
​
23
// Indicates if CSRF cookie is secure.
24
// Optional. Default value false.
25
CookieSecure bool
26
​
27
// Indicates if CSRF cookie is HTTP only.
28
// Optional. Default value false.
29
CookieHTTPOnly bool
30
​
31
// Indicates if CSRF cookie is HTTP only.
32
// Optional. Default value false.
33
CookieSameSite string
34
​
35
// KeyGenerator generates the session key.
36
// Optional. Default value utils.UUID
37
KeyGenerator func() string
38
}
Copied!

Default Config

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