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
npm install --save rs-jwt
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)
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)
MIT