Skip to content

Commit a50aa1f

Browse files
committed
docs: add cookie callback example
1 parent 518620e commit a50aa1f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ For a list of stores, see [compatible session stores](#compatible-session-stores
4949
Settings object for the session ID cookie. The default value is
5050
`{ path: '/', httpOnly: true, secure: false, maxAge: null }`.
5151

52+
In addition to providing a static object, you can also pass a callback function to dynamically generate the cookie options for each request. The callback receives the `req` object as its argument and should return an object containing the cookie settings.
53+
54+
```js
55+
var app = express()
56+
app.use(session({
57+
secret: 'keyboard cat',
58+
resave: false,
59+
saveUninitialized: true,
60+
cookie: function(req) {
61+
var match = req.url.match(/^\/([^/]+)/);
62+
return {
63+
path: match ? '/' + match[1] : '/',
64+
httpOnly: true,
65+
secure: req.secure || false,
66+
maxAge: 60000
67+
}
68+
}
69+
}))
70+
```
71+
5272
The following are options that can be set in this object.
5373

5474
##### cookie.domain

0 commit comments

Comments
 (0)