-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move session handling code into a SessionManager class
- Loading branch information
Showing
13 changed files
with
95 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"releases": [ | ||
{ "name": "@voussoir/admin-ui", "type": "patch" }, | ||
{ "name": "@voussoir/core", "type": "major" }, | ||
{ "name": "@voussoir/fields", "type": "patch" }, | ||
{ "name": "@voussoir/server", "type": "patch" }, | ||
{ "name": "@voussoir/cypress-project-basic", "type": "patch" }, | ||
{ "name": "@voussoir/cypress-project-login", "type": "patch" }, | ||
{ "name": "@voussoir/cypress-project-twitter-login", "type": "patch" } | ||
], | ||
"dependents": [ | ||
{ "name": "@voussoir/adapter-mongoose", "type": "patch", "dependencies": ["@voussoir/core"] }, | ||
{ | ||
"name": "@voussoir/test-utils", | ||
"type": "patch", | ||
"dependencies": ["@voussoir/adapter-mongoose", "@voussoir/core", "@voussoir/server"] | ||
}, | ||
{ | ||
"name": "@voussoir/cypress-project-access-control", | ||
"type": "patch", | ||
"dependencies": [ | ||
"@voussoir/adapter-mongoose", | ||
"@voussoir/test-utils", | ||
"@voussoir/admin-ui", | ||
"@voussoir/core", | ||
"@voussoir/fields", | ||
"@voussoir/server" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- Rename keystone.session to keystone.sessionManager | ||
- Rename keystone.session.validate to keystone.sessionManager.populateAuthedItemMiddleware | ||
- Rename keystone.session.create to keystone.sessionManager.startAuthedSession | ||
- Rename keystone.session.destroy to keystone.sessionManager.endAuthedSession |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
const noop = () => undefined; | ||
|
||
const validate = keystone => ({ valid = noop, invalid = noop }) => async (req, res, next) => { | ||
if (!req.session || !req.session.keystoneItemId) { | ||
invalid({ req, reason: 'empty' }); | ||
return next(); | ||
class SessionManager { | ||
constructor(keystone) { | ||
this.keystone = keystone; | ||
this.populateAuthedItemMiddleware = this.populateAuthedItemMiddleware.bind(this); | ||
} | ||
const list = keystone.lists[req.session.keystoneListKey]; | ||
if (!list) { | ||
// TODO: probably destroy the session | ||
invalid({ req, reason: 'invalid-list' }); | ||
return next(); | ||
|
||
async populateAuthedItemMiddleware(req, res, next) { | ||
if (!req.session || !req.session.keystoneItemId) { | ||
return next(); | ||
} | ||
const list = this.keystone.lists[req.session.keystoneListKey]; | ||
if (!list) { | ||
// TODO: probably destroy the session | ||
return next(); | ||
} | ||
const item = await list.adapter.findById(req.session.keystoneItemId); | ||
if (!item) { | ||
// TODO: probably destroy the session | ||
return next(); | ||
} | ||
req.user = item; | ||
req.authedListKey = list.key; | ||
|
||
next(); | ||
} | ||
const item = await list.adapter.findById(req.session.keystoneItemId); | ||
if (!item) { | ||
// TODO: probably destroy the session | ||
invalid({ req, reason: 'invalid-item' }); | ||
return next(); | ||
|
||
startAuthedSession(req, { item, list }) { | ||
return new Promise((resolve, reject) => | ||
req.session.regenerate(err => { | ||
if (err) return reject(err); | ||
req.session.keystoneListKey = list.key; | ||
req.session.keystoneItemId = item.id; | ||
resolve(); | ||
}) | ||
); | ||
} | ||
valid({ req, list, item }); | ||
next(); | ||
}; | ||
|
||
function create(req, { item, list }) { | ||
return new Promise((resolve, reject) => | ||
req.session.regenerate(err => { | ||
if (err) return reject(err); | ||
req.session.keystoneListKey = list.key; | ||
req.session.keystoneItemId = item.id; | ||
resolve(); | ||
}) | ||
); | ||
} | ||
function destroy(req) { | ||
return new Promise((resolve, reject) => | ||
req.session.regenerate(err => { | ||
if (err) return reject(err); | ||
resolve({ success: true }); | ||
}) | ||
); | ||
endAuthedSession(req) { | ||
return new Promise((resolve, reject) => | ||
req.session.regenerate(err => { | ||
if (err) return reject(err); | ||
resolve({ success: true }); | ||
}) | ||
); | ||
} | ||
} | ||
|
||
module.exports = keystone => ({ | ||
create: create, | ||
destroy: destroy, | ||
validate: validate(keystone), | ||
}); | ||
module.exports = SessionManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters