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