jwt-express-auth

0.1.6 • Public • Published

JWT User Authorization Middleware

Add the following to any params that require authorization. The tokenParam must match the name of the user_id in the jwt, not the route param.

    var auth = require('jwt-express-auth');
    router.param('user_id', auth({secret:'secret', tokenParam:'id'}));

To generate a jwt token in Ruby.

    require 'jwt'
    JWT.encode({"id" => 1}, "secret")
    => "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCciOjg5fQ.wUlU3JFv-wQYPfa9NRwh06wJWcOtYErRN23iTvMPTIE"

Examples using curl

    curl -X GET -H "Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCciOjg5fQ.wUlU3JFv-wQYPfa9NRwh06wJWcOtYErRN23iTvMPTIE" -H "Cache-Control:no-cache" http://localhost:2014/users/1

Or

    curl -X GET http://localhost:2014/users/1&authorization=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCciOjg5fQ.wUlU3JFv-wQYPfa9NRwh06wJWcOtYErRN23iTvMPTIE

From jquery

$.ajax({
    url: 'http://localhost:2014/users/1',
    headers: {
        'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCciOjg5fQ.wUlU3JFv-wQYPfa9NRwh06wJWcOtYErRN23iTvMPTIE',
        'Cache-Control': 'no-cache'
    }
}).done(function(res) { console.log(res) })

Or

$.get({
    url: 'http://localhost:2014/users/1?authorization=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCciOjg5fQ.wUlU3JFv-wQYPfa9NRwh06wJWcOtYErRN23iTvMPTIE'
}).done(function(res) { console.log(res) })

Readme

Keywords

none

Package Sidebar

Install

npm i jwt-express-auth

Weekly Downloads

12

Version

0.1.6

License

none

Last publish

Collaborators

  • tdfairbrother