1.0.0 • Published 7 years ago

@botmatic/js-jwt v1.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

JS JWT

A simple JWT module to authenticate an integration to external services

Initialisation

// Initialisation
const jwt = require('js-jwt')({
  url: 'https://my.external-api.com/authorize',
  contentType: 'application/json',
  headerName: 'Authorization',
  headerPrefix: 'Bearer',
  onResponse: body => body.token || false
})

Initialisation parameters

parameterstypeattributesdescription
urlstringrequiredurl on an external service to obtain a JSON Web Token
onResponsefunction(object) -> string or falserequiredreceives the response body to the call to url and returns the token it contains
contentTypestringoptional, default = "application/json"Content-Type for the request sent to url
headerNamestringoptional, default = "Authorization"Name for the Authorization header
headerPrefixstringoptional, default = "Bearer"Will be prefix to the token in the Authorization header

Obtaining a token

// using async / await
const header = await jwt.auth({user: "name", pass: "word"})
// header == {"Authorization": "Bearer <YOUR_TOKEN>"}

// using Promises
jwt.auth({user: "name", pass: "word"})
 .then(header => {
   // header == {"Authorization": "Bearer <YOUR_TOKEN>"}
 })