@forgepack/request 1.0.4 → 1.0.6
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 +217 -86
- package/dist/api/client.d.ts +17 -10
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +18 -6
- package/dist/hooks/AuthContext.d.ts +6 -6
- package/dist/hooks/AuthContext.d.ts.map +1 -1
- package/dist/hooks/AuthContext.js +1 -1
- package/dist/hooks/AuthProvider.d.ts +34 -17
- package/dist/hooks/AuthProvider.d.ts.map +1 -1
- package/dist/hooks/AuthProvider.js +26 -15
- package/dist/hooks/useAuth.d.ts +33 -12
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +30 -12
- package/dist/hooks/useRequest.d.ts +37 -17
- package/dist/hooks/useRequest.d.ts.map +1 -1
- package/dist/hooks/useRequest.js +36 -13
- package/dist/index.d.ts +159 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +159 -14
- package/dist/services/api.d.ts +37 -15
- package/dist/services/api.d.ts.map +1 -1
- package/dist/services/api.js +40 -14
- package/dist/services/auth.d.ts +76 -19
- package/dist/services/auth.d.ts.map +1 -1
- package/dist/services/auth.js +94 -22
- package/dist/services/crud.d.ts +115 -62
- package/dist/services/crud.d.ts.map +1 -1
- package/dist/services/crud.js +132 -85
- package/dist/services/token.d.ts +83 -26
- package/dist/services/token.d.ts.map +1 -1
- package/dist/services/token.js +141 -52
- package/dist/types/auth.d.ts +20 -20
- package/dist/types/auth.d.ts.map +1 -1
- package/dist/types/error.d.ts +3 -3
- package/dist/types/error.d.ts.map +1 -1
- package/dist/types/request.d.ts +8 -8
- package/dist/types/request.d.ts.map +1 -1
- package/dist/types/response.d.ts +15 -15
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/token.d.ts +15 -15
- package/dist/types/token.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +7 -7
- package/dist/utils/constants.js +7 -7
- package/package.json +70 -56
- package/dist/api/api.d.ts +0 -8
- package/dist/api/api.d.ts.map +0 -1
- package/dist/api/api.js +0 -35
- package/dist/component/auth.d.ts +0 -8
- package/dist/component/auth.d.ts.map +0 -1
- package/dist/component/auth.js +0 -9
- package/dist/component/errorMessage.d.ts +0 -6
- package/dist/component/errorMessage.d.ts.map +0 -1
- package/dist/component/errorMessage.js +0 -7
- package/dist/component/request.d.ts +0 -13
- package/dist/component/request.d.ts.map +0 -1
- package/dist/component/request.js +0 -12
- package/dist/component/response.d.ts +0 -13
- package/dist/component/response.d.ts.map +0 -1
- package/dist/component/response.js +0 -13
- package/dist/component/token.d.ts +0 -32
- package/dist/component/token.d.ts.map +0 -1
- package/dist/component/token.js +0 -16
- package/dist/hook/useProvider.d.ts +0 -13
- package/dist/hook/useProvider.d.ts.map +0 -1
- package/dist/hook/useProvider.js +0 -51
- package/dist/hook/useRequest.d.ts +0 -11
- package/dist/hook/useRequest.d.ts.map +0 -1
- package/dist/hook/useRequest.js +0 -38
- package/dist/hook/useRequireAuth.d.ts +0 -4
- package/dist/hook/useRequireAuth.d.ts.map +0 -1
- package/dist/hook/useRequireAuth.js +0 -21
- package/dist/service/crud.d.ts +0 -15
- package/dist/service/crud.d.ts.map +0 -1
- package/dist/service/crud.js +0 -116
- package/dist/service/fetchPage.d.ts +0 -5
- package/dist/service/fetchPage.d.ts.map +0 -1
- package/dist/service/fetchPage.js +0 -22
- package/dist/service/token.d.ts +0 -10
- package/dist/service/token.d.ts.map +0 -1
- package/dist/service/token.js +0 -93
package/dist/services/token.d.ts
CHANGED
|
@@ -1,87 +1,144 @@
|
|
|
1
1
|
import { Auth } from '../types/auth';
|
|
2
2
|
import { Header, Payload } from '../types/token';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Verifies if the current JWT token stored in localStorage is valid and not expired
|
|
5
5
|
*
|
|
6
|
-
* @returns true
|
|
6
|
+
* @returns {boolean} true if token exists, is valid and not expired; false otherwise
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```typescript
|
|
10
10
|
* if (isValidToken()) {
|
|
11
|
-
* //
|
|
11
|
+
* // Allow access
|
|
12
|
+
* console.log('User is authenticated')
|
|
12
13
|
* } else {
|
|
13
|
-
*
|
|
14
|
+
* console.log('Session expired or invalid')
|
|
15
|
+
* window.location.href = '/login'
|
|
14
16
|
* }
|
|
15
17
|
* ```
|
|
16
18
|
*/
|
|
17
19
|
export declare const isValidToken: () => boolean;
|
|
18
20
|
/**
|
|
19
|
-
*
|
|
21
|
+
* Retrieves the JWT token stored in localStorage
|
|
20
22
|
*
|
|
21
|
-
* @returns
|
|
23
|
+
* @returns {Auth} Auth object with token data, or initialAuth if token is missing/invalid
|
|
22
24
|
*
|
|
23
25
|
* @example
|
|
24
26
|
* ```typescript
|
|
27
|
+
* // Basic retrieval
|
|
25
28
|
* const token = getToken()
|
|
26
|
-
* console.log(
|
|
29
|
+
* console.log(auth.accessToken) // JWT string
|
|
30
|
+
* console.log(auth.tokenType) // 'Bearer'
|
|
31
|
+
* console.log(auth.role) // ['USER', 'ADMIN']
|
|
27
32
|
* ```
|
|
28
33
|
*/
|
|
29
34
|
export declare const getToken: () => Auth;
|
|
30
35
|
/**
|
|
31
|
-
*
|
|
36
|
+
* Stores JWT token and authentication data in localStorage
|
|
32
37
|
*
|
|
33
|
-
* @param token -
|
|
38
|
+
* @param {Auth} token - Authentication data to be stored
|
|
39
|
+
* @param {string} token.accessToken - JWT access token
|
|
40
|
+
* @param {string} [token.refreshToken] - Optional refresh token
|
|
41
|
+
* @param {string} token.tokenType - Token type (usually 'Bearer')
|
|
42
|
+
* @param {string[]} [token.role] - User roles/permissions
|
|
43
|
+
* @returns {void}
|
|
34
44
|
*
|
|
35
45
|
* @example
|
|
36
46
|
* ```typescript
|
|
47
|
+
* // After successful login
|
|
48
|
+
* const loginResponse = await api.post('/auth/login', credentials)
|
|
49
|
+
*
|
|
37
50
|
* setToken({
|
|
38
|
-
* accessToken:
|
|
39
|
-
* refreshToken:
|
|
51
|
+
* accessToken: loginResponse.data.accessToken,
|
|
52
|
+
* refreshToken: loginResponse.data.refreshToken,
|
|
40
53
|
* tokenType: 'Bearer',
|
|
41
|
-
* role: ['USER']
|
|
54
|
+
* role: ['USER', 'ADMIN']
|
|
42
55
|
* })
|
|
43
56
|
* ```
|
|
44
57
|
*/
|
|
45
|
-
export declare const setToken: (token:
|
|
58
|
+
export declare const setToken: (token: Auth) => void;
|
|
46
59
|
/**
|
|
47
|
-
*
|
|
60
|
+
* Removes the JWT token from localStorage, effectively logging out the user
|
|
61
|
+
*
|
|
62
|
+
* @returns {void}
|
|
48
63
|
*
|
|
49
64
|
* @example
|
|
50
65
|
* ```typescript
|
|
51
|
-
*
|
|
66
|
+
* // Simple logout
|
|
67
|
+
* removeToken()
|
|
68
|
+
* window.location.href = '/login'
|
|
69
|
+
*
|
|
70
|
+
* // Complete logout with backend call
|
|
71
|
+
* const handleLogout = async () => {
|
|
72
|
+
* try {
|
|
73
|
+
* await api.post('/auth/logout')
|
|
74
|
+
* } finally {
|
|
75
|
+
* removeToken()
|
|
76
|
+
* navigate('/login')
|
|
77
|
+
* }
|
|
78
|
+
* }
|
|
52
79
|
* ```
|
|
53
80
|
*/
|
|
54
81
|
export declare const removeToken: () => void;
|
|
55
82
|
/**
|
|
56
|
-
*
|
|
83
|
+
* Decodes and returns the JWT token payload (claims)
|
|
57
84
|
*
|
|
58
|
-
*
|
|
85
|
+
* The payload contains user information and token metadata such as:
|
|
86
|
+
* - sub: Subject (user ID)
|
|
87
|
+
* - exp: Expiration timestamp (seconds since epoch)
|
|
88
|
+
* - iat: Issued at timestamp
|
|
89
|
+
* - Custom claims (email, name, roles, etc.)
|
|
90
|
+
*
|
|
91
|
+
* @returns {Payload} Decoded payload object, or initialPayload if token is missing/invalid
|
|
59
92
|
*
|
|
60
93
|
* @example
|
|
61
94
|
* ```typescript
|
|
95
|
+
* // Get user information from token
|
|
96
|
+
* const payload = getPayload()
|
|
97
|
+
* console.log(payload.exp) // Expiration: 1735689600
|
|
98
|
+
* console.log(payload.iat) // Issued at: 1735603200
|
|
99
|
+
* // Get time until expiration
|
|
100
|
+
* const payload = getPayload()
|
|
101
|
+
* const timeLeft = payload.exp * 1000 - Date.now()
|
|
102
|
+
* const minutesLeft = Math.floor(timeLeft / 60000)
|
|
103
|
+
* console.log(`Token expires in ${minutesLeft} minutes`)
|
|
104
|
+
* // Access custom claims
|
|
62
105
|
* const payload = getPayload()
|
|
63
|
-
*
|
|
64
|
-
* console.log(payload.sub) // Subject (ID do usuário)
|
|
106
|
+
* const userRoles = payload.roles || []
|
|
65
107
|
* ```
|
|
66
108
|
*/
|
|
67
109
|
export declare const getPayload: () => Payload;
|
|
68
110
|
/**
|
|
69
|
-
*
|
|
111
|
+
* Decodes the JWT token and returns the payload as a JSON string
|
|
70
112
|
*
|
|
71
|
-
* @deprecated Use getPayload()
|
|
72
|
-
* @returns
|
|
113
|
+
* @deprecated Use getPayload() instead for type-safe access to payload data
|
|
114
|
+
* @returns {string | null} Decoded payload as JSON string, or null if token is invalid
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* // ❌ Deprecated approach
|
|
119
|
+
* const payloadString = decodeJwt()
|
|
120
|
+
* if (payloadString) {
|
|
121
|
+
* const payload = JSON.parse(payloadString)
|
|
122
|
+
* console.log(payload.exp)
|
|
123
|
+
* }
|
|
124
|
+
*
|
|
125
|
+
* // ✅ Use this instead
|
|
126
|
+
* const payload = getPayload()
|
|
127
|
+
* console.log(payload.exp)
|
|
128
|
+
* ```
|
|
73
129
|
*/
|
|
74
130
|
export declare const decodeJwt: () => string | null;
|
|
75
131
|
/**
|
|
76
|
-
*
|
|
132
|
+
* Decodes and returns the JWT token header
|
|
77
133
|
*
|
|
78
|
-
* @returns
|
|
134
|
+
* @returns {Header} Decoded header object, or initialHeader if token is missing/invalid
|
|
79
135
|
*
|
|
80
136
|
* @example
|
|
81
137
|
* ```typescript
|
|
138
|
+
* // Check signing algorithm
|
|
82
139
|
* const header = getHeader()
|
|
83
|
-
* console.log(header.alg)
|
|
84
|
-
* console.log(header.typ)
|
|
140
|
+
* console.log(header.alg) // 'HS256'
|
|
141
|
+
* console.log(header.typ) // 'JWT'
|
|
85
142
|
* ```
|
|
86
143
|
*/
|
|
87
144
|
export declare const getHeader: () => Header;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/services/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AAEpC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/services/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AAEpC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AA0BhD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,YAAY,QAAO,OAa/B,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,QAAQ,QAAO,IAG3B,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,IAAI,KAAG,IAEtC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,WAAW,YAEvB,CAAA;AA6CD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,UAAU,QAAO,OAO7B,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,SAAS,QAAO,MAAM,GAAG,IAYrC,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,SAAS,QAAO,MAO5B,CAAA"}
|
package/dist/services/token.js
CHANGED
|
@@ -3,10 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getHeader = exports.decodeJwt = exports.getPayload = exports.removeToken = exports.setToken = exports.getToken = exports.isValidToken = void 0;
|
|
4
4
|
const constants_1 = require("../utils/constants");
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Verifies if a string is valid JSON
|
|
7
7
|
*
|
|
8
|
-
* @param json - String
|
|
9
|
-
* @returns true
|
|
8
|
+
* @param {string} json - String to be validated as JSON
|
|
9
|
+
* @returns {boolean} true if the string is valid JSON, false otherwise
|
|
10
|
+
*
|
|
11
|
+
* @internal This is a utility function used internally by token operations
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* isValidJSON('{"key": "value"}') // true
|
|
16
|
+
* isValidJSON('invalid json') // false
|
|
17
|
+
* isValidJSON('') // false
|
|
18
|
+
* ```
|
|
10
19
|
*/
|
|
11
20
|
const isValidJSON = (json) => {
|
|
12
21
|
try {
|
|
@@ -18,16 +27,18 @@ const isValidJSON = (json) => {
|
|
|
18
27
|
}
|
|
19
28
|
};
|
|
20
29
|
/**
|
|
21
|
-
*
|
|
30
|
+
* Verifies if the current JWT token stored in localStorage is valid and not expired
|
|
22
31
|
*
|
|
23
|
-
* @returns true
|
|
32
|
+
* @returns {boolean} true if token exists, is valid and not expired; false otherwise
|
|
24
33
|
*
|
|
25
34
|
* @example
|
|
26
35
|
* ```typescript
|
|
27
36
|
* if (isValidToken()) {
|
|
28
|
-
* //
|
|
37
|
+
* // Allow access
|
|
38
|
+
* console.log('User is authenticated')
|
|
29
39
|
* } else {
|
|
30
|
-
*
|
|
40
|
+
* console.log('Session expired or invalid')
|
|
41
|
+
* window.location.href = '/login'
|
|
31
42
|
* }
|
|
32
43
|
* ```
|
|
33
44
|
*/
|
|
@@ -39,6 +50,7 @@ const isValidToken = () => {
|
|
|
39
50
|
const { exp } = (0, exports.getPayload)();
|
|
40
51
|
if (typeof exp !== 'number')
|
|
41
52
|
return false;
|
|
53
|
+
/** exp is in seconds, Date.now() is in milliseconds */
|
|
42
54
|
return exp * 1000 > Date.now();
|
|
43
55
|
}
|
|
44
56
|
catch {
|
|
@@ -47,14 +59,17 @@ const isValidToken = () => {
|
|
|
47
59
|
};
|
|
48
60
|
exports.isValidToken = isValidToken;
|
|
49
61
|
/**
|
|
50
|
-
*
|
|
62
|
+
* Retrieves the JWT token stored in localStorage
|
|
51
63
|
*
|
|
52
|
-
* @returns
|
|
64
|
+
* @returns {Auth} Auth object with token data, or initialAuth if token is missing/invalid
|
|
53
65
|
*
|
|
54
66
|
* @example
|
|
55
67
|
* ```typescript
|
|
68
|
+
* // Basic retrieval
|
|
56
69
|
* const token = getToken()
|
|
57
|
-
* console.log(
|
|
70
|
+
* console.log(auth.accessToken) // JWT string
|
|
71
|
+
* console.log(auth.tokenType) // 'Bearer'
|
|
72
|
+
* console.log(auth.role) // ['USER', 'ADMIN']
|
|
58
73
|
* ```
|
|
59
74
|
*/
|
|
60
75
|
const getToken = () => {
|
|
@@ -63,17 +78,25 @@ const getToken = () => {
|
|
|
63
78
|
};
|
|
64
79
|
exports.getToken = getToken;
|
|
65
80
|
/**
|
|
66
|
-
*
|
|
81
|
+
* Stores JWT token and authentication data in localStorage
|
|
67
82
|
*
|
|
68
|
-
* @param token -
|
|
83
|
+
* @param {Auth} token - Authentication data to be stored
|
|
84
|
+
* @param {string} token.accessToken - JWT access token
|
|
85
|
+
* @param {string} [token.refreshToken] - Optional refresh token
|
|
86
|
+
* @param {string} token.tokenType - Token type (usually 'Bearer')
|
|
87
|
+
* @param {string[]} [token.role] - User roles/permissions
|
|
88
|
+
* @returns {void}
|
|
69
89
|
*
|
|
70
90
|
* @example
|
|
71
91
|
* ```typescript
|
|
92
|
+
* // After successful login
|
|
93
|
+
* const loginResponse = await api.post('/auth/login', credentials)
|
|
94
|
+
*
|
|
72
95
|
* setToken({
|
|
73
|
-
* accessToken:
|
|
74
|
-
* refreshToken:
|
|
96
|
+
* accessToken: loginResponse.data.accessToken,
|
|
97
|
+
* refreshToken: loginResponse.data.refreshToken,
|
|
75
98
|
* tokenType: 'Bearer',
|
|
76
|
-
* role: ['USER']
|
|
99
|
+
* role: ['USER', 'ADMIN']
|
|
77
100
|
* })
|
|
78
101
|
* ```
|
|
79
102
|
*/
|
|
@@ -82,11 +105,25 @@ const setToken = (token) => {
|
|
|
82
105
|
};
|
|
83
106
|
exports.setToken = setToken;
|
|
84
107
|
/**
|
|
85
|
-
*
|
|
108
|
+
* Removes the JWT token from localStorage, effectively logging out the user
|
|
109
|
+
*
|
|
110
|
+
* @returns {void}
|
|
86
111
|
*
|
|
87
112
|
* @example
|
|
88
113
|
* ```typescript
|
|
89
|
-
*
|
|
114
|
+
* // Simple logout
|
|
115
|
+
* removeToken()
|
|
116
|
+
* window.location.href = '/login'
|
|
117
|
+
*
|
|
118
|
+
* // Complete logout with backend call
|
|
119
|
+
* const handleLogout = async () => {
|
|
120
|
+
* try {
|
|
121
|
+
* await api.post('/auth/logout')
|
|
122
|
+
* } finally {
|
|
123
|
+
* removeToken()
|
|
124
|
+
* navigate('/login')
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
90
127
|
* ```
|
|
91
128
|
*/
|
|
92
129
|
const removeToken = () => {
|
|
@@ -94,43 +131,104 @@ const removeToken = () => {
|
|
|
94
131
|
};
|
|
95
132
|
exports.removeToken = removeToken;
|
|
96
133
|
/**
|
|
97
|
-
*
|
|
134
|
+
* Decodes a Base64URL-encoded JWT segment (header or payload)
|
|
135
|
+
*
|
|
136
|
+
* Handles the complete decoding process:
|
|
137
|
+
* 1. Converts Base64URL to standard Base64
|
|
138
|
+
* 2. Decodes Base64 to binary
|
|
139
|
+
* 3. Properly handles Unicode characters via percent-encoding
|
|
140
|
+
* 4. Parses JSON if valid
|
|
98
141
|
*
|
|
99
|
-
* @
|
|
142
|
+
* @template T - Type of the decoded object (Header or Payload)
|
|
143
|
+
* @param {string} segment - Base64URL-encoded JWT segment
|
|
144
|
+
* @param {T} fallback - Fallback value if decoding fails
|
|
145
|
+
* @returns {T} Decoded and parsed object, or fallback if invalid
|
|
146
|
+
*
|
|
147
|
+
* @internal This is a utility function used internally by getHeader and getPayload
|
|
100
148
|
*
|
|
101
149
|
* @example
|
|
102
150
|
* ```typescript
|
|
103
|
-
* const
|
|
104
|
-
*
|
|
105
|
-
* console.log(payload.sub) // Subject (ID do usuário)
|
|
151
|
+
* const header = decodeJwtSegment(token.split('.')[0], initialHeader)
|
|
152
|
+
* const payload = decodeJwtSegment(token.split('.')[1], initialPayload)
|
|
106
153
|
* ```
|
|
107
154
|
*/
|
|
108
|
-
const
|
|
155
|
+
const decodeJwtSegment = (segment, fallback) => {
|
|
109
156
|
try {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return constants_1.initialPayload;
|
|
113
|
-
const base64 = token.accessToken.split('.')[1]
|
|
157
|
+
/** Convert Base64URL to Base64 */
|
|
158
|
+
const base64 = segment
|
|
114
159
|
.replace(/-/g, '+')
|
|
115
160
|
.replace(/_/g, '/');
|
|
116
|
-
|
|
161
|
+
/** Decode Base64 and handle Unicode characters properly */
|
|
162
|
+
const decoded = decodeURIComponent(atob(base64)
|
|
117
163
|
.split('')
|
|
118
164
|
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
|
119
165
|
.join(''));
|
|
120
|
-
return isValidJSON(
|
|
166
|
+
return isValidJSON(decoded) ? JSON.parse(decoded) : fallback;
|
|
121
167
|
}
|
|
122
168
|
catch {
|
|
123
|
-
return
|
|
169
|
+
return fallback;
|
|
124
170
|
}
|
|
125
171
|
};
|
|
172
|
+
/**
|
|
173
|
+
* Decodes and returns the JWT token payload (claims)
|
|
174
|
+
*
|
|
175
|
+
* The payload contains user information and token metadata such as:
|
|
176
|
+
* - sub: Subject (user ID)
|
|
177
|
+
* - exp: Expiration timestamp (seconds since epoch)
|
|
178
|
+
* - iat: Issued at timestamp
|
|
179
|
+
* - Custom claims (email, name, roles, etc.)
|
|
180
|
+
*
|
|
181
|
+
* @returns {Payload} Decoded payload object, or initialPayload if token is missing/invalid
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* // Get user information from token
|
|
186
|
+
* const payload = getPayload()
|
|
187
|
+
* console.log(payload.exp) // Expiration: 1735689600
|
|
188
|
+
* console.log(payload.iat) // Issued at: 1735603200
|
|
189
|
+
* // Get time until expiration
|
|
190
|
+
* const payload = getPayload()
|
|
191
|
+
* const timeLeft = payload.exp * 1000 - Date.now()
|
|
192
|
+
* const minutesLeft = Math.floor(timeLeft / 60000)
|
|
193
|
+
* console.log(`Token expires in ${minutesLeft} minutes`)
|
|
194
|
+
* // Access custom claims
|
|
195
|
+
* const payload = getPayload()
|
|
196
|
+
* const userRoles = payload.roles || []
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
const getPayload = () => {
|
|
200
|
+
const token = (0, exports.getToken)();
|
|
201
|
+
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
202
|
+
return constants_1.initialPayload;
|
|
203
|
+
/** Extract the payload part of the JWT */
|
|
204
|
+
const payloadSegment = token.accessToken.split('.')[1];
|
|
205
|
+
return decodeJwtSegment(payloadSegment, constants_1.initialPayload);
|
|
206
|
+
};
|
|
126
207
|
exports.getPayload = getPayload;
|
|
127
208
|
/**
|
|
128
|
-
*
|
|
209
|
+
* Decodes the JWT token and returns the payload as a JSON string
|
|
210
|
+
*
|
|
211
|
+
* @deprecated Use getPayload() instead for type-safe access to payload data
|
|
212
|
+
* @returns {string | null} Decoded payload as JSON string, or null if token is invalid
|
|
129
213
|
*
|
|
130
|
-
* @
|
|
131
|
-
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* // ❌ Deprecated approach
|
|
217
|
+
* const payloadString = decodeJwt()
|
|
218
|
+
* if (payloadString) {
|
|
219
|
+
* const payload = JSON.parse(payloadString)
|
|
220
|
+
* console.log(payload.exp)
|
|
221
|
+
* }
|
|
222
|
+
*
|
|
223
|
+
* // ✅ Use this instead
|
|
224
|
+
* const payload = getPayload()
|
|
225
|
+
* console.log(payload.exp)
|
|
226
|
+
* ```
|
|
132
227
|
*/
|
|
133
228
|
const decodeJwt = () => {
|
|
229
|
+
const token = (0, exports.getToken)();
|
|
230
|
+
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
231
|
+
return null;
|
|
134
232
|
if ((0, exports.getToken)() !== null) {
|
|
135
233
|
var base64Url = (0, exports.getToken)().accessToken.split('.')[1];
|
|
136
234
|
var base64 = decodeURIComponent(atob(base64Url).split('').map((c) => {
|
|
@@ -144,33 +242,24 @@ const decodeJwt = () => {
|
|
|
144
242
|
};
|
|
145
243
|
exports.decodeJwt = decodeJwt;
|
|
146
244
|
/**
|
|
147
|
-
*
|
|
245
|
+
* Decodes and returns the JWT token header
|
|
148
246
|
*
|
|
149
|
-
* @returns
|
|
247
|
+
* @returns {Header} Decoded header object, or initialHeader if token is missing/invalid
|
|
150
248
|
*
|
|
151
249
|
* @example
|
|
152
250
|
* ```typescript
|
|
251
|
+
* // Check signing algorithm
|
|
153
252
|
* const header = getHeader()
|
|
154
|
-
* console.log(header.alg)
|
|
155
|
-
* console.log(header.typ)
|
|
253
|
+
* console.log(header.alg) // 'HS256'
|
|
254
|
+
* console.log(header.typ) // 'JWT'
|
|
156
255
|
* ```
|
|
157
256
|
*/
|
|
158
257
|
const getHeader = () => {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
162
|
-
return constants_1.initialHeader;
|
|
163
|
-
const base64 = token.accessToken.split('.')[0]
|
|
164
|
-
.replace(/-/g, '+')
|
|
165
|
-
.replace(/_/g, '/');
|
|
166
|
-
const header = decodeURIComponent(atob(base64)
|
|
167
|
-
.split('')
|
|
168
|
-
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
|
169
|
-
.join(''));
|
|
170
|
-
return isValidJSON(header) ? JSON.parse(header) : constants_1.initialHeader;
|
|
171
|
-
}
|
|
172
|
-
catch {
|
|
258
|
+
const token = (0, exports.getToken)();
|
|
259
|
+
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
173
260
|
return constants_1.initialHeader;
|
|
174
|
-
|
|
261
|
+
/** Extract the header part of the JWT */
|
|
262
|
+
const headerSegment = token.accessToken.split('.')[0];
|
|
263
|
+
return decodeJwtSegment(headerSegment, constants_1.initialHeader);
|
|
175
264
|
};
|
|
176
265
|
exports.getHeader = getHeader;
|
package/dist/types/auth.d.ts
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Interface
|
|
2
|
+
* Interface representing user authentication data
|
|
3
3
|
* @interface Auth
|
|
4
4
|
*/
|
|
5
5
|
export interface Auth {
|
|
6
|
-
/**
|
|
6
|
+
/** JWT access token for authentication */
|
|
7
7
|
readonly accessToken: string;
|
|
8
|
-
/** Token
|
|
8
|
+
/** Token for session renewal */
|
|
9
9
|
refreshToken: string;
|
|
10
|
-
/**
|
|
10
|
+
/** Token type (usually "Bearer") */
|
|
11
11
|
tokenType: string;
|
|
12
|
-
/**
|
|
12
|
+
/** List of user roles/permissions */
|
|
13
13
|
role: string[];
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Interface
|
|
16
|
+
* Interface for login credentials
|
|
17
17
|
* @interface LoginCredentials
|
|
18
18
|
*/
|
|
19
19
|
export interface LoginCredentials {
|
|
20
|
-
/**
|
|
20
|
+
/** Username or email */
|
|
21
21
|
username: string;
|
|
22
|
-
/**
|
|
22
|
+
/** User password */
|
|
23
23
|
password: string;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* Interface
|
|
26
|
+
* Interface for login response
|
|
27
27
|
* @interface LoginResponse
|
|
28
28
|
*/
|
|
29
29
|
export interface LoginResponse {
|
|
30
|
-
/**
|
|
30
|
+
/** Indicates if login was successful */
|
|
31
31
|
success: boolean;
|
|
32
|
-
/**
|
|
32
|
+
/** Authentication data (if successful) */
|
|
33
33
|
data?: Auth;
|
|
34
|
-
/**
|
|
34
|
+
/** Validation errors (if failed) */
|
|
35
35
|
errors?: Array<{
|
|
36
36
|
field: string;
|
|
37
37
|
message: string;
|
|
38
38
|
}>;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* Interface
|
|
41
|
+
* Interface for password change
|
|
42
42
|
* @interface ChangePasswordData
|
|
43
43
|
*/
|
|
44
44
|
export interface ChangePasswordData {
|
|
45
|
-
/**
|
|
45
|
+
/** Current password */
|
|
46
46
|
currentPassword: string;
|
|
47
|
-
/**
|
|
47
|
+
/** New password */
|
|
48
48
|
newPassword: string;
|
|
49
|
-
/**
|
|
49
|
+
/** New password confirmation */
|
|
50
50
|
confirmPassword?: string;
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
53
|
+
* Generic interface for password reset
|
|
54
54
|
* @interface ResetPasswordData
|
|
55
55
|
*/
|
|
56
56
|
export interface ResetPasswordData {
|
|
57
|
-
/** Email
|
|
57
|
+
/** Email for reset */
|
|
58
58
|
email?: string;
|
|
59
|
-
/**
|
|
59
|
+
/** Reset token */
|
|
60
60
|
token?: string;
|
|
61
|
-
/**
|
|
61
|
+
/** New password */
|
|
62
62
|
newPassword?: string;
|
|
63
63
|
}
|
|
64
64
|
//# sourceMappingURL=auth.d.ts.map
|
package/dist/types/auth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/types/auth.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,IAAI;IACjB,
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/types/auth.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,IAAI;IACjB,0CAA0C;IAC1C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,gCAAgC;IACnC,YAAY,EAAE,MAAM,CAAC;IAClB,oCAAoC;IACvC,SAAS,EAAE,MAAM,CAAC;IACf,qCAAqC;IACxC,IAAI,EAAE,MAAM,EAAE,CAAA;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC7B,wBAAwB;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,oBAAoB;IACpB,QAAQ,EAAE,MAAM,CAAA;CACnB;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC1B,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;IACjB,0CAA0C;IAC1C,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,oCAAoC;IACpC,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;CACrD;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IAC/B,uBAAuB;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,mBAAmB;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,gCAAgC;IAChC,eAAe,CAAC,EAAE,MAAM,CAAA;CAC3B;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAC9B,sBAAsB;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kBAAkB;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mBAAmB;IACnB,WAAW,CAAC,EAAE,MAAM,CAAA;CACvB"}
|
package/dist/types/error.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Interface
|
|
2
|
+
* Interface representing a validation error message
|
|
3
3
|
* @interface ErrorMessage
|
|
4
4
|
*/
|
|
5
5
|
export interface ErrorMessage {
|
|
6
|
-
/**
|
|
6
|
+
/** Name of the field containing the error */
|
|
7
7
|
field: string;
|
|
8
|
-
/**
|
|
8
|
+
/** Descriptive error message */
|
|
9
9
|
message: string;
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/types/error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,YAAY;IACzB,
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/types/error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,YAAY;IACzB,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,gCAAgC;IAChC,OAAO,EAAE,MAAM,CAAA;CAClB"}
|
package/dist/types/request.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Interface
|
|
2
|
+
* Interface defining sort parameters
|
|
3
3
|
* @interface Sort
|
|
4
4
|
*/
|
|
5
5
|
export interface Sort {
|
|
6
|
-
/**
|
|
6
|
+
/** Field to sort by */
|
|
7
7
|
key: string;
|
|
8
|
-
/**
|
|
8
|
+
/** Sort direction */
|
|
9
9
|
order: 'ASC' | 'DESC';
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* Interface
|
|
12
|
+
* Interface defining search and pagination parameters
|
|
13
13
|
* @interface Search
|
|
14
14
|
*/
|
|
15
15
|
export interface Search {
|
|
16
|
-
/**
|
|
16
|
+
/** Search/filter term */
|
|
17
17
|
value?: string;
|
|
18
|
-
/**
|
|
18
|
+
/** Page number (zero-based) */
|
|
19
19
|
page?: number;
|
|
20
|
-
/**
|
|
20
|
+
/** Number of items per page */
|
|
21
21
|
size?: number;
|
|
22
|
-
/**
|
|
22
|
+
/** Sort configuration */
|
|
23
23
|
sort?: Sort;
|
|
24
24
|
}
|
|
25
25
|
//# sourceMappingURL=request.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/types/request.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,IAAI;IACjB,
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/types/request.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,IAAI;IACjB,uBAAuB;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,qBAAqB;IACrB,KAAK,EAAE,KAAK,GAAG,MAAM,CAAA;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,MAAM;IACnB,yBAAyB;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+BAA+B;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,+BAA+B;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yBAAyB;IACzB,IAAI,CAAC,EAAE,IAAI,CAAA;CACd"}
|