Skip to content

Commit

Permalink
Added Hapi API Example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Totten committed Feb 13, 2016
1 parent 64ff24d commit 6b3a28d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/hapi-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Logs
logs
*.log
npm-debug.log*

node_modules
.env
16 changes: 16 additions & 0 deletions examples/hapi-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "hapijs-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Auth0",
"license": "MIT",
"dependencies": {
"dotenv": "^2.0.0",
"hapi": "^13.0.0",
"hapi-auth-jwt": "^4.0.0"
}
}
53 changes: 53 additions & 0 deletions examples/hapi-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var Hapi = require('hapi');
var dotenv = require('dotenv');
var server = new Hapi.Server();

dotenv.load();

server.connection({ port: 8000 });

server.register(require('hapi-auth-jwt'), function (err) {

if (err) {
throw err;
}

server.auth.strategy('token', 'jwt', {
key: new Buffer(process.env.AUTH0_CLIENT_SECRET, 'base64'),
verifyOptions: {
algorithms: [ 'HS256' ],
audience: process.env.AUTH0_CLIENT_ID
}
});

server.route({
method: 'GET',
path: '/private',
config: { auth: 'token' },
handler: function(request, reply) {
reply({
message: 'This is a private endpoint - a token is required.',
credentials: request.auth.credentials
});
}
});

server.route({
method: 'GET',
path: '/public',
config: { auth: false },
handler: function(request, reply) {
reply({
message: 'This is a public endpoint - no token required.'
});
}
});


server.start((err) => {
if (err) {
throw err;
}
console.log('Server started at:', server.info.uri);
});
});

0 comments on commit 6b3a28d

Please sign in to comment.