-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nathan Totten
committed
Feb 13, 2016
1 parent
64ff24d
commit 6b3a28d
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
node_modules | ||
.env |
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
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" | ||
} | ||
} |
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
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); | ||
}); | ||
}); |