1.2.3 โข Published 2 years ago
jwt-authorize v1.2.3
๐ 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 ๐
- ๐ Welcome to JWT Authorize! ๐
- ๐ ๏ธ Function Guide ๐ ๏ธ
- ๐ generateToken ๐
- ๐ก๏ธ authenticate ๐ก๏ธ
- ๐ซ revokeToken ๐ซ
- ๐ซ๐ข revokeManyTokens ๐ซ๐ข
- โ isTokenRevoked โ
- ๐ refreshToken ๐
- ๐ 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 (ifrefreshTokenOptions
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.