- Fix login check: use HTTP 200 instead of 201 for GET /api/user - Add encodeURIComponent for city in Ticketmaster URL - Re-throw errors in ticketmaster.js so app.js can show user feedback - Add JSDoc comments to all service and utility modules - Add German labels and text throughout (de-CH locale)
47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
/**
|
||
* auth.js – Kommunikation mit dem Backend für Registrierung und Login.
|
||
* Nutzt die Fetch-API mit einfachem Header-basiertem Authentifizierungsschema.
|
||
*
|
||
* Sicherheitshinweis: Passwörter werden unverschlüsselt als HTTP-Header übertragen.
|
||
* Dies ist bewusst vereinfacht (kein TLS/HTTPS in der lokalen Entwicklungsumgebung).
|
||
* In einer Produktionsumgebung müssten JWT-Tokens oder Sessions eingesetzt werden.
|
||
*/
|
||
|
||
const BASE_URL = "http://localhost:3000/api";
|
||
|
||
/**
|
||
* Registriert einen neuen Benutzer beim Backend.
|
||
* Das Backend vergibt ein Standard-Passwort ("1234").
|
||
* @param {string} username - Gewünschter Benutzername
|
||
* @returns {Promise<Object>} Objekt mit name und password
|
||
*/
|
||
export async function register(username) {
|
||
const res = await fetch(`${BASE_URL}/user`, {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ username })
|
||
});
|
||
|
||
return res.json();
|
||
}
|
||
|
||
/**
|
||
* Meldet einen Benutzer an.
|
||
* Credentials werden als HTTP-Header übermittelt (X-Username, X-Password).
|
||
* @param {string} username
|
||
* @param {string} password
|
||
* @returns {Promise<boolean>} true bei Erfolg (HTTP 200), false bei falschen Credentials
|
||
*/
|
||
export async function login(username, password) {
|
||
const res = await fetch(`${BASE_URL}/user`, {
|
||
method: "GET",
|
||
headers: {
|
||
"X-Username": username,
|
||
"X-Password": password
|
||
}
|
||
});
|
||
|
||
// HTTP 200 = Anmeldung erfolgreich, 401 = ungültige Credentials
|
||
return res.status === 200;
|
||
}
|