github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/session.md (about) 1 --- 2 id: session 3 title: Session 4 --- 5 6 Session middleware for [Fiber](https://github.com/gofiber/fiber). 7 8 :::note 9 This middleware uses our [Storage](https://github.com/gofiber/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. 10 ::: 11 12 ## Signatures 13 14 ```go 15 func New(config ...Config) *Store 16 func (s *Store) RegisterType(i interface{}) 17 func (s *Store) Get(c *fiber.Ctx) (*Session, error) 18 func (s *Store) Reset() error 19 20 func (s *Session) Get(key string) interface{} 21 func (s *Session) Set(key string, val interface{}) 22 func (s *Session) Delete(key string) 23 func (s *Session) Destroy() error 24 func (s *Session) Regenerate() error 25 func (s *Session) Save() error 26 func (s *Session) Fresh() bool 27 func (s *Session) ID() string 28 func (s *Session) Keys() []string 29 ``` 30 31 :::caution 32 Storing `interface{}` values are limited to built-ins Go types. 33 ::: 34 35 ## Examples 36 Import the middleware package that is part of the Fiber web framework 37 ```go 38 import ( 39 "github.com/gofiber/fiber/v2" 40 "github.com/gofiber/fiber/v2/middleware/session" 41 ) 42 ``` 43 44 After you initiate your Fiber app, you can use the following possibilities: 45 46 ```go 47 // Initialize default config 48 // This stores all of your app's sessions 49 store := session.New() 50 51 app.Get("/", func(c *fiber.Ctx) error { 52 // Get session from storage 53 sess, err := store.Get(c) 54 if err != nil { 55 panic(err) 56 } 57 58 // Get value 59 name := sess.Get("name") 60 61 // Set key/value 62 sess.Set("name", "john") 63 64 // Get all Keys 65 keys := sess.Keys() 66 67 // Delete key 68 sess.Delete("name") 69 70 // Destroy session 71 if err := sess.Destroy(); err != nil { 72 panic(err) 73 } 74 75 // Sets a specific expiration for this session 76 sess.SetExpiry(time.Second * 2) 77 78 // Save session 79 if err := sess.Save(); err != nil { 80 panic(err) 81 } 82 83 return c.SendString(fmt.Sprintf("Welcome %v", name)) 84 }) 85 ``` 86 87 ## Config 88 89 ```go 90 // Config defines the config for middleware. 91 type Config struct { 92 // Allowed session duration 93 // Optional. Default value 24 * time.Hour 94 Expiration time.Duration 95 96 // Storage interface to store the session data 97 // Optional. Default value memory.New() 98 Storage fiber.Storage 99 100 // KeyLookup is a string in the form of "<source>:<name>" that is used 101 // to extract session id from the request. 102 // Possible values: "header:<name>", "query:<name>" or "cookie:<name>" 103 // Optional. Default value "cookie:session_id". 104 KeyLookup string 105 106 // Domain of the CSRF cookie. 107 // Optional. Default value "". 108 CookieDomain string 109 110 // Path of the CSRF cookie. 111 // Optional. Default value "". 112 CookiePath string 113 114 // Indicates if CSRF cookie is secure. 115 // Optional. Default value false. 116 CookieSecure bool 117 118 // Indicates if CSRF cookie is HTTP only. 119 // Optional. Default value false. 120 CookieHTTPOnly bool 121 122 // Value of SameSite cookie. 123 // Optional. Default value "Lax". 124 CookieSameSite string 125 126 // Decides whether cookie should last for only the browser sesison. 127 // Ignores Expiration if set to true 128 // Optional. Default value false. 129 CookieSessionOnly bool 130 131 // KeyGenerator generates the session key. 132 // Optional. Default value utils.UUIDv4 133 KeyGenerator func() string 134 135 // Deprecated: Please use KeyLookup 136 CookieName string 137 138 // Source defines where to obtain the session id 139 source Source 140 141 // The session name 142 sessionName string 143 } 144 ``` 145 146 ## Default Config 147 148 ```go 149 var ConfigDefault = Config{ 150 Expiration: 24 * time.Hour, 151 KeyLookup: "cookie:session_id", 152 KeyGenerator: utils.UUIDv4, 153 source: "cookie", 154 sessionName: "session_id", 155 } 156 ``` 157 158 ## Constants 159 160 ```go 161 const ( 162 SourceCookie Source = "cookie" 163 SourceHeader Source = "header" 164 SourceURLQuery Source = "query" 165 ) 166 ``` 167 168 ### Custom Storage/Database 169 170 You can use any storage from our [storage](https://github.com/gofiber/storage/) package. 171 172 ```go 173 storage := sqlite3.New() // From github.com/gofiber/storage/sqlite3 174 store := session.New(session.Config{ 175 Storage: storage, 176 }) 177 ``` 178 179 To use the store, see the [Examples](#examples).