Skip to content

Commit 95c2f3c

Browse files
committed
fixed login
1 parent b9c62a6 commit 95c2f3c

File tree

12 files changed

+814
-163
lines changed

12 files changed

+814
-163
lines changed

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/favicon.light.svg">
1515
<link rel="apple-touch-icon" href="/icons/favicon.light.png">
1616
<script type="module" src="/js/theme.js"></script>
17+
<script src="/js/alerts.js"></script>
1718
<script type="module" src="/js/main.js"></script>
1819
</head>
1920
<body>

public/js/alerts.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Global alert utilities for the application
3+
*/
4+
5+
// Global alert function for login page
6+
window.showLoginAlert = function(message, type = 'error') {
7+
// Try to find the alert element
8+
const alertElement = document.getElementById('login-alert');
9+
10+
if (alertElement) {
11+
// If the alert element exists, update its content and display it
12+
alertElement.textContent = message;
13+
alertElement.className = `alert alert-${type}`;
14+
alertElement.classList.remove('hidden');
15+
16+
// Auto-dismiss success messages after 5 seconds
17+
if (type === 'success') {
18+
setTimeout(() => {
19+
alertElement.classList.add('hidden');
20+
}, 5000);
21+
}
22+
} else {
23+
// Fallback to standard browser alert if element doesn't exist
24+
console.log(`Alert (${type}): ${message}`);
25+
alert(message);
26+
}
27+
};
28+
29+
// Function to show any type of alert by ID
30+
window.showAlert = function(elementId, message, type = 'error') {
31+
const alertElement = document.getElementById(elementId);
32+
33+
if (alertElement) {
34+
alertElement.textContent = message;
35+
alertElement.className = `alert alert-${type}`;
36+
alertElement.classList.remove('hidden');
37+
38+
if (type === 'success') {
39+
setTimeout(() => {
40+
alertElement.classList.add('hidden');
41+
}, 5000);
42+
}
43+
} else {
44+
// Fallback to console + alert
45+
console.log(`Alert (${type}): ${message}`);
46+
alert(message);
47+
}
48+
};

