Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import * as Hapi from 'hapi';
- import * as HapiAuthJwt2 from 'hapi-auth-jwt2';
- const server = new Hapi.Server();
- server.connection({ host: 'localhost', port: 3000 });
- const people = { // our "users database"
- 1: {
- id: 1,
- name: 'Jen Jones'
- }
- };
- // bring your own validation function
- const validate = function (decoded, request, callback) {
- // do your checks to see if the person is valid
- if (!people[decoded.id]) {
- return callback(null, false);
- }
- else {
- return callback(null, true);
- }
- };
- server.register( HapiAuthJwt2, function (err) {
- if(err){
- console.log(err);
- }
- server.auth.strategy('jwt', 'jwt',
- { key: 'NeverShareYourSecret',
- validateFunc: validate,
- verifyOptions: { algorithms: [ 'HS256' ] } // pick a strong algorithm
- });
- 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);
- }
- }
- ]);
- });
- server.start(function () {
- console.log('Server running at:', server.info.uri);
- });
Advertisement
Add Comment
Please, Sign In to add comment