|
2 | 2 | * API client for document generation service |
3 | 3 | */ |
4 | 4 | export class ApiClient { |
| 5 | + // Flag to avoid logging token warning multiple times |
| 6 | + static _tokenWarningLogged = false; |
| 7 | + |
| 8 | + /** |
| 9 | + * Get a valid JWT token or null if none is available |
| 10 | + * @returns {string|null} - Valid JWT token or null |
| 11 | + * @private |
| 12 | + */ |
| 13 | + static _getValidJwtToken() { |
| 14 | + // Get JWT token from localStorage |
| 15 | + const jwtToken = localStorage.getItem('jwt_token'); |
| 16 | + |
| 17 | + // Validate token to prevent sending 'null' string |
| 18 | + if (jwtToken && jwtToken !== 'null' && jwtToken.length > 50) { |
| 19 | + return jwtToken; |
| 20 | + } |
| 21 | + |
| 22 | + // Log only once per session to avoid console spam |
| 23 | + if (!ApiClient._tokenWarningLogged) { |
| 24 | + console.warn('No valid JWT token available in localStorage'); |
| 25 | + ApiClient._tokenWarningLogged = true; |
| 26 | + } |
| 27 | + |
| 28 | + return null; |
| 29 | + } |
5 | 30 | /** |
6 | 31 | * Base URL for API endpoints |
7 | 32 | * @type {string} |
@@ -128,13 +153,13 @@ export class ApiClient { |
128 | 153 | url.searchParams.append('limit', limit.toString()); |
129 | 154 | url.searchParams.append('offset', offset.toString()); |
130 | 155 |
|
131 | | - // Get JWT token from localStorage |
132 | | - const jwtToken = localStorage.getItem('jwt_token'); |
133 | | - |
134 | 156 | const headers = { |
135 | 157 | 'Accept': 'application/json', |
136 | 158 | }; |
137 | 159 |
|
| 160 | + // Get a valid JWT token using our helper method |
| 161 | + const jwtToken = ApiClient._getValidJwtToken(); |
| 162 | + |
138 | 163 | // Add Authorization header with JWT token if available |
139 | 164 | if (jwtToken) { |
140 | 165 | headers['Authorization'] = `Bearer ${jwtToken}`; |
@@ -162,13 +187,13 @@ export class ApiClient { |
162 | 187 | */ |
163 | 188 | static async createSubscription(email, plan, coin) { |
164 | 189 | try { |
165 | | - // Get JWT token from localStorage |
166 | | - const jwtToken = localStorage.getItem('jwt_token'); |
167 | | - |
168 | 190 | const headers = { |
169 | 191 | 'Content-Type': 'application/json' |
170 | 192 | }; |
171 | 193 |
|
| 194 | + // Get a valid JWT token using our helper method |
| 195 | + const jwtToken = ApiClient._getValidJwtToken(); |
| 196 | + |
172 | 197 | // Add Authorization header with JWT token if available |
173 | 198 | if (jwtToken) { |
174 | 199 | headers['Authorization'] = `Bearer ${jwtToken}`; |
@@ -227,21 +252,16 @@ export class ApiClient { |
227 | 252 | * @private |
228 | 253 | */ |
229 | 254 | static async fetchBinaryResponse(url, body) { |
230 | | - // Get JWT token from localStorage and ensure it's properly formatted |
231 | | - let jwtToken = localStorage.getItem('jwt_token'); |
232 | | - |
233 | 255 | const headers = { |
234 | 256 | 'Content-Type': 'application/json', |
235 | 257 | }; |
236 | 258 |
|
| 259 | + // Get a valid JWT token using our helper method |
| 260 | + const jwtToken = ApiClient._getValidJwtToken(); |
| 261 | + |
237 | 262 | // Add Authorization header with JWT token if available |
238 | 263 | if (jwtToken) { |
239 | | - // Ensure token is properly trimmed |
240 | | - jwtToken = jwtToken.trim(); |
241 | | - |
242 | | - // Log token information for debugging |
243 | 264 | console.log(`API Client: Using JWT token of length ${jwtToken.length}`); |
244 | | - |
245 | 265 | headers['Authorization'] = `Bearer ${jwtToken}`; |
246 | 266 | } |
247 | 267 |
|
|
0 commit comments