Skip to content

Instantly share code, notes, and snippets.

@jokeyrhyme
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokeyrhyme/dcf622385ded9cf232bc to your computer and use it in GitHub Desktop.
Save jokeyrhyme/dcf622385ded9cf232bc to your computer and use it in GitHub Desktop.
Hapi.js versus asynchronous authentication schemes
'use strict';
// Node.js built-ins
var http = require('http');
// 3rd-party modules
var Hapi = require('hapi');
// this module
var server = new Hapi.Server();
server.connection({ port: 3000 });
server.auth.scheme('http', function () {
return {
authenticate: function (req, reply) {
console.log('authenticate(): ...');
http.get(server.info.uri + '/auth', function (res) {
if (res && res.statusCode === 200) {
console.log('authenticated');
return reply.continue({ credentials: {} });
}
console.log('denied');
return reply(new Error('denied'));
}).on('error', function (err) {
console.error(err);
return reply(new Error('denied due to error'));
});
}
};
});
server.auth.strategy('default', 'http');
server.auth.default('default');
server.route({
method: 'GET',
path: '/auth',
config: {
auth: false,
},
handler: function (request, reply) {
reply('GET /auth');
}
});
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('GET /');
}
});
server.start(function () {
console.log('Server running at:', server.info.uri);
});
'use strict';
// Node.js built-ins
var http = require('http');
// 3rd-party modules
var Hapi = require('hapi');
// this module
var server = new Hapi.Server();
server.connection({ port: 3000 });
server.auth.scheme('http', function () {
return {
authenticate: function (req, reply) {
console.log('authenticate(): ...');
setTimeout(function () {
http.get(server.info.uri + '/auth', function (res) {
if (res && res.statusCode === 200) {
console.log('authenticated');
return reply.continue({ credentials: {} });
}
console.log('denied');
return reply(new Error('denied'));
}).on('error', function (err) {
console.error(err);
return reply(new Error('denied due to error'));
});
}, 1e3);
}
};
});
server.auth.strategy('default', 'http');
server.auth.default('default');
server.route({
method: 'GET',
path: '/auth',
config: {
auth: false,
},
handler: function (request, reply) {
reply('GET /auth');
}
});
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('GET /');
}
});
server.start(function () {
console.log('Server running at:', server.info.uri);
});
'use strict';
// Node.js built-ins
var http = require('http');
// 3rd-party modules
var Hapi = require('hapi');
// this module
var server = new Hapi.Server();
server.connection({ port: 3000 });
server.auth.scheme('http', function () {
return {
authenticate: function (req, reply) {
console.log('authenticate(): ...');
http.get(server.info.uri + '/auth', function (res) {
if (res && res.statusCode === 200) {
setTimeout(function () {
console.log('authenticated');
return reply.continue({ credentials: {} });
}, 1e3);
} else {
console.log('denied');
return reply(new Error('denied'));
}
}).on('error', function (err) {
console.error(err);
return reply(new Error('denied due to error'));
});
}
};
});
server.auth.strategy('default', 'http');
server.auth.default('default');
server.route({
method: 'GET',
path: '/auth',
config: {
auth: false,
},
handler: function (request, reply) {
reply('GET /auth');
}
});
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('GET /');
}
});
server.start(function () {
console.log('Server running at:', server.info.uri);
});
@jokeyrhyme
Copy link
Author

fails-1.js:

Debug: internal, implementation, error
TypeError: Property '_next' of object function (err, response, data) {

   reply._data = data;                 // Held for later
   return reply.response(err !== null && err !== undefined ? err : response);

} is not a function
at Function.internals.continue (/Users/ron/Projects/hapi-test/node_modules/hapi/lib/reply.js:102:10)
at null._onTimeout (/Users/ron/Projects/hapi-test/index.js:24:34)
at Timer.listOnTimeout as ontimeout

@jokeyrhyme
Copy link
Author

fails-1.js -> works-3.js after fixing a logic bug

@jokeyrhyme
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment