Skip to content

Commit 35c4f5e

Browse files
committed
fix: reg
1 parent 45252db commit 35c4f5e

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

public/js/api-client.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22
* API client for document generation service
33
*/
44
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+
}
530
/**
631
* Base URL for API endpoints
732
* @type {string}
@@ -128,13 +153,13 @@ export class ApiClient {
128153
url.searchParams.append('limit', limit.toString());
129154
url.searchParams.append('offset', offset.toString());
130155

131-
// Get JWT token from localStorage
132-
const jwtToken = localStorage.getItem('jwt_token');
133-
134156
const headers = {
135157
'Accept': 'application/json',
136158
};
137159

160+
// Get a valid JWT token using our helper method
161+
const jwtToken = ApiClient._getValidJwtToken();
162+
138163
// Add Authorization header with JWT token if available
139164
if (jwtToken) {
140165
headers['Authorization'] = `Bearer ${jwtToken}`;
@@ -162,13 +187,13 @@ export class ApiClient {
162187
*/
163188
static async createSubscription(email, plan, coin) {
164189
try {
165-
// Get JWT token from localStorage
166-
const jwtToken = localStorage.getItem('jwt_token');
167-
168190
const headers = {
169191
'Content-Type': 'application/json'
170192
};
171193

194+
// Get a valid JWT token using our helper method
195+
const jwtToken = ApiClient._getValidJwtToken();
196+
172197
// Add Authorization header with JWT token if available
173198
if (jwtToken) {
174199
headers['Authorization'] = `Bearer ${jwtToken}`;
@@ -227,21 +252,16 @@ export class ApiClient {
227252
* @private
228253
*/
229254
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-
233255
const headers = {
234256
'Content-Type': 'application/json',
235257
};
236258

259+
// Get a valid JWT token using our helper method
260+
const jwtToken = ApiClient._getValidJwtToken();
261+
237262
// Add Authorization header with JWT token if available
238263
if (jwtToken) {
239-
// Ensure token is properly trimmed
240-
jwtToken = jwtToken.trim();
241-
242-
// Log token information for debugging
243264
console.log(`API Client: Using JWT token of length ${jwtToken.length}`);
244-
245265
headers['Authorization'] = `Bearer ${jwtToken}`;
246266
}
247267

public/js/components/api-key-manager.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,27 @@ export class ApiKeyManager extends BaseComponent {
729729
_getApiKey() {
730730
// First, try to get JWT token
731731
const jwtToken = localStorage.getItem('jwt_token');
732-
if (jwtToken) {
733-
console.log('Using JWT token for authentication, length:', jwtToken.length);
732+
if (jwtToken && jwtToken !== 'null' && jwtToken.length > 50) {
733+
console.log('Using valid JWT token for authentication, length:', jwtToken.length);
734734
return jwtToken;
735+
} else if (jwtToken) {
736+
console.warn('Found invalid JWT token in localStorage, length:', jwtToken?.length || 0);
737+
}
738+
739+
// Try to recover JWT token from Supabase
740+
try {
741+
// Check if Supabase client is available globally
742+
if (window.supabase) {
743+
const session = window.supabase.auth.session();
744+
if (session && session.access_token && session.access_token.length > 50) {
745+
console.log('Recovered token from Supabase session, length:', session.access_token.length);
746+
// Store for future use
747+
localStorage.setItem('jwt_token', session.access_token);
748+
return session.access_token;
749+
}
750+
}
751+
} catch (e) {
752+
console.error('Error recovering token from Supabase:', e);
735753
}
736754

737755
// Next, try API key from localStorage
@@ -749,7 +767,7 @@ export class ApiKeyManager extends BaseComponent {
749767
return urlApiKey;
750768
}
751769

752-
console.warn('No authentication token found in storage or URL');
770+
console.warn('No valid authentication token found in storage or URL');
753771
return null;
754772
}
755773
}

public/js/components/pf-dialog.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,7 @@ if (typeof window !== 'undefined') {
357357

358358
// Add to window for direct access
359359
window.PfDialog = PfDialog;
360-
}
360+
}
361+
362+
// Export the class for module imports
363+
export default PfDialog;

public/js/utils/auth-status.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ export async function checkAuthStatus() {
6767
};
6868
}
6969

70+
// Only proceed with a valid token
71+
if (!jwtToken || jwtToken === 'null' || jwtToken.length < 50) {
72+
console.error('No valid token available after recovery attempts');
73+
return {
74+
authenticated: false,
75+
message: 'No valid authentication token available'
76+
};
77+
}
78+
79+
console.log('Using JWT token for auth check, length:', jwtToken.length);
80+
7081
// Make request to auth status endpoint
7182
const response = await fetch('/api/1/auth/status', {
7283
method: 'GET',

0 commit comments

Comments
 (0)