Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use server.decorate #107

Merged
merged 4 commits into from Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 27 additions & 8 deletions lib/index.js
Expand Up @@ -69,10 +69,15 @@ internals.implementation = function (server, options) {

server.state(settings.cookie, cookieOptions);

server.ext('onPreAuth', (request, reply) => {
const decoration = function (request) {

const cookieAuth = function () {

const self = this;

this.set = function (session, value) {

request.cookieAuth = {
set: function (session, value) {
const reply = self.reply;

if (arguments.length > 1) {
const key = session;
Expand All @@ -87,8 +92,11 @@ internals.implementation = function (server, options) {
Hoek.assert(session && typeof session === 'object', 'Invalid session');
request.auth.artifacts = session;
reply.state(settings.cookie, session);
},
clear: function (key) {
};

this.clear = function (key) {

const reply = self.reply;

if (arguments.length) {
Hoek.assert(key && typeof key === 'string', 'Invalid session key');
Expand All @@ -100,15 +108,26 @@ internals.implementation = function (server, options) {

request.auth.artifacts = null;
reply.unstate(settings.cookie);
},
ttl: function (msecs) {
};

this.ttl = function (msecs) {

const reply = self.reply;
const session = request.auth.artifacts;
Hoek.assert(session, 'No active session to modify ttl on');
reply.state(settings.cookie, session, { ttl: msecs });
}
};
};

return new cookieAuth();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be CookieAuth

};

server.decorate('request', 'cookieAuth', decoration, { apply: true });

server.ext('onPreAuth', (request, reply) => {

request.cookieAuth.reply = reply;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment that reply is only used for setting and unsetting state, and not for actual replies.


return reply.continue();
});

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -20,11 +20,11 @@
"joi": "7.x.x"
},
"peerDependencies": {
"hapi": ">=10.x.x"
"hapi": ">=11.x.x"
},
"devDependencies": {
"code": "2.x.x",
"hapi": "10.x.x",
"hapi": "11.x.x",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not 12?

"lab": "8.x.x"
},
"scripts": {
Expand Down