Improve deployment safety and chat reliability
Deploy FluentGerman.ai / deploy (push) Successful in 1m25s
Deploy FluentGerman.ai / deploy (push) Successful in 1m25s
This commit is contained in:
+19
-5
@@ -52,15 +52,27 @@ async function api(path, options = {}) {
|
||||
return response;
|
||||
}
|
||||
|
||||
async function apiJSON(path, options = {}) {
|
||||
const response = await api(path, options);
|
||||
if (!response || !response.ok) {
|
||||
const error = await response?.json().catch(() => ({ detail: 'Request failed' }));
|
||||
throw new Error(error.detail || 'Request failed');
|
||||
async function ensureOk(response) {
|
||||
// api() returns undefined after a 401 — the redirect is already under way
|
||||
if (!response) throw new Error('Session expired');
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => null);
|
||||
throw new Error(error?.detail || 'Request failed');
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
async function apiJSON(path, options = {}) {
|
||||
const response = await ensureOk(await api(path, options));
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/* Same, for endpoints that answer 204 No Content (the deletes). */
|
||||
async function apiVoid(path, options = {}) {
|
||||
await ensureOk(await api(path, options));
|
||||
}
|
||||
|
||||
function requireAuth() {
|
||||
if (!getToken()) {
|
||||
window.location.href = '/';
|
||||
@@ -81,6 +93,8 @@ function requireAdmin() {
|
||||
function logout() {
|
||||
clearToken();
|
||||
clearUser();
|
||||
// Drop the saved conversation as well — shared devices are the norm here
|
||||
try { sessionStorage.clear(); } catch (e) { /* storage may be blocked */ }
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user