Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Basic usage example to README #13

Closed
nelsonic opened this issue Mar 4, 2015 · 1 comment
Closed

Add Basic usage example to README #13

nelsonic opened this issue Mar 4, 2015 · 1 comment

Comments

@nelsonic
Copy link
Member

nelsonic commented Mar 4, 2015

In addition to the /example it makes sense to have a simple example in the README

@nelsonic
Copy link
Member Author

nelsonic commented Mar 4, 2015

Usage

Install from NPM

npm install hapi-auth-jwt2 --save

Example

There is a an example in the /example directory of this repo
and we are preparing a more real-world
example you can drop into your app and go!

But here are the basic usage to get started:

var Hapi        = require('hapi');
var JWT         = require('jsonwebtoken');  // used to sign our content
var port        = process.env.PORT || 8000; // allow port to be set

var secret = 'NeverShareYourSecret'; // Never Share This! even in private GitHub repos!

var people = { // our "users databse"
    1: {
      id: 1,
      name: 'Anthony Valid User'
    }
};

// use the token as the 'authorization' header in requests
var token = JWT.sign(people[1], secret); // synchronous

// bring your own validation function
var validate = function (decoded, callback) {

    console.log(decoded);

    // do your checks to see if the person is valid
    if (!people[decoded.id]) {
      return callback(null, false);
    }
    else {
      return callback(null, true);
    }
};

var server = new Hapi.Server();
server.connection({ port: port });
        // include our module here ↓↓
server.register(require('hapi-auth-jwt2'), function (err) {

    if(err){
      console.log(err);
    }
    // see: http://hapijs.com/api#serverauthschemename-scheme
    server.auth.strategy('jwt', 'jwt', true, { key: secret,  validateFunc: validate });

    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();

Run the server with: node example/server.js

Now use curl to access the two routes:

No Token Required

curl -v http://localhost:8000/

Token Required

Try to access the /restricted content without supplying a Token
(expect to see a _401 error_):

curl -v http://localhost:8000/restricted

Now access the url using the following format:
curl -H "Authorization: <TOKEN>" http://localhost:8000/restricted

A here's a valid token you can use:

curl -v -H "Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6IkFudGhvbnkgVmFsaWQgVXNlciIsImlhdCI6MTQyNTQ3MzUzNX0.KA68l60mjiC8EXaC2odnjFwdIDxE__iDu5RwLdN1F2A" \
http://localhost:8000/restricted

That's it.

Write your own validateFunc with what ever checks you want to perform
on the decoded token before allowing the visitor to proceed.

nelsonic added a commit that referenced this issue Mar 4, 2015
@nelsonic nelsonic self-assigned this Mar 4, 2015
@nelsonic nelsonic closed this as completed Mar 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant