jwt-auths 1.0.2 → 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 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/dist/index.d.mts CHANGED
@@ -1,5 +1,24 @@
1
- declare const createAccessToken: (jwtsecret: String, expiredDate: String | undefined, payload: any, algorithm?: String) => String;
1
+ import { JwtPayload } from 'jsonwebtoken';
2
2
 
3
- declare const createRefreshToken: (jwtsecret: String, expiredDate: String | undefined, payload: any, algorithm?: String) => String;
3
+ type Payload$1 = string | object | Buffer;
4
+ interface TokenOptions$1 {
5
+ expiresIn?: string | number;
6
+ algorithm?: Algorithm | string;
7
+ }
8
+ declare const createAccessToken: (secret: string, payload: Payload$1, options?: TokenOptions$1) => string;
4
9
 
5
- export { createAccessToken, createRefreshToken };
10
+ type Payload = string | object | Buffer;
11
+ interface TokenOptions {
12
+ expiresIn?: string | number;
13
+ algorithm?: Algorithm | string;
14
+ }
15
+ declare const createRefreshToken: (secret: string, payload: Payload, options?: TokenOptions) => string;
16
+
17
+ declare const verifyAccessToken: (token: string, secret: string) => JwtPayload | string;
18
+ declare const verifyRefreshToken: (token: string, secret: string) => JwtPayload | string;
19
+ declare const isTokenExpired: (token: string) => boolean;
20
+ declare const isValidJwtFormat: (token: string) => boolean;
21
+
22
+ declare const decodeToken: (token: string) => null | JwtPayload | string;
23
+
24
+ export { createAccessToken, createRefreshToken, decodeToken, isTokenExpired, isValidJwtFormat, verifyAccessToken, verifyRefreshToken };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,24 @@
1
- declare const createAccessToken: (jwtsecret: String, expiredDate: String | undefined, payload: any, algorithm?: String) => String;
1
+ import { JwtPayload } from 'jsonwebtoken';
2
2
 
3
- declare const createRefreshToken: (jwtsecret: String, expiredDate: String | undefined, payload: any, algorithm?: String) => String;
3
+ type Payload$1 = string | object | Buffer;
4
+ interface TokenOptions$1 {
5
+ expiresIn?: string | number;
6
+ algorithm?: Algorithm | string;
7
+ }
8
+ declare const createAccessToken: (secret: string, payload: Payload$1, options?: TokenOptions$1) => string;
4
9
 
5
- export { createAccessToken, createRefreshToken };
10
+ type Payload = string | object | Buffer;
11
+ interface TokenOptions {
12
+ expiresIn?: string | number;
13
+ algorithm?: Algorithm | string;
14
+ }
15
+ declare const createRefreshToken: (secret: string, payload: Payload, options?: TokenOptions) => string;
16
+
17
+ declare const verifyAccessToken: (token: string, secret: string) => JwtPayload | string;
18
+ declare const verifyRefreshToken: (token: string, secret: string) => JwtPayload | string;
19
+ declare const isTokenExpired: (token: string) => boolean;
20
+ declare const isValidJwtFormat: (token: string) => boolean;
21
+
22
+ declare const decodeToken: (token: string) => null | JwtPayload | string;
23
+
24
+ export { createAccessToken, createRefreshToken, decodeToken, isTokenExpired, isValidJwtFormat, verifyAccessToken, verifyRefreshToken };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,35 +17,84 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
21
31
  var index_exports = {};
22
32
  __export(index_exports, {
23
33
  createAccessToken: () => createAccessToken,
24
- createRefreshToken: () => createRefreshToken
34
+ createRefreshToken: () => createRefreshToken,
35
+ decodeToken: () => decodeToken,
36
+ isTokenExpired: () => isTokenExpired,
37
+ isValidJwtFormat: () => isValidJwtFormat,
38
+ verifyAccessToken: () => verifyAccessToken,
39
+ verifyRefreshToken: () => verifyRefreshToken
25
40
  });
26
41
  module.exports = __toCommonJS(index_exports);
27
42
 
28
43
  // src/cors/createToken.ts
29
44
  var jwt = require("jsonwebtoken");
