Skip to content

Latest commit

 

History

History
91 lines (81 loc) · 2.23 KB

README.md

File metadata and controls

91 lines (81 loc) · 2.23 KB

jwt-toolkit-fast

jwt-toolkit-fast ✨ is a JavaScript module for JSON Object Signing and Encryption, providing support for JSON Web Tokens (JWT). The module is designed to work across various Web-interoperable runtimes including Node.js and browsers.

Demo

jwt-toolkit-fast-demo🔗

Documentation

The jwt-toolkit-fast module supports JSON Web Tokens (JWT) and provides functionality for signing and verifying tokens.

Encode/Sign JWT

Creates a JWT using the given payload.

import { encode_jwt } from "jwt-toolkit-fast";

const generateToken = async (): Promise<void> => {
  const secret = 'secret';
  const payload = { userId: 123, name: 'Anish' };
  const options = {ttl: 3600, aud: "my-aud", iss: "laptop"}

  try {
    const token = await encode_jwt(secret, 1, payload, options);
    console.log('Encoded Token:', token);
  } catch (error) {
    console.error('Error encoding JWT:', error);
  }
};

aud and iss are optional parameters passed in options.

Decode JWT

Decodes a JWT and returns the payload.

import { decode_jwt } from "jwt-toolkit-fast";

const decodeToken = async (): Promise<void> => {
  const secret = 'secret';
  try {
    const signature = await decode_jwt(secret, token)
    console.log(signature);
  } catch (err) {
    console.log(err);
  }
}

Validate JWT

Validates a JWT, if a JWT token is expired then it returns false otherwise returns true

import { validate_jwt } from "jwt-toolkit-fast";

const validateToken = async (): Promise<void> => {
  const secret = 'secret';
  try {
    const signature = await validate_jwt(secret, token)
    console.log(signature);
  } catch (err) {
    console.log(err);
  }
}

Installation

  git clone https://github.com/kirtanchandak/jwt-toolkit-fast
  cd jwt-toolkit-fast

Installing dependencies

  npm install

Compile typescipt files

  cd src
  npx tsc

Testing

I have added two methods to the test.

Manual testing

Via console logging JWT and decoded JWT

  cd dist 
  node manualTest.js

Jest testing

 npm test

Authors

Conclusion

happy hacking:) 🚀