jwt-auths 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +45 -17
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -3,40 +3,68 @@
3
3
  A simple and secure JWT authentication library for Node.js, providing functions to create access tokens and refresh tokens.
4
4
 
5
5
  ## 🚀 Features
6
- - Generate access tokens with a secret key.
7
- - Refresh tokens for extended authentication sessions.
8
- - Secure and easy to use.
6
+ 🔐 Create JWT tokens with a secret key.
7
+ 🪪 Generate access tokens with customizable expiration and algorithm.
8
+ 📥 Decode JWT tokens without verifying the signature.
9
+ ✅ Verify token authenticity and integrity.
10
+ ⌛ Check if a token is expired.
9
11
 
10
12
  ## 📦 Installation
11
13
  ```sh
12
- npm install @brang/jwt-auth
14
+ npm install jwt-auths
13
15
  ```
14
16
 
15
17
  ## 🔧 Usage
16
18
  ### Import the package
17
19
  ```js
18
- const jwtAuth = require('@brang/jwt-auth');
20
+ const jwtAuth = require('jwt-auths');
19
21
  ```
20
22
 
21
23
  ### Create an Access Token
22
24
  ```js
23
- const accessToken = jwtAuth.createAccessToken({ userId: 123 }, 'your-secret-key', '1h');
24
- console.log(accessToken);
25
+ const accessToken = jwtAuth.createAccessToken('your-secret-key', { userId: 123 }, { expiresIn: '1h', algorithm: 'HS256' });
25
26
  ```
27
+ The createAccessToken function generates a new JWT access token. It now takes the secret key first, followed by the payload (your user data), and an optional options object for configuration.
28
+
26
29
  **Parameters:**
27
- - `payload` (Object) - User data to encode in the token.
28
- - `secretKey` (String) - Secret key for signing the token.
29
- - `expiresIn` (String) - Expiration time (e.g., `"1h"`, `"7d"`).
30
+ - `secretKey` (String) - The secret key used for signing the token. This should be a strong, securely stored string.
31
+ - `payload` (Object) - A JavaScript object containing the user data you want to encode in the token. It's best practice to include non-sensitive data here, such as `userId`, `role`, or `username`.
32
+ - `options` (Object, optional) - An object to customize the token's properties. If not provided, the default options will be used.
33
+ - `expiresIn` (String | Number) - The expiration time for the token (e.g., `"1h"`, `"7d"`, or `3600` for 1 hour in seconds). By default, this is set to `'15m'` (15 minutes), as defined in
34
+ - `algorithm` (String) - The algorithm used to sign the token (e.g., `"HS256"`, `"RS256"`). The default algorithm is `'HS256'`.
30
35
 
31
- ### Refresh Token
36
+ #### The default options object looks like this:
32
37
  ```js
33
- const newAccessToken = jwtAuth.refreshToken(oldToken, 'your-secret-key', '1h');
34
- console.log(newAccessToken);
38
+ const defaultAccessTokenOptions = {
39
+ expiresIn: '15m',
40
+ algorithm: 'HS256',
41
+ };
42
+ ```
43
+ ### Create an Access Token
44
+ ```js
45
+ const refreshToken = jwtAuth.createRefreshToken('your-secret-key', { userId: 123 }, { expiresIn: '7d', algorithm: 'HS256' });
46
+ ```
47
+
48
+ ### Verify Access Token & Refresh Token
49
+ ```js
50
+ const payload = jwtAuth.verifyAccessToken(token, 'your-secret-key');
51
+ ```
52
+ ```js
53
+ const payload = jwtAuth.verifyRefreshToken(token, 'your-secret-key');
54
+ ```
55
+
56
+ ### Check If a Token Is Expired
57
+ ```js
58
+ const isExpired = jwtAuth.isTokenExpired(token);
59
+ ```
60
+ ### Validate JWT Format
61
+ ```js
62
+ const isValidFormat = jwtAuth.isValidJwtFormat(token);
63
+ ```
64
+ ### Decode a Token (Without Verifying)
65
+ ```js
66
+ const decoded = jwtAuth.decodeToken(token);
35
67
  ```
36
- **Parameters:**
37
- - `oldToken` (String) - Expired or near-expired token.
38
- - `secretKey` (String) - Secret key used for verification.
39
- - `expiresIn` (String) - Expiration time for the new token.
40
68
 
41
69
  ## 🛡️ Security Best Practices
42
70
  - Use strong secret keys and store them securely (e.g., environment variables).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jwt-auths",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "A fully functional JWT authentication library for securely generating, verifying, and managing JSON Web Tokens.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",