-
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
11 changed files
with
60 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
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