Fix token name inconsistencies in app.js

- Change 'access_token' to 'authToken' throughout
- Change 'refresh_token' to 'refreshToken' throughout
- Fix redirect loop caused by token name mismatch
parent ec94d7c4
...@@ -1746,7 +1746,7 @@ async function updateAvatar(avatarData = null, avatarUrl = null) { ...@@ -1746,7 +1746,7 @@ async function updateAvatar(avatarData = null, avatarUrl = null) {
* Make an authenticated API request * Make an authenticated API request
*/ */
async function apiRequest(endpoint, method = 'GET', data = null) { async function apiRequest(endpoint, method = 'GET', data = null) {
const token = localStorage.getItem('access_token'); const token = localStorage.getItem('authToken');
const options = { const options = {
method: method, method: method,
...@@ -1773,13 +1773,13 @@ async function apiRequest(endpoint, method = 'GET', data = null) { ...@@ -1773,13 +1773,13 @@ async function apiRequest(endpoint, method = 'GET', data = null) {
const refreshed = await refreshAccessToken(); const refreshed = await refreshAccessToken();
if (refreshed) { if (refreshed) {
// Retry the request // Retry the request
options.headers['Authorization'] = `Bearer ${localStorage.getItem('access_token')}`; options.headers['Authorization'] = `Bearer ${localStorage.getItem('authToken')}`;
const retryResponse = await fetch(endpoint, options); const retryResponse = await fetch(endpoint, options);
return await retryResponse.json(); return await retryResponse.json();
} else { } else {
// Redirect to login // Redirect to login
localStorage.removeItem('access_token'); localStorage.removeItem('authToken');
localStorage.removeItem('refresh_token'); localStorage.removeItem('refreshToken');
window.location.href = 'index.html'; window.location.href = 'index.html';
return { error: 'Authentication required' }; return { error: 'Authentication required' };
} }
...@@ -1796,7 +1796,7 @@ async function apiRequest(endpoint, method = 'GET', data = null) { ...@@ -1796,7 +1796,7 @@ async function apiRequest(endpoint, method = 'GET', data = null) {
* Refresh the access token * Refresh the access token
*/ */
async function refreshAccessToken() { async function refreshAccessToken() {
const refreshToken = localStorage.getItem('refresh_token'); const refreshToken = localStorage.getItem('refreshToken');
if (!refreshToken) { if (!refreshToken) {
return false; return false;
} }
...@@ -1807,14 +1807,14 @@ async function refreshAccessToken() { ...@@ -1807,14 +1807,14 @@ async function refreshAccessToken() {
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({ refresh_token: refreshToken }) body: JSON.stringify({ refresh_token: localStorage.getItem('refreshToken') })
}); });
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
localStorage.setItem('access_token', data.access_token); localStorage.setItem('authToken', data.access_token);
if (data.refresh_token) { if (data.refresh_token) {
localStorage.setItem('refresh_token', data.refresh_token); localStorage.setItem('refreshToken', data.refresh_token);
} }
return true; return true;
} }
......
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