Redirect to login when not authenticated

This commit is contained in:
Reimar 2025-04-01 13:35:15 +02:00
parent 9529d2cdc8
commit 0c532fb979
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

View File

@ -15,31 +15,35 @@ export async function request(method, path, body = null) {
headers, headers,
body: body ? JSON.stringify(body) : undefined, body: body ? JSON.stringify(body) : undefined,
}) })
.then(async response => { .then(async response => {
try { if (response.status === 401) {
const json = await response.json(); location.href = "/login";
}
if (response.ok) return resolve(json); try {
const json = await response.json();
if (json.error) return reject(json.error); if (response.ok) return resolve(json);
if (json.message) return reject(json.message); if (json.error) return reject(json.error);
if (json.title) return reject(json.title); if (json.message) return reject(json.message);
if (json.errors) return reject(Object.values(json.errors)[0][0]); if (json.title) return reject(json.title);
} finally {
reject("Request failed with HTTP code " + response.status); if (json.errors) return reject(Object.values(json.errors)[0][0]);
} } finally {
}) reject("Request failed with HTTP code " + response.status);
.catch(err => reject(err.message)); }
})
.catch(err => reject(err.message));
}); });
} }
export function logout() { export function logout() {
localStorage.removeItem("user"); localStorage.removeItem("user");
document.cookie = "auth-token="; document.cookie = "auth-token=";
window.location.href = "/"; window.location.href = "/";
} }
export function getUser() { export function getUser() {