Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

njwt

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

njwt

JWT Library for Node.js

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
286K
decreased by-36.78%
Maintainers
1
Weekly downloads
 
Created

What is njwt?

The njwt package is a Node.js library for creating, parsing, and verifying JSON Web Tokens (JWTs). It provides a simple and secure way to handle JWTs, which are commonly used for authentication and authorization in web applications.

What are njwt's main functionalities?

Creating a JWT

This feature allows you to create a new JWT with specific claims. The `create` method takes the claims and a signing key, and returns a JWT object. The `compact` method then converts the JWT object into a compact, URL-safe string.

const njwt = require('njwt');
const secureRandom = require('secure-random');

const claims = { sub: 'user123', iss: 'my-app' };
const signingKey = secureRandom(256, { type: 'Buffer' });
const jwt = njwt.create(claims, signingKey);
const token = jwt.compact();
console.log(token);

Verifying a JWT

This feature allows you to verify the authenticity and integrity of a JWT. The `verify` method takes the token, the signing key, and a callback function. If the token is valid, the callback function receives the verified JWT object; otherwise, it receives an error.

const njwt = require('njwt');
const token = 'your.jwt.token.here';
const signingKey = 'your-signing-key';

njwt.verify(token, signingKey, (err, verifiedJwt) => {
  if (err) {
    console.log('Token is invalid:', err.message);
  } else {
    console.log('Token is valid:', verifiedJwt);
  }
});

Parsing a JWT

This feature allows you to parse a JWT and extract its claims. The `verify` method can be used to parse the token and return the JWT object, from which you can access the claims via the `body` property.

const njwt = require('njwt');
const token = 'your.jwt.token.here';

const parsedJwt = njwt.verify(token, 'your-signing-key');
console.log(parsedJwt.body);

Other packages similar to njwt

Keywords

FAQs

Package last updated on 11 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc