Fix API authentication issues

- Add Authorization header passthrough in nginx config
- Add cookie passthrough for session authentication
- Improve apiRequest error handling for non-JSON responses
- Only redirect to login on protected pages when auth fails
parent 4a64f953
...@@ -46,8 +46,13 @@ server { ...@@ -46,8 +46,13 @@ server {
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Authorization $http_authorization;
proxy_set_header Connection ""; proxy_set_header Connection "";
# Pass cookies for session authentication
proxy_pass_header Set-Cookie;
proxy_cookie_path / /;
# Timeouts # Timeouts
proxy_connect_timeout 60s; proxy_connect_timeout 60s;
proxy_send_timeout 60s; proxy_send_timeout 60s;
......
...@@ -1765,7 +1765,20 @@ async function apiRequest(endpoint, method = 'GET', data = null) { ...@@ -1765,7 +1765,20 @@ async function apiRequest(endpoint, method = 'GET', data = null) {
try { try {
const response = await fetch(`${API_BASE_URL}${endpoint}`, options); const response = await fetch(`${API_BASE_URL}${endpoint}`, options);
const result = await response.json();
// Check content-type before parsing
const contentType = response.headers.get('content-type');
let result;
if (contentType && contentType.includes('application/json')) {
result = await response.json();
} else {
// Handle non-JSON responses (HTML error pages, etc.)
const text = await response.text();
console.error('API returned non-JSON response:', response.status, response.statusText);
console.error('Response text:', text.substring(0, 500));
return { error: `API returned non-JSON response (${response.status}): ${response.statusText}` };
}
// Handle token expiration // Handle token expiration
if (response.status === 401) { if (response.status === 401) {
...@@ -1775,12 +1788,27 @@ async function apiRequest(endpoint, method = 'GET', data = null) { ...@@ -1775,12 +1788,27 @@ async function apiRequest(endpoint, method = 'GET', data = null) {
// Retry the request // Retry the request
options.headers['Authorization'] = `Bearer ${localStorage.getItem('authToken')}`; options.headers['Authorization'] = `Bearer ${localStorage.getItem('authToken')}`;
const retryResponse = await fetch(`${API_BASE_URL}${endpoint}`, options); const retryResponse = await fetch(`${API_BASE_URL}${endpoint}`, options);
return await retryResponse.json();
// Check content-type for retry response too
const retryContentType = retryResponse.headers.get('content-type');
if (retryContentType && retryContentType.includes('application/json')) {
return await retryResponse.json();
} else {
const retryText = await retryResponse.text();
console.error('API retry returned non-JSON response:', retryResponse.status);
return { error: 'API retry failed' };
}
} else { } else {
// Redirect to login // Only redirect if we're on a protected page and can't refresh
localStorage.removeItem('authToken'); const isProtectedPage = window.location.pathname.includes('player.html') ||
localStorage.removeItem('refreshToken'); window.location.pathname.includes('broker.html') ||
window.location.href = 'index.html'; window.location.pathname.includes('profile.html') ||
window.location.pathname.includes('wallet.html');
if (isProtectedPage) {
localStorage.removeItem('authToken');
localStorage.removeItem('refreshToken');
window.location.href = 'index.html';
}
return { error: 'Authentication required' }; return { error: 'Authentication required' };
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment