npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

jwt-authorize

v1.2.3

Published

A powerful and easy-to-use library for handling JSON Web Tokens (JWT) in your Node.js applications. Be it generating, authenticating, refreshing, or managing tokens, JWT Authorize has got you covered! 🎩

Downloads

28

Readme

πŸš€ JWT Authorize - Secure Your Tokens! πŸ›‘οΈ

A powerful and easy-to-use library for handling JSON Web Tokens (JWT) in your Node.js applications. Be it generating, authenticating, refreshing, or managing tokens, JWT Authorize has got you covered! 🎩

🎁 What's inside

  • πŸ’ͺ Robust token generation
  • πŸ” Secure token authentication
  • πŸ”„ Easy token refreshing
  • ❌ Token revocation utilities

πŸ“š Table of Contents πŸ“š

  1. πŸŽ‰ Welcome to JWT Authorize! πŸŽ‰
  2. πŸ› οΈ Function Guide πŸ› οΈ
    1. πŸš€ generateToken πŸš€
    2. πŸ›‘οΈ authenticate πŸ›‘οΈ
    3. 🚫 revokeToken 🚫
    4. πŸš«πŸ”’ revokeManyTokens πŸš«πŸ”’
    5. ❓ isTokenRevoked ❓
    6. πŸ”„ refreshToken πŸ”„
  3. πŸ“ƒ License πŸ“ƒ

🧰 Installation

npm install jwt-authorize

πŸ§ͺ Functions

🎟️ generateToken

Generates access and refresh tokens.

Parameters

  • accessTokenOptions: Object
    • payload: Object - The data you want to encode in the access token.
    • secret: String - The secret key for the access token.
    • options: SignOptions - Optional JWT sign options.
  • refreshTokenOptions: Object (optional)
    • payload: Object - The data you want to encode in the refresh token.
    • secret: String - The secret key for the refresh token.
    • options: SignOptions - Optional JWT sign options.

Returns

  • Object
    • accessToken: String - The generated access token.
    • refreshToken: String - The generated refresh token (if refreshTokenOptions were provided).
    • status: Number - 200 if successful.

Example

import { generateToken } from "jwt-authorize";

const tokens = generateToken(
  {
    payload: { username: "Alice" },
    secret: "super-secret-key",
    options: { expiresIn: '1h' },
  },
  {
    payload: { username: "Alice" },
    secret: "another-super-secret-key",
    options: { expiresIn: '7d' },
  }
);

console.log(tokens); // { accessToken: "...", refreshToken: "...", status: 200 }

πŸ”’ authenticate

Authenticates a user based on their access token.

Parameters

  • token: Object
    • accessToken: String - The access token to authenticate.
    • refreshToken: String (optional) - The refresh token.
  • secret: String - The secret key to verify the token.

Returns

  • Object
    • isAuthenticated: Boolean - True if the token is valid, false otherwise.
    • payload: Object - The decoded payload of the token if authenticated.
    • status: Number - 200 if authenticated, 401 if not authenticated.

Example

import { authenticate } from "jwt-authorize";

const result = authenticate(
  {
    accessToken: "your-access-token",
  },
  "your-secret-key"
);

console.log(result); // { isAuthenticated: true, payload: {...}, status: 200 }

❌ revokeToken

Revokes a single token, so it can't be used again.

Parameters

  • token: String - The token to revoke.

Example

import { revokeToken } from "jwt-authorize";

revokeToken("your-token-to-revoke");

❌❌ revokeManyTokens

Revokes multiple tokens at once.

Parameters

  • tokens: Array of Strings - The tokens to revoke.

Example

import { revokeManyTokens } from "jwt-authorize";

revokeManyTokens(["token-1", "token-2", "token-3"]);

πŸ•΅οΈ isTokenRevoked

Checks if a token has been revoked.

Parameters

  • token: String - The token to check.

Returns

  • Boolean - True if the token has been revoked, false otherwise.

Example

import { isTokenRevoked } from "jwt-authorize";

const revoked = isTokenRevoked("your-token");

console.log(revoked); // true or false

πŸ”„ refreshToken

Refreshes the access and refresh tokens. This is useful to get new tokens without asking the user for their credentials again.

Parameters

  • refreshToken: String - The refresh token.
  • refreshSecret: String - The secret for the refresh token.
  • accessTokenSecret: String - The secret for the access token.
  • accessTokenExpiry: String (default = "15m") - The expiry duration for the access token.
  • refreshTokenExpiry: String (default = "7d") - The expiry duration for the refresh token.

Returns

  • Object
    • accessToken: String - The new access token.
    • refreshToken: String - The new refresh token.
    • status: Number - 200 if successful.

Example

import { refreshToken } from "jwt-authorize";

const newTokens = refreshToken(
  "your-old-refresh-token",
  "refresh-secret",
  "access-secret"
);

console.log(newTokens); // { accessToken: "...", refreshToken: "...", status: 200 }

πŸ“œ License

This package is licensed under the MIT License.