rs-jwt
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

Build Status codecov Maintainability Mutation testing badge npm version

Really Simple JWT JS

RS-JWT is a TypeScript JSON Web Token reader for the browser which allows you to access the header and payload claims data contained in a JWT. It can be used in any JavaScript or TypeScript project. Please note you cannot validate tokens with this package.

It is part of the PHP ReallySimpleJWT family of JSON Web Token tools which help you easily add JWT functionality to your web projects.

  • ReallySimpleJWT: Create and validate JSON Web Tokens.
  • PSR-JWT: Add JSON Web Token authorisation middleware to a PHP Project.

For more information on JSON Web Tokens please read our basic introduction to them or take a look at RFC 7519

Installation

npm install --save rs-jwt

Basic Usage

To read a token simply import the parseJwt() method and provide it a token.

This will return an object with the methods getHeader() and getPayload() which can be used to return the relevant claims.

import { parseJwt } from 'rs-jwt'
 
const result = parseJwt('json.web.token')
 
// Return the header claims as an object.
const header = result.getHeader()
 
// Access the type claim.
console.log(header.typ)
 
// Return the payload claims as an object.
const payload = result.getPayload()
 
// Access the expiry claim.
console.log(payload.exp)

Advanced Usage

The parseJwt() method is a facade for the RSJwt class. If you wish to use an OOP approach in your project import the RSJwt class directly, instantiate it and then call the parse() method and pass in a JSON Web Token.

import { RSJwt } from 'rs-jwt'
 
const jwt = new RSJwt()
 
const result = jwt.parse('json.web.token')
 
// Return the header claims as an object.
const header = result.getHeader()
 
// Access the type claim.
console.log(header.typ)
 
// Return the payload claims as an object.
const payload = result.getPayload()
 
// Access the expiry claim.
console.log(payload.exp)

License

MIT

Author

Rob Waller

Package Sidebar

Install

npm i rs-jwt

Weekly Downloads

38

Version

0.2.0

License

MIT

Unpacked Size

9.08 kB

Total Files

11

Last publish

Collaborators

  • robdwaller