30
- var createAccessToken = (jwtsecret, expiredDate = "15m", payload, algorithm = "HS256") => {
31
- return jwt.sign(payload, jwtsecret, {
32
- algorithm,
33
- expiresIn: expiredDate
45
+ var defaultAccessTokenOptions = {
46
+ expiresIn: "15m",
47
+ algorithm: "HS256"
48
+ };
49
+ var createAccessToken = (secret, payload, options = defaultAccessTokenOptions) => {
50
+ return jwt.sign(payload, secret, {
51
+ algorithm: options.algorithm,
52
+ expiresIn: options.expiresIn
34
53
  });
35
54
  };
36
55
 
37
56
  // src/cors/refreshToken.ts
38
57
  var jwt2 = require("jsonwebtoken");
39
- var createRefreshToken = (jwtsecret, expiredDate = "7d", payload, algorithm = "HS256") => {
40
- return jwt2.sign(payload, jwtsecret, {
41
- algorithm,
42
- expiresIn: expiredDate
58
+ var defaultRefreshTokenOptions = {
59
+ expiresIn: "7d",
60
+ algorithm: "HS256"
61
+ };
62
+ var createRefreshToken = (secret, payload, options = defaultRefreshTokenOptions) => {
63
+ return jwt2.sign(payload, secret, {
64
+ algorithm: options.algorithm,
65
+ expiresIn: options.expiresIn
43
66
  });
44
67
  };
68
+
69
+ // src/cors/verifyToken.ts
70
+ var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
71
+ var verifyAccessToken = (token, secret) => {
72
+ return import_jsonwebtoken.default.verify(token, secret);
73
+ };
74
+ var verifyRefreshToken = (token, secret) => {
75
+ return import_jsonwebtoken.default.verify(token, secret);
76
+ };
77
+ var isTokenExpired = (token) => {
78
+ const decoded = import_jsonwebtoken.default.decode(token);
79
+ if (!decoded?.exp) return true;
80
+ return decoded.exp * 1e3 < Date.now();
81
+ };
82
+ var isValidJwtFormat = (token) => {
83
+ return /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/.test(token);
84
+ };
85
+
86
+ // src/cors/decodeToken.ts
87
+ var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"));
88
+ var decodeToken = (token) => {
89
+ return import_jsonwebtoken2.default.decode(token);
90
+ };
45
91
  // Annotate the CommonJS export names for ESM import in node:
46
92
  0 && (module.exports = {
47
93
  createAccessToken,
48
- createRefreshToken
94
+ createRefreshToken,
95
+ decodeToken,
96
+ isTokenExpired,
97
+ isValidJwtFormat,
98
+ verifyAccessToken,
99
+ verifyRefreshToken
49
100
  });
package/dist/index.mjs CHANGED
@@ -7,22 +7,58 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
7
7
 
8
8
  // src/cors/createToken.ts
9
9
  var jwt = __require("jsonwebtoken");
10
- var createAccessToken = (jwtsecret, expiredDate = "15m", payload, algorithm = "HS256") => {
11
- return jwt.sign(payload, jwtsecret, {
12
- algorithm,
13
- expiresIn: expiredDate
10
+ var defaultAccessTokenOptions = {
11
+ expiresIn: "15m",
12
+ algorithm: "HS256"
13
+ };
14
+ var createAccessToken = (secret, payload, options = defaultAccessTokenOptions) => {
15
+ return jwt.sign(payload, secret, {
16
+ algorithm: options.algorithm,
17
+ expiresIn: options.expiresIn
14
18
  });
15
19
  };
16
20
 
17
21
  // src/cors/refreshToken.ts
18
22
  var jwt2 = __require("jsonwebtoken");
19
- var createRefreshToken = (jwtsecret, expiredDate = "7d", payload, algorithm = "HS256") => {
20
- return jwt2.sign(payload, jwtsecret, {
21
- algorithm,
22
- expiresIn: expiredDate
23
+ var defaultRefreshTokenOptions = {
24
+ expiresIn: "7d",
25
+ algorithm: "HS256"
26
+ };
27
+ var createRefreshToken = (secret, payload, options = defaultRefreshTokenOptions) => {
28
+ return jwt2.sign(payload, secret, {
29
+ algorithm: options.algorithm,
30
+ expiresIn: options.expiresIn
23
31
  });
24
32
  };
33
+
34
+ // src/cors/verifyToken.ts
35
+ import jwt3 from "jsonwebtoken";
36
+ var verifyAccessToken = (token, secret) => {
37
+ return jwt3.verify(token, secret);
38
+ };
39
+ var verifyRefreshToken = (token, secret) => {
40
+ return jwt3.verify(token, secret);
41
+ };
42
+ var isTokenExpired = (token) => {
43
+ const decoded = jwt3.decode(token);
44
+ if (!decoded?.exp) return true;
45
+ return decoded.exp * 1e3 < Date.now();
46
+ };
47
+ var isValidJwtFormat = (token) => {
48
+ return /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/.test(token);
49
+ };
50
+
51
+ // src/cors/decodeToken.ts
52
+ import jwt4 from "jsonwebtoken";
53
+ var decodeToken = (token) => {
54
+ return jwt4.decode(token);
55
+ };
25
56
  export {
26
57
  createAccessToken,
27
- createRefreshToken
58
+ createRefreshToken,
59
+ decodeToken,
60
+ isTokenExpired,
61
+ isValidJwtFormat,
62
+ verifyAccessToken,
63
+ verifyRefreshToken
28
64
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jwt-auths",
3
- "version": "1.0.2",
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",
@@ -1,8 +1,20 @@
1
1
  const jwt = require('jsonwebtoken');
2
2
 
3
- export const createAccessToken = (jwtsecret: String, expiredDate: String = '15m', payload: any, algorithm:String = 'HS256'):String => {
4
- return jwt.sign(payload, jwtsecret, {
5
- algorithm: algorithm,
6
- expiresIn: expiredDate,
7
- });
8
- }
3
+ type Payload = string | object | Buffer;
4
+
5
+ interface TokenOptions {
6
+ expiresIn?: string | number;
7
+ algorithm?: Algorithm | string;
8
+ }
9
+
10
+ const defaultAccessTokenOptions: TokenOptions = {
11
+ expiresIn: '15m',
12
+ algorithm: 'HS256',
13
+ };
14
+
15
+ export const createAccessToken = (secret: string, payload: Payload, options: TokenOptions = defaultAccessTokenOptions): string => {
16
+ return jwt.sign(payload, secret, {
17
+ algorithm: options.algorithm,
18
+ expiresIn: options.expiresIn,
19
+ });
20
+ };
@@ -0,0 +1,5 @@
1
+ import jwt, { JwtPayload } from 'jsonwebtoken';
2
+
3
+ export const decodeToken = (token: string): null | JwtPayload | string => {
4
+ return jwt.decode(token);
5
+ };
@@ -1,8 +1,20 @@
1
1
  const jwt = require('jsonwebtoken');
2
2
 
3
- export const createRefreshToken = (jwtsecret: String, expiredDate: String = '7d', payload: any, algorithm:String = 'HS256'):String => {
4
- return jwt.sign(payload, jwtsecret, {
5
- algorithm: algorithm,
6
- expiresIn: expiredDate,
7
- });
8
- }
3
+ type Payload = string | object | Buffer;
4
+
5
+ interface TokenOptions {
6
+ expiresIn?: string | number;
7
+ algorithm?: Algorithm | string;
8
+ }
9
+
10
+ const defaultRefreshTokenOptions: TokenOptions = {
11
+ expiresIn: '7d',
12
+ algorithm: 'HS256',
13
+ };
14
+
15
+ export const createRefreshToken = (secret: string, payload: Payload,options: TokenOptions = defaultRefreshTokenOptions): string => {
16
+ return jwt.sign(payload, secret, {
17
+ algorithm: options.algorithm,
18
+ expiresIn: options.expiresIn,
19
+ });
20
+ };
@@ -0,0 +1,26 @@
1
+ import jwt, { JwtPayload } from 'jsonwebtoken';
2
+
3
+ export const verifyAccessToken = (
4
+ token: string,
5
+ secret: string
6
+ ): JwtPayload | string => {
7
+ return jwt.verify(token, secret);
8
+ };
9
+
10
+ export const verifyRefreshToken = (
11
+ token: string,
12
+ secret: string
13
+ ): JwtPayload | string => {
14
+ return jwt.verify(token, secret);
15
+ };
16
+
17
+ export const isTokenExpired = (token: string): boolean => {
18
+ const decoded = jwt.decode(token) as JwtPayload | null;
19
+ if (!decoded?.exp) return true;
20
+ return decoded.exp * 1000 < Date.now();
21
+ };
22
+
23
+ export const isValidJwtFormat = (token: string): boolean => {
24
+ return /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/.test(token);
25
+ };
26
+
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './cors/createToken';
2
- export * from './cors/refreshToken';
2
+ export * from './cors/refreshToken';
3
+ export * from './cors/verifyToken';
4
+ export * from './cors/decodeToken';