Last active
August 29, 2015 14:13
-
-
Save jokeyrhyme/dcf622385ded9cf232bc to your computer and use it in GitHub Desktop.
Hapi.js versus asynchronous authentication schemes
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
'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); | |
}); |
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
'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); | |
}); |
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
'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); | |
}); |
fails-1.js -> works-3.js after fixing a logic bug
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fails-1.js: