-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from salzhrani/v-17
hapi v17
- Loading branch information
Showing
31 changed files
with
1,183 additions
and
1,173 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
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
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
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 |
---|---|---|
@@ -1,57 +1,57 @@ | ||
var Hapi = require('hapi'); | ||
const Hapi = require('hapi'); | ||
|
||
var people = { // our "users database" | ||
const people = { // our "users database" | ||
1: { | ||
id: 1, | ||
name: 'Jen Jones' | ||
} | ||
}; | ||
|
||
// bring your own validation function | ||
var validate = function (decoded, request, callback) { | ||
const validate = async function (decoded, request, h) { | ||
|
||
// do your checks to see if the person is valid | ||
if (!people[decoded.id]) { | ||
return callback(null, false); | ||
return { valid: false }; | ||
} | ||
else { | ||
return callback(null, true); | ||
return { valid: true }; | ||
} | ||
}; | ||
|
||
var server = new Hapi.Server(); | ||
server.connection({ port: 8000 }); | ||
// include our module here ↓↓ | ||
server.register(require('../lib'), function (err) { | ||
|
||
if(err){ | ||
console.log(err); | ||
} | ||
|
||
server.auth.strategy('jwt', 'jwt', | ||
{ key: 'NeverShareYourSecret', // Never Share your secret key | ||
validateFunc: validate // validate function defined above | ||
}); | ||
|
||
server.auth.default('jwt'); | ||
|
||
server.route([ | ||
{ | ||
method: "GET", path: "/", config: { auth: false }, | ||
handler: function(request, reply) { | ||
reply({text: 'Token not required'}); | ||
} | ||
}, | ||
{ | ||
method: 'GET', path: '/restricted', config: { auth: 'jwt' }, | ||
handler: function(request, reply) { | ||
reply({text: 'You used a Token!'}) | ||
.header("Authorization", request.headers.authorization); | ||
} | ||
const init = async () => { | ||
const server = new Hapi.Server({ port: 8000 }); | ||
// include our module here ↓↓ | ||
await server.register(require('../lib')); | ||
server.auth.strategy('jwt', 'jwt', | ||
{ key: 'NeverShareYourSecret', // Never Share your secret key | ||
validate // validate function defined above | ||
}); | ||
|
||
server.auth.default('jwt'); | ||
|
||
server.route([ | ||
{ | ||
method: "GET", path: "/", config: { auth: false }, | ||
handler: function(request, h) { | ||
return {text: 'Token not required'}; | ||
} | ||
]); | ||
}); | ||
|
||
server.start(function () { | ||
}, | ||
{ | ||
method: 'GET', path: '/restricted', config: { auth: 'jwt' }, | ||
handler: function(request, h) { | ||
const response = h.response({text: 'You used a Token!'}); | ||
response.header("Authorization", request.headers.authorization); | ||
return response; | ||
} | ||
} | ||
]); | ||
await server.start(); | ||
return server; | ||
} | ||
init().then(server => { | ||
console.log('Server running at:', server.info.uri); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); |
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
Oops, something went wrong.