fix: Preserve HTTP status code in Response::json and handle 401 session expiration gracefully in loadConversations

This commit is contained in:
Hamza-Ayed
2026-08-27 14:56:50 +03:00
parent d18d1aa2b5
commit 7faf223583
2 changed files with 25 additions and 10 deletions
+4 -2
View File
@@ -39,9 +39,11 @@ class Response
* @param int $code * @param int $code
* @return void * @return void
*/ */
public function json($data, int $code = 200): void public function json($data, ?int $code = null): void
{ {
$this->setStatusCode($code); if ($code !== null) {
$this->setStatusCode($code);
}
$this->setHeader('Content-Type', 'application/json; charset=utf-8'); $this->setHeader('Content-Type', 'application/json; charset=utf-8');
// Setup CORS headers — restrict origin to the configured allowed domain // Setup CORS headers — restrict origin to the configured allowed domain
+21 -8
View File
@@ -660,10 +660,23 @@ class TeacherPortal
} }
} }
function getAuthToken() {
return localStorage.getItem('saqel_teacher_jwt') || getCookie('saqel_teacher_jwt') || '';
}
function getCookie(name) {
const v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? v[2] : null;
}
// 2. Load Conversations List via REST API // 2. Load Conversations List via REST API
async function loadConversations() { async function loadConversations() {
const token = localStorage.getItem('saqel_teacher_jwt'); const token = getAuthToken();
if (!token) return; const container = document.getElementById('conv_list_container');
if (!token) {
if (container) container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">يرجى تسجيل الدخول أولاً</div>';
return;
}
try { try {
const res = await fetch('/api/chat/conversations', { const res = await fetch('/api/chat/conversations', {
headers: { 'Authorization': 'Bearer ' + token } headers: { 'Authorization': 'Bearer ' + token }
@@ -674,10 +687,12 @@ class TeacherPortal
data = JSON.parse(text); data = JSON.parse(text);
} catch (jsonErr) { } catch (jsonErr) {
console.error('Conversations parse error:', jsonErr, 'Raw:', text); console.error('Conversations parse error:', jsonErr, 'Raw:', text);
const container = document.getElementById('conv_list_container'); if (container) container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">خطأ في معالجة البيانات</div>';
if (container) { return;
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">خطأ في معالجة البيانات</div>'; }
}
if (res.status === 401 || (data && data.error === 'Unauthorized')) {
if (container) container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">انتهت الجلسة — يرجى تسجيل الدخول مجدداً</div>';
return; return;
} }
@@ -685,14 +700,12 @@ class TeacherPortal
renderConversations(data.data || []); renderConversations(data.data || []);
} else { } else {
console.warn('Load convs returned error payload:', data); console.warn('Load convs returned error payload:', data);
const container = document.getElementById('conv_list_container');
if (container) { if (container) {
container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">' + (data.message || 'لا توجد محادثات نشطة') + '</div>'; container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">' + (data.message || 'لا توجد محادثات نشطة') + '</div>';
} }
} }
} catch (e) { } catch (e) {
console.error('Load convs network error:', e); console.error('Load convs network error:', e);
const container = document.getElementById('conv_list_container');
if (container) { if (container) {
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">تعذر تحميل المحادثات</div>'; container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">تعذر تحميل المحادثات</div>';
} }