Skip to content

Commit

Permalink
Add cookieMaxAge and secureCookies options (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadeByMike committed Sep 11, 2019
1 parent c50a7be commit 0a627ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/smooth-pumpkins-deliver/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"releases": [
{ "name": "@keystone-alpha/keystone", "type": "minor" },
{ "name": "@keystone-alpha/session", "type": "major" }
],
"dependents": []
}
13 changes: 13 additions & 0 deletions .changeset/smooth-pumpkins-deliver/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Adds a `cookieMaxAge` and `secureCookies` option to the keystone constructor.

These will default to 30 days for `cookieMaxAge` and `true` in production `false` in other environments for `secureCookies`.

### Usage
```javascript
const keystone = new Keystone({
cookieMaxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
secureCookies: true,
});
```

Note: `commonSessionMiddleware` now accepts a config object rather than multiple arguments.
14 changes: 12 additions & 2 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ module.exports = class Keystone {
onConnect,
cookieSecret = 'qwerty',
sessionStore,
secureCookies = process.env.NODE_ENV === 'production', // Default to true in production
cookieMaxAge = 1000 * 60 * 60 * 24 * 30, // 30 days
schemaNames = ['public'],
}) {
this.name = name;
Expand All @@ -58,9 +60,11 @@ module.exports = class Keystone {
this._extendedMutations = [];
this._graphQLQuery = {};
this._cookieSecret = cookieSecret;
this._secureCookies = secureCookies;
this._cookieMaxAge = cookieMaxAge;
this._sessionStore = sessionStore;
this.registeredTypes = new Set();
this.eventHandlers = { onConnect };
this.registeredTypes = new Set();
this._schemaNames = schemaNames;

if (adapters) {
Expand Down Expand Up @@ -544,7 +548,13 @@ module.exports = class Keystone {
// Used by other middlewares such as authentication strategies. Important
// to be first so the methods added to `req` are available further down
// the request pipeline.
commonSessionMiddleware(this, this._cookieSecret, this._sessionStore),
commonSessionMiddleware({
keystone: this,
cookieSecret: this._cookieSecret,
sessionStore: this.sessionStore,
secureCookies: this._secureCookies,
cookieMaxAge: this._cookieMaxAge,
}),
...(await Promise.all(
[
// Inject any field middlewares (eg; WYSIWIG's static assets)
Expand Down
9 changes: 8 additions & 1 deletion packages/session/lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const cookieSignature = require('cookie-signature');
const expressSession = require('express-session');
const cookie = require('cookie');

const commonSessionMiddleware = (keystone, cookieSecret, sessionStore) => {
const commonSessionMiddleware = ({
keystone,
cookieSecret,
sessionStore,
secureCookies,
cookieMaxAge,
}) => {
const COOKIE_NAME = 'keystone.sid';

// We have at least one auth strategy
Expand Down Expand Up @@ -50,6 +56,7 @@ const commonSessionMiddleware = (keystone, cookieSecret, sessionStore) => {
resave: false,
saveUninitialized: false,
name: COOKIE_NAME,
cookie: { secure: secureCookies, maxAge: cookieMaxAge },
store: sessionStore,
});

Expand Down

0 comments on commit 0a627ef

Please sign in to comment.