fix: replace deprecated get_current_user with authenticate_user across all endpoints and fix apiCall auth injection in frontend
This commit is contained in:
+91
-80
@@ -636,39 +636,41 @@
|
||||
document.getElementById('createRantBtn').style.display = isLoggedIn ? 'flex' : 'none';
|
||||
}
|
||||
|
||||
async function apiCall(endpoint, options = {}) {
|
||||
const url = `${API_URL}${endpoint}`;
|
||||
|
||||
// Add auth to FormData or URLSearchParams if logged in
|
||||
if (currentUser && options.body) {
|
||||
if (options.body instanceof FormData) {
|
||||
options.body.append('app', APP_ID);
|
||||
options.body.append('token_id', currentUser.token_id);
|
||||
options.body.append('token_key', currentUser.token_key);
|
||||
options.body.append('user_id', currentUser.id);
|
||||
} else if (options.body instanceof URLSearchParams) {
|
||||
options.body.append('app', APP_ID);
|
||||
options.body.append('token_id', currentUser.token_id);
|
||||
options.body.append('token_key', currentUser.token_key);
|
||||
options.body.append('user_id', currentUser.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Add auth to query params for GET requests
|
||||
if (currentUser && (options.method === 'GET' || !options.method)) {
|
||||
const separator = endpoint.includes('?') ? '&' : '?';
|
||||
endpoint += `${separator}app=${APP_ID}&token_id=${currentUser.token_id}&token_key=${currentUser.token_key}&user_id=${currentUser.id}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, options);
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('API Error:', error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
async function apiCall(endpoint, options = {}) {
|
||||
let url = `${API_URL}${endpoint}`;
|
||||
|
||||
// Add auth to FormData or URLSearchParams if logged in
|
||||
if (currentUser && options.body) {
|
||||
if (options.body instanceof FormData) {
|
||||
options.body.append('app', APP_ID);
|
||||
options.body.append('token_id', currentUser.token_id);
|
||||
options.body.append('token_key', currentUser.token_key);
|
||||
options.body.append('user_id', currentUser.id);
|
||||
} else if (options.body instanceof URLSearchParams) {
|
||||
options.body.append('app', APP_ID);
|
||||
options.body.append('token_id', currentUser.token_id);
|
||||
options.body.append('token_key', currentUser.token_key);
|
||||
options.body.append('user_id', currentUser.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Add auth to query params for GET requests
|
||||
if (currentUser && (options.method === 'GET' || !options.method)) {
|
||||
const separator = endpoint.includes('?') ? '&' : '?';
|
||||
url += `${separator}app=${APP_ID}&token_id=${currentUser.token_id}&token_key=${currentUser.token_key}&user_id=${currentUser.id}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, options);
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('API Error:', error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// View functions
|
||||
async function showFeed(sort = 'recent') {
|
||||
@@ -981,57 +983,66 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function showNotifications() {
|
||||
currentView = 'notifications';
|
||||
|
||||
const content = document.getElementById('content');
|
||||
content.innerHTML = `
|
||||
<div class="loading">
|
||||
<div class="spinner"></div>
|
||||
<p>Loading notifications...</p>
|
||||
async function showNotifications() {
|
||||
currentView = 'notifications';
|
||||
|
||||
const content = document.getElementById('content');
|
||||
content.innerHTML = `
|
||||
<div class="loading">
|
||||
<div class="spinner"></div>
|
||||
<p>Loading notifications...</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
ext_prof: 1,
|
||||
last_time: Math.floor(Date.now() / 1000) - 86400
|
||||
});
|
||||
|
||||
const data = await apiCall(`/users/me/notif-feed?${params}`);
|
||||
|
||||
if (data.success) {
|
||||
const items = data.data.items;
|
||||
|
||||
content.innerHTML = `
|
||||
<h2>Notifications</h2>
|
||||
${items.length === 0 ? '<p style="text-align: center; color: var(--text-dim); margin-top: 2rem;">No notifications</p>' : ''}
|
||||
${items.map(notif => `
|
||||
<div class="rant-card" onclick="showRant(${notif.rant_id})" style="cursor: pointer;">
|
||||
<p><strong>${notif.username}</strong> ${notif.type === 'comment' ? 'commented on your rant' : 'mentioned you'}</p>
|
||||
<p style="color: var(--text-dim); font-size: 0.9rem;">${formatTime(notif.created_time)}</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
ext_prof: 1,
|
||||
last_time: Math.floor(Date.now() / 1000) - 86400,
|
||||
app: APP_ID
|
||||
});
|
||||
const data = await apiCall(`/users/me/notif-feed?${params}`);
|
||||
|
||||
if (data.success) {
|
||||
const items = data.data.items;
|
||||
|
||||
content.innerHTML = `
|
||||
<h2>Notifications</h2>
|
||||
${items.length === 0 ? '<p style="text-align: center; color: var(--text-dim); margin-top: 2rem;">No notifications</p>' : ''}
|
||||
${items.map(notif => `
|
||||
<div class="rant-card" onclick="showRant(${notif.rant_id})">
|
||||
<p><strong>${notif.username}</strong> ${notif.type === 'comment' ? 'commented on your rant' : 'mentioned you'}</p>
|
||||
<p style="color: var(--text-dim); font-size: 0.9rem;">${formatTime(notif.created_time)}</p>
|
||||
</div>
|
||||
`).join('')}
|
||||
`;
|
||||
|
||||
// Update notification count
|
||||
updateNotificationCount(0);
|
||||
}
|
||||
}
|
||||
`).join('')}
|
||||
`;
|
||||
|
||||
// Update notification count
|
||||
updateNotificationCount(0);
|
||||
} else {
|
||||
content.innerHTML = `
|
||||
<h2>Notifications</h2>
|
||||
<p style="text-align: center; color: var(--error); margin-top: 2rem;">Failed to load notifications: ${data.error || 'Unknown error'}</p>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
async function checkNotifications() {
|
||||
if (!currentUser) return;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
ext_prof: 1,
|
||||
last_time: Math.floor(Date.now() / 1000) - 86400
|
||||
});
|
||||
|
||||
const data = await apiCall(`/users/me/notif-feed?${params}`);
|
||||
|
||||
if (data.success) {
|
||||
updateNotificationCount(data.data.num_unread);
|
||||
}
|
||||
}
|
||||
|
||||
async function checkNotifications() {
|
||||
if (!currentUser) return;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
ext_prof: 1,
|
||||
last_time: Math.floor(Date.now() / 1000) - 86400,
|
||||
app: APP_ID
|
||||
});
|
||||
const data = await apiCall(`/users/me/notif-feed?${params}`);
|
||||
|
||||
if (data.success) {
|
||||
updateNotificationCount(data.data.num_unread);
|
||||
}
|
||||
}
|
||||
|
||||
function updateNotificationCount(count) {
|
||||
const notifCount = document.getElementById('notifCount');
|
||||
|
||||
Reference in New Issue
Block a user