Code Monkey home page Code Monkey logo

jwt-ts's Introduction

JWT

JWT Library

A TypeScript library for encoding, decoding and validating JSON Web Tokens (JWTs) using the HS256 algorithm. This library includes functionality for encoding JWTs with a specified payload and secret, decoding JWTs to extract their contents, and validating JWTs.

Features

  • Encode JWTs with HS256 algorithm
  • Decode JWTs and extract payload and header
  • Validate JWTs
  • Support for customizable payload expiration
  • Supports both ES Modules and CommonJS modules

Installation

You can install this library via npm:

npm i @falgunpal/jwt-helper-ts

Usage

Encoding a JWT

To create a JWT, use the encode_jwt function:

import { encode_jwt } from "@falgunpaljwt-helper-ts";
const secret = 'your-secret';
const id = 1;
const payload = { name: 'John Doe' };
const ttl = 3600; // Optional, in seconds

const token = encode_jwt(secret, id, payload, ttl);
console.log('Generated Token:', token);

Decoding a JWT

To decode a JWT, use the decode_jwt function:

import { decode_jwt } from '@falgunpaljwt-helper-ts';

const secret = 'your-secret';
const token = 'your-jwt-token';

const decoded = decode_jwt(secret, token);
console.log('Decoded JWT:', decoded);

Validating a JWT

To validate a JWT, use the validate_jwt function:

import { validate_jwt } from  '@falgunpaljwt-helper-ts';

const secret = 'your-secret';
const token = 'your-jwt-token';

const isValid = validate_jwt(secret, token);
console.log('Is Token Valid?', isValid);

Module System Support

This package supports both ECMAScript Modules (ESM) and CommonJS (CJS) module configurations. You can import and use the functions based on your module system preference.

ESM Usage

If you're using ESM:

import { encode_jwt, decode_jwt, validate_jwt } from 'jwt-helper';

CJS Usage

If you're using CommonJS:

const { encode_jwt, decode_jwt, validate_jwt } = require('jwt-helper');

Testing

The library includes tests for its core functionality. To run the tests, use:

npm test

jwt-ts's People

Contributors

falgun143 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.