- Neues Profil-Modal: Avatar-Upload, E-Mail bearbeiten, Kalender-Übersicht - Passwort ändern mit Validierung des aktuellen Passworts - TOTP 2FA: QR-Code + manueller Schlüssel, Aktivierung/Deaktivierung - Login-Flow unterstützt 2FA-Code (neuer JSON-Endpoint /auth/login) - User-Dropdown mit Profil-Link statt confirm()-Dialog - Kalenderfarben in Sidebar editierbar (Color-Picker auf Farbpunkt) - Monatsansicht nutzt volle Höhe (#view-container flex fix) - requirements.txt: passlib durch bcrypt ersetzt, pyotp/qrcode/Pillow hinzugefügt
72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
const BASE = '/api';
|
|
|
|
async function request(method, path, body = null, formEncoded = false) {
|
|
const token = localStorage.getItem('token');
|
|
const headers = {};
|
|
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
let bodyStr = null;
|
|
if (body !== null) {
|
|
if (formEncoded) {
|
|
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
bodyStr = new URLSearchParams(body).toString();
|
|
} else {
|
|
headers['Content-Type'] = 'application/json';
|
|
bodyStr = JSON.stringify(body);
|
|
}
|
|
}
|
|
|
|
const res = await fetch(`${BASE}${path}`, { method, headers, body: bodyStr });
|
|
|
|
if (res.status === 401 && !path.startsWith('/auth/login')) {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.reload();
|
|
return null;
|
|
}
|
|
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ detail: 'Unbekannter Fehler' }));
|
|
throw new Error(err.detail || `HTTP ${res.status}`);
|
|
}
|
|
|
|
if (res.status === 204) return null;
|
|
return res.json();
|
|
}
|
|
|
|
async function uploadRequest(path, formData) {
|
|
const token = localStorage.getItem('token');
|
|
const headers = {};
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
const res = await fetch(`${BASE}${path}`, { method: 'POST', headers, body: formData });
|
|
|
|
if (res.status === 401) {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.reload();
|
|
return null;
|
|
}
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ detail: 'Unbekannter Fehler' }));
|
|
throw new Error(err.detail || `HTTP ${res.status}`);
|
|
}
|
|
if (res.status === 204) return null;
|
|
return res.json();
|
|
}
|
|
|
|
export const api = {
|
|
get: (path) => request('GET', path),
|
|
post: (path, body) => request('POST', path, body),
|
|
put: (path, body) => request('PUT', path, body),
|
|
delete: (path) => request('DELETE', path),
|
|
upload: (path, form) => uploadRequest(path, form),
|
|
|
|
login: (username, password, totp_code = null) =>
|
|
request('POST', '/auth/login', { username, password, totp_code }),
|
|
|
|
setupRequired: () => request('GET', '/auth/setup-required'),
|
|
setup: (data) => request('POST', '/auth/setup', data),
|
|
};
|