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.
- package/README.md +45 -17
- 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
|
-
|
7
|
-
|
8
|
-
|
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
|
14
|
+
npm install jwt-auths
|
13
15
|
```
|
14
16
|
|
15
17
|
## 🔧 Usage
|
16
18
|
### Import the package
|
17
19
|
```js
|
18
|
-
const jwtAuth = require('
|
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 }, '
|
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
|
-
- `
|
28
|
-
- `
|
29
|
-
- `
|
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
|
-
|
36
|
+
#### The default options object looks like this:
|
32
37
|
```js
|
33
|
-
const
|
34
|
-
|
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