public/js/api-client.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,21 @@ export class ApiClient {
227227
* @private
228228
*/
229229
static async fetchBinaryResponse(url, body) {
230-
// Get JWT token from localStorage
231-
const jwtToken = localStorage.getItem('jwt_token');
230+
// Get JWT token from localStorage and ensure it's properly formatted
231+
let jwtToken = localStorage.getItem('jwt_token');
232232

233233
const headers = {
234234
'Content-Type': 'application/json',
235235
};
236236

237237
// Add Authorization header with JWT token if available
238238
if (jwtToken) {
239+
// Ensure token is properly trimmed
240+
jwtToken = jwtToken.trim();
241+
242+
// Log token information for debugging
243+
console.log(`API Client: Using JWT token of length ${jwtToken.length}`);
244+
239245
headers['Authorization'] = `Bearer ${jwtToken}`;
240246
}
241247

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

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,27 @@ export class ApiKeyManager extends BaseComponent {
487487
const { ApiClient } = await import('../api-client.js');
488488
const baseUrl = ApiClient.baseUrl || '/api/1';
489489

490+
// Get a valid token, avoid sending 'null'
491+
const authToken = this._getApiKey();
492+
493+
// Debug the token we're about to use
494+
console.log('API Key Manager: Authorization token length:', authToken?.length || 0);
495+
496+
// Only include Authorization header if we have a valid token
497+
const headers = {
498+
'Accept': 'application/json'
499+
};
500+
501+
if (authToken && authToken !== 'null' && authToken.length > 10) {
502+
console.log('API Key Manager: Using valid token for Authorization header');
503+
headers['Authorization'] = `Bearer ${authToken}`;
504+
} else {
505+
console.warn('API Key Manager: No valid token available, proceeding without Authorization header');
506+
}
507+
490508
const response = await fetch(`${baseUrl}/api-keys`, {
491509
method: 'GET',
492-
headers: {
493-
'Accept': 'application/json',
494-
'Authorization': `Bearer ${this._getApiKey()}`
495-
}
510+
headers
496511
});
497512

498513
if (!response.ok) {
@@ -532,16 +547,28 @@ export class ApiKeyManager extends BaseComponent {
532547
const { ApiClient } = await import('../api-client.js');
533548
const baseUrl = ApiClient.baseUrl || '/api/1';
534549

550+
// Get a valid token, avoid sending 'null'
551+
const authToken = this._getApiKey();
552+
553+
// Debug the token we're about to use
554+
console.log('API Key Manager: Create key - token length:', authToken?.length || 0);
555+
556+
// Only include Authorization header if we have a valid token
557+
const headers = {
558+
'Content-Type': 'application/json'
559+
};
560+
561+
if (authToken && authToken !== 'null' && authToken.length > 10) {
562+
console.log('API Key Manager: Create key - using valid token');
563+
headers['Authorization'] = `Bearer ${authToken}`;
564+
} else {
565+
console.warn('API Key Manager: Create key - no valid token available');
566+
}
567+
535568
const response = await fetch(`${baseUrl}/api-keys`, {
536569
method: 'POST',
537-
headers: {
538-
'Content-Type': 'application/json',
539-
'Accept': 'application/json',
540-
'Authorization': `Bearer ${this._getApiKey()}`
541-
},
542-
body: JSON.stringify({
543-
name: this._newKeyName
544-
})
570+
headers,
571+
body: JSON.stringify({ name: this._newKeyName })
545572
});
546573

547574
if (!response.ok) {
@@ -576,13 +603,28 @@ export class ApiKeyManager extends BaseComponent {
576603
const { ApiClient } = await import('../api-client.js');
577604
const baseUrl = ApiClient.baseUrl || '/api/1';
578605

606+
// Get a valid token, avoid sending 'null'
607+
const authToken = this._getApiKey();
608+
609+
// Debug the token we're about to use
610+
console.log('API Key Manager: Toggle key - token length:', authToken?.length || 0);
611+
612+
// Only include Authorization header if we have a valid token
613+
const headers = {
614+
'Content-Type': 'application/json',
615+
'Accept': 'application/json'
616+
};
617+
618+
if (authToken && authToken !== 'null' && authToken.length > 10) {
619+
console.log('API Key Manager: Toggle key - using valid token');
620+
headers['Authorization'] = `Bearer ${authToken}`;
621+
} else {
622+
console.warn('API Key Manager: Toggle key - no valid token available');
623+
}
624+
579625
const response = await fetch(`${baseUrl}/api-keys/${keyId}`, {
580626
method: 'PUT',
581-
headers: {
582-
'Content-Type': 'application/json',
583-
'Accept': 'application/json',
584-
'Authorization': `Bearer ${this._getApiKey()}`
585-
},
627+
headers,
586628
body: JSON.stringify({
587629
is_active: isActive
588630
})
@@ -617,12 +659,27 @@ export class ApiKeyManager extends BaseComponent {
617659
const { ApiClient } = await import('../api-client.js');
618660
const baseUrl = ApiClient.baseUrl || '/api/1';
619661

662+
// Get a valid token, avoid sending 'null'
663+
const authToken = this._getApiKey();
664+
665+
// Debug the token we're about to use
666+
console.log('API Key Manager: Delete key - token length:', authToken?.length || 0);
667+
668+
// Only include Authorization header if we have a valid token
669+
const headers = {
670+
'Accept': 'application/json'
671+
};
672+
673+
if (authToken && authToken !== 'null' && authToken.length > 10) {
674+
console.log('API Key Manager: Delete key - using valid token');
675+
headers['Authorization'] = `Bearer ${authToken}`;
676+
} else {
677+
console.warn('API Key Manager: Delete key - no valid token available');
678+
}
679+
620680
const response = await fetch(`${baseUrl}/api-keys/${keyId}`, {
621681
method: 'DELETE',
622-
headers: {
623-
'Accept': 'application/json',
624-
'Authorization': `Bearer ${this._getApiKey()}`
625-
}
682+
headers
626683
});
627684

628685
if (!response.ok) {
@@ -670,15 +727,30 @@ export class ApiKeyManager extends BaseComponent {
670727
* @private
671728
*/
672729
_getApiKey() {
673-
// Try to get from localStorage
730+
// First, try to get JWT token
731+
const jwtToken = localStorage.getItem('jwt_token');
732+
if (jwtToken) {
733+
console.log('Using JWT token for authentication, length:', jwtToken.length);
734+
return jwtToken;
735+
}
736+
737+
// Next, try API key from localStorage
674738
const apiKey = localStorage.getItem('api_key');
675739
if (apiKey) {
740+
console.log('Using API key from localStorage for authentication');
676741
return apiKey;
677742
}
678743

679-
// Try to get from URL
744+
// Finally, try to get from URL
680745
const urlParams = new URLSearchParams(window.location.search);
681-
return urlParams.get('api_key');
746+
const urlApiKey = urlParams.get('api_key');
747+
if (urlApiKey) {
748+
console.log('Using API key from URL for authentication');
749+
return urlApiKey;
750+
}
751+
752+
console.warn('No authentication token found in storage or URL');
753+
return null;
682754
}
683755
}
684756

0 commit comments

Comments
 (0)