1.2.3 โ€ข Published 2 years ago

jwt-authorize v1.2.3

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

๐Ÿš€ 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.

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1-2

2 years ago

1.0.1-1

2 years ago

1.0.1-c

2 years ago