Skip to content

Instantly share code, notes, and snippets.

@ptte
Last active December 29, 2015 02:49
Show Gist options
  • Save ptte/7603018 to your computer and use it in GitHub Desktop.
Save ptte/7603018 to your computer and use it in GitHub Desktop.
Custom authentication example for node hapi library Just an example of simplest possible custom authentication to build on. The slightly limited documentation is available here: http://spumko.github.io/resource/api/#server-auth-name--options-
// Hapi Custom auth
// ===========
//
// ## /lib/auth.js
//
var internals = {};
var Hapi = require('hapi');
exports = module.exports = internals.Scheme = function (server, options) {};
internals.Scheme.prototype.authenticate = function (request, callback) {
if (request.query.token === 'PASSWORD') {
return callback(null, 'Authed');
}
callback(Hapi.error.unauthorized('No auth'), []);
};
// ## index.js
var server = new Hapi.Server(
interface,
port
);
var Auth = require('./lib/auth');
server.auth('myAuth', {
implementation: new Auth(server, {})
});
var routes = [
{
method: 'GET',
path: '/auth',
config: {
handler: function (request) {
request.reply({message:"Auth works"});
},
auth: {
strategies: ['myAuth']
}
}
}
]
server.addRoutes(routes);
server.start(function() {
console.log("Server started");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment