Skip to content

Upgraded module to hapi v17#174

Merged
mrlannigan merged 11 commits intohapijs:masterfrom
mrlannigan:feature/hapi17
Dec 9, 2017
Merged

Upgraded module to hapi v17#174
mrlannigan merged 11 commits intohapijs:masterfrom
mrlannigan:feature/hapi17

Conversation

@mrlannigan
Copy link
Copy Markdown
Contributor

@mrlannigan mrlannigan commented Nov 7, 2017

Breaking change

  • Updates validateFunc function signature, see Readme.

This includes updates to all tests, example, and documentation.

@mrlannigan mrlannigan mentioned this pull request Nov 7, 2017
test/index.js Outdated
expect(() => {

expect(err).to.not.exist();
server.auth.strategy('session', 'cookie', { password: new Buffer('foobar') });
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

new Buffer is deprecated since node v6. Should be replaced by Buffer.from.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Updated.

@mrlannigan mrlannigan mentioned this pull request Nov 7, 2017
test/index.js Outdated
const server = new Hapi.Server();
server.connection();
server.register(require('../'), (err) => {
server.register(require('../'));
Copy link
Copy Markdown

@vinicius0026 vinicius0026 Nov 7, 2017

Choose a reason for hiding this comment

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

Shouldn't you await on server.register as per the new docs?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes, you should

test/index.js Outdated

server.auth.strategy('default', 'cookie', true, { validateFunc: 'not a function' });
}).to.throw(Error);
/* eslint-disable hapi/no-shadow-relaxed */
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I guess this flag is no longer necessary

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, if you need to turn off linting in your tests your tests are broken - it looks like you've fixed the error this was meant to circumvent anyway though

Copy link
Copy Markdown
Member

@nlf nlf left a comment

Choose a reason for hiding this comment

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

i added some comments, but overall this is looking really great.

the "fat arrow" related comments, i'm more or less neutral as to whether you use fat arrow functions or not, but they should at least be consistent.

README.md Outdated
};

const logout = function (request, reply) {
const logout = function (request, h) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fat arrow functions

README.md Outdated

const server = new Hapi.Server();
server.connection({ port: 8000 });
const server = new Hapi.Server({ port: 8000 });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

const server = Hapi.server({ port: 8000 });

README.md Outdated
validateFunc: function (request, session, callback) {

cache.get(session.sid, (err, cached) => {
validateFunc: async function (request, session) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fat arrow

README.md Outdated
exports.start();
}
catch (err) {
console.log(err.stack);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i feel like there should be a process.exit(1); here, as-is this would log an error but exit with a non-error exit code, that seems bad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I doubt the catch would ever be triggered since node doesn't have top level async/await.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

good point, it would be a promise rejection, so exports.start().catch((err) => {}); is more appropriate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So what do you all think about wrapping this in a self-executing async function?

Copy link
Copy Markdown
Contributor Author

@mrlannigan mrlannigan Nov 9, 2017

Choose a reason for hiding this comment

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

Also, I didn't really want to make too large of a change in this PR aside from making it compatible with hapi17, but I'm not a huge fan of the fact the whole example/index.js being in the readme. I could simply keep the example updated and point to it from here.

example/index.js Outdated
};

const home = function (request, reply) {
const home = function (request, h) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fat arrow

example/index.js Outdated
};

const logout = function (request, reply) {
const logout = function (request, h) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fat arrow

example/index.js Outdated

const server = new Hapi.Server();
server.connection({ port: 8000 });
const server = new Hapi.Server({ port: 8000 });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

const server = Hapi.server({ port: 8000 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Are we not using new anymore? I thought I saw that somewhere.. maybe I was reading the wrong docs version!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cool, thanks for the research!

example/index.js Outdated
redirectTo: '/login',
isSecure: false,
validateFunc: function (request, session, callback) {
validateFunc: async function (request, session) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fat arrow

test/index.js Outdated
it('fails with no plugin options', (done) => {
it('fails with no plugin options', () => {

const server = new Hapi.Server();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

const server = Hapi.server()

example/index.js Outdated
exports.start();
}
catch (err) {
console.log(err.stack);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

again, process.exit(1);

Copy link
Copy Markdown
Contributor

@Marsup Marsup left a comment

Choose a reason for hiding this comment

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

I think now CookieAuth could be an actual class. Also don't forget to update the engines in package.json.

README.md Outdated
exports.start();
}
catch (err) {
console.log(err.stack);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I doubt the catch would ever be triggered since node doesn't have top level async/await.

lib/index.js Outdated
const decoration = (request) => {

const CookieAuth = function () {
class CookieAuth {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably the class can be extracted and request attached to it through the constructor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, also had to attach the settings.

@toddhickerson
Copy link
Copy Markdown

Any ETA on when this will be completed, or should someone else provide a new PR?

package.json Outdated
"node": ">=8.9.x"
},
"dependencies": {
"boom": "3.x.x",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think boom needs a well-deserved bump.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, done.

@Marsup
Copy link
Copy Markdown
Contributor

Marsup commented Nov 23, 2017

@toddhickerson Why a new PR ?

@aknuds1
Copy link
Copy Markdown
Contributor

aknuds1 commented Nov 24, 2017

@nlf Are there any remaining issues now?

@toddhickerson
Copy link
Copy Markdown

@Marsup Not sure how the whole process works, but what is remaining to get this accepted? This is the last item holding me back from hapi v17 on two projects.

README.md Outdated
not trigger a redirection. Requires **hapi** version 6.2.0 or newer. Defaults to `true`;
- `validateFunc` - an optional session validation function used to validate the content of the
not trigger a redirection. Defaults to `true`;
- `await validateFunc` - an optional session validation function used to validate the content of the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be async here as it is the method provided by the user, not invoked by the user.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, updated.

@aknuds1
Copy link
Copy Markdown
Contributor

aknuds1 commented Nov 26, 2017

I've tested this PR now, by upgrading to it in my app, and it works AFAICT.

not trigger a redirection. Requires **hapi** version 6.2.0 or newer. Defaults to `true`;
- `validateFunc` - an optional session validation function used to validate the content of the
not trigger a redirection. Defaults to `true`;
- `async validateFunc` - an optional session validation function used to validate the content of the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think async should be here? It could confuse people, thinking that async is part of the property name? In hapi-auth-basic I put it as part of the signature: https://github.com/hapijs/hapi-auth-basic#hapi-auth-basic

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In hapi docs its in the description signature.

README.md Outdated
console.log('Server ready');
};

exports.start().catch((err) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

test/helpers.js Outdated
server.route({
method: 'GET', path: '/resource', handler: function (request, h) {

return h.response('resource');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can just return it?

test/helpers.js Outdated
handler: function (request, h) {

request.cookieAuth.set({ user: request.params.user });
return h.response(request.params.user);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can just return it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You can.

if (settings.clearInvalid) {
reply.unstate(settings.cookie);
}
Hoek.assert(typeof result === 'object', 'Invalid return from validateFunc');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here you're validating the user's own code. It feels a bit out of scope, they should have tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Personally I find it useful that the framework verifies the results of user provided hooks for ease of debugging, unless the benefits are outweighed by performance concerns.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's no harm in it I guess. I don't think there's any perf concerns with a couple of asserts.

@toddhickerson
Copy link
Copy Markdown

@mrlannigan I appreciate your work on this and am anxious to use it with Hapi v17. Are there any remaining issues to be addressed?

@aknuds1
Copy link
Copy Markdown
Contributor

aknuds1 commented Dec 4, 2017

I'm also anxious to see this being integrated, I'm already using it (via my namespaced package) :)

@BigWillie
Copy link
Copy Markdown

Will this be accepted? Or should I go with the namespaced package: https://www.npmjs.com/package/@arve.knudsen/hapi-auth-cookie

@nlf
Copy link
Copy Markdown
Member

nlf commented Dec 8, 2017

@mrlannigan i'm going to make you the new maintainer of this module and let you merge and publish this at will. what's your npm username so we can get you permissions there?

@nlf
Copy link
Copy Markdown
Member

nlf commented Dec 8, 2017

also, you have a pending invitation to the hapijs org, iirc you can go to https://github.com/hapijs to accept it

@mrlannigan
Copy link
Copy Markdown
Contributor Author

@nlf Thank you!

@toddhickerson I am going to address @mtharrison changes then work on a release.

@nlf
Copy link
Copy Markdown
Member

nlf commented Dec 8, 2017

@mrlannigan what's your npm username?

@mrlannigan
Copy link
Copy Markdown
Contributor Author

Sorry @nlf, it's the same as my github username: mrlannigan

@nlf
Copy link
Copy Markdown
Member

nlf commented Dec 9, 2017

No worries, I just gave you publish permissions there. Module's all yours!

@mrlannigan mrlannigan added this to the 8.0.0 milestone Dec 9, 2017
@mrlannigan mrlannigan added the breaking changes Change that can breaking existing code label Dec 9, 2017
@mrlannigan mrlannigan merged commit 49b2cd9 into hapijs:master Dec 9, 2017
@mrlannigan mrlannigan deleted the feature/hapi17 branch December 9, 2017 17:49
@mtharrison
Copy link
Copy Markdown
Contributor

It was merged without any approvals?

@nlf
Copy link
Copy Markdown
Member

nlf commented Dec 11, 2017

It’s his module now and his pull request, it’s on him to maintain as he sees fit

@mrlannigan
Copy link
Copy Markdown
Contributor Author

@mtharrison I felt like I had addressed all the blocking changes and next time I will definitely either explicitly ask for the disapproval to be reevaluated or I will dismiss it with an explanation.

@lock
Copy link
Copy Markdown

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

breaking changes Change that can breaking existing code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants