Last active
December 29, 2015 02:49
-
-
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-
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
// 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