Base Website
Basic Website, mit Login, Homepage, Usericon, Profilsettings, adminsettings. (KI)
This commit is contained in:
1 parent
99cea87151
commit
c4cef10909
8 files changed
+424
-14
No files matched your search
@@ -31,3 +31,12 @@ def get_me(request: Request, db: Session = Depends(get_db)) -> User:
|
|||||||
raise UnauthorizedError("Token invalid or expired")
|
raise UnauthorizedError("Token invalid or expired")
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
def get_current_user_optional(request: Request, db: Session = Depends(get_db)) -> User | None:
|
||||||
|
"""
|
||||||
|
Same as get_me, but without error
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return get_me(request, db)
|
||||||
|
except UnauthorizedError:
|
||||||
|
return None
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
# @/main.py
|
# @/main.py
|
||||||
from fastapi import FastAPI, Request, status
|
from fastapi import FastAPI, Request, status, Depends
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse, RedirectResponse
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
from exceptions import UserNotFoundError, UserAlreadyExistsError, UnauthorizedError, ForbiddenError, ValidationError #Errors
|
from exceptions import UserNotFoundError, UserAlreadyExistsError, UnauthorizedError, ForbiddenError, ValidationError #Errors
|
||||||
|
from auth.dependencies import get_current_user_optional
|
||||||
|
from database import get_db
|
||||||
import models #Database
|
import models #Database
|
||||||
from routers import api_router #API
|
from routers import api_router #API
|
||||||
|
|
||||||
@@ -13,6 +15,17 @@ app.include_router(api_router)
|
|||||||
templates = Jinja2Templates(directory="templates")
|
templates = Jinja2Templates(directory="templates")
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_next(path: str) -> str:
|
||||||
|
"""
|
||||||
|
Lässt als Redirect-Ziel nur relative, interne Pfade zu.
|
||||||
|
Ohne diese Prüfung wäre /login?next=https://evil.example ein offener
|
||||||
|
Redirect (Phishing: Login bei uns, Weiterleitung auf fremde Seite).
|
||||||
|
"""
|
||||||
|
if path.startswith("/") and not path.startswith("//") and "://" not in path:
|
||||||
|
return path
|
||||||
|
return "/"
|
||||||
|
|
||||||
|
|
||||||
######### ERROR handling #########
|
######### ERROR handling #########
|
||||||
|
|
||||||
# 1. Globale Regel für 404
|
# 1. Globale Regel für 404
|
||||||
@@ -54,8 +67,47 @@ def permissions_handler(request: Request, exc: ValidationError):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
######### PAGES #########
|
||||||
|
|
||||||
|
@app.get("/login")
|
||||||
|
async def login_page(request: Request, next: str = "/", db: Session = Depends(get_db)):
|
||||||
|
# Schon eingeloggt? Dann /login gar nicht erst anzeigen.
|
||||||
|
user = get_current_user_optional(request, db)
|
||||||
|
if user is not None:
|
||||||
|
return RedirectResponse(url=_safe_next(next), status_code=status.HTTP_302_FOUND)
|
||||||
|
return templates.TemplateResponse(request=request, name="login.html")
|
||||||
|
|
||||||
|
@app.get("/logout")
|
||||||
|
async def logout():
|
||||||
|
response = RedirectResponse(url="/login", status_code=status.HTTP_302_FOUND)
|
||||||
|
response.delete_cookie("access_token")
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def home(request: Request):
|
async def home(request: Request, db: Session = Depends(get_db)):
|
||||||
return templates.TemplateResponse(request=request, name="index.html")
|
user = get_current_user_optional(request, db)
|
||||||
|
if user is None:
|
||||||
|
return RedirectResponse(url="/login?next=/", status_code=status.HTTP_302_FOUND)
|
||||||
|
return templates.TemplateResponse(request=request, name="index.html", context={"user": user})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/profile")
|
||||||
|
async def profile_page(request: Request, db: Session = Depends(get_db)):
|
||||||
|
user = get_current_user_optional(request, db)
|
||||||
|
if user is None:
|
||||||
|
return RedirectResponse(url="/login?next=/profile", status_code=status.HTTP_302_FOUND)
|
||||||
|
return templates.TemplateResponse(request=request, name="profile.html", context={"user": user})
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/admin")
|
||||||
|
async def admin_page(request: Request, db: Session = Depends(get_db)):
|
||||||
|
user = get_current_user_optional(request, db)
|
||||||
|
if user is None:
|
||||||
|
return RedirectResponse(url="/login?next=/admin", status_code=status.HTTP_302_FOUND)
|
||||||
|
if user.role != "admin":
|
||||||
|
# Kein 403 auf Seitenebene - einfach zurück zur Startseite.
|
||||||
|
# Der eigentliche Schutz sitzt ohnehin serverseitig in den /api/users-Endpunkten.
|
||||||
|
return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND)
|
||||||
|
return templates.TemplateResponse(request=request, name="admin.html", context={"user": user})
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}Admin – Tandur{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
{% include "navbar.html" %}
|
||||||
|
|
||||||
|
<main class="mx-auto max-w-5xl px-4 py-10" x-data="adminPanel()" x-init="load()">
|
||||||
|
<div class="mb-8 flex items-center justify-between">
|
||||||
|
<h1 class="font-display text-2xl font-semibold">Benutzer</h1>
|
||||||
|
<button @click="openCreate()" class="rounded bg-marker px-4 py-2 text-sm font-medium text-board hover:brightness-95">
|
||||||
|
Benutzer anlegen
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p x-show="error" x-text="error" class="mb-4 text-sm text-brick" style="display:none"></p>
|
||||||
|
|
||||||
|
<div class="overflow-hidden rounded border border-panel">
|
||||||
|
<table class="w-full text-left text-sm">
|
||||||
|
<thead class="bg-panel text-muted">
|
||||||
|
<tr>
|
||||||
|
<th class="px-4 py-2 font-medium">E-Mail</th>
|
||||||
|
<th class="px-4 py-2 font-medium">Rolle</th>
|
||||||
|
<th class="px-4 py-2 font-medium"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<template x-for="u in users" :key="u.id">
|
||||||
|
<tr class="border-t border-panel">
|
||||||
|
<td class="px-4 py-2" x-text="u.email"></td>
|
||||||
|
<td class="px-4 py-2" x-text="u.role"></td>
|
||||||
|
<td class="space-x-3 px-4 py-2 text-right">
|
||||||
|
<button @click="openEdit(u)" class="text-marker hover:underline">Bearbeiten</button>
|
||||||
|
<button @click="remove(u)" class="text-brick hover:underline">Löschen</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</template>
|
||||||
|
<tr x-show="!loading && users.length === 0">
|
||||||
|
<td colspan="3" class="px-4 py-6 text-center text-muted">Keine Benutzer vorhanden.</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal: Anlegen / Bearbeiten -->
|
||||||
|
<div x-show="modalOpen" style="display:none" class="fixed inset-0 z-10 flex items-center justify-center bg-black/50 px-4">
|
||||||
|
<div class="w-full max-w-sm rounded border border-panel bg-panel p-6">
|
||||||
|
<h2 class="mb-4 font-display text-lg font-semibold" x-text="editing ? 'Benutzer bearbeiten' : 'Benutzer anlegen'"></h2>
|
||||||
|
|
||||||
|
<form @submit.prevent="save" class="space-y-4">
|
||||||
|
<div>
|
||||||
|
<label class="mb-1 block text-sm text-muted">E-Mail</label>
|
||||||
|
<input type="username" x-model="form.email" required
|
||||||
|
class="w-full rounded border border-board bg-board px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="mb-1 block text-sm text-muted" x-text="editing ? 'Neues Passwort (optional)' : 'Passwort'"></label>
|
||||||
|
<input type="password" x-model="form.password" :required="!editing"
|
||||||
|
class="w-full rounded border border-board bg-board px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="mb-1 block text-sm text-muted">Rolle</label>
|
||||||
|
<select x-model="form.role" class="w-full rounded border border-board bg-board px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
<option value="user">user</option>
|
||||||
|
<option value="admin">admin</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p x-show="formError" x-text="formError" class="text-sm text-brick" style="display:none"></p>
|
||||||
|
|
||||||
|
<div class="flex justify-end gap-3 pt-2">
|
||||||
|
<button type="button" @click="modalOpen = false" class="rounded px-4 py-2 text-sm text-muted hover:text-chalk">Abbrechen</button>
|
||||||
|
<button type="submit" class="rounded bg-marker px-4 py-2 text-sm font-medium text-board hover:brightness-95">Speichern</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function adminPanel() {
|
||||||
|
return {
|
||||||
|
users: [],
|
||||||
|
loading: true,
|
||||||
|
error: '',
|
||||||
|
modalOpen: false,
|
||||||
|
editing: null,
|
||||||
|
form: { email: '', password: '', role: 'user' },
|
||||||
|
formError: '',
|
||||||
|
|
||||||
|
async load() {
|
||||||
|
this.loading = true;
|
||||||
|
this.error = '';
|
||||||
|
const res = await fetch('/api/users/', { credentials: 'same-origin' });
|
||||||
|
if (res.status === 401) {
|
||||||
|
window.location.href = '/login?next=/admin';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (res.status === 404) {
|
||||||
|
// Backend meldet "keine User" als 404 statt leerer Liste
|
||||||
|
this.users = [];
|
||||||
|
} else if (!res.ok) {
|
||||||
|
this.error = 'Benutzer konnten nicht geladen werden.';
|
||||||
|
} else {
|
||||||
|
this.users = await res.json();
|
||||||
|
}
|
||||||
|
this.loading = false;
|
||||||
|
},
|
||||||
|
|
||||||
|
openCreate() {
|
||||||
|
this.editing = null;
|
||||||
|
this.form = { email: '', password: '', role: 'user' };
|
||||||
|
this.formError = '';
|
||||||
|
this.modalOpen = true;
|
||||||
|
},
|
||||||
|
|
||||||
|
openEdit(u) {
|
||||||
|
this.editing = u;
|
||||||
|
this.form = { email: u.email, password: '', role: u.role };
|
||||||
|
this.formError = '';
|
||||||
|
this.modalOpen = true;
|
||||||
|
},
|
||||||
|
|
||||||
|
async save() {
|
||||||
|
this.formError = '';
|
||||||
|
const payload = { email: this.form.email, role: this.form.role };
|
||||||
|
if (this.form.password) payload.password = this.form.password;
|
||||||
|
|
||||||
|
const url = this.editing ? `/api/users/${this.editing.id}` : '/api/users/';
|
||||||
|
const method = this.editing ? 'PATCH' : 'POST';
|
||||||
|
|
||||||
|
const res = await fetch(url, {
|
||||||
|
method,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
credentials: 'same-origin',
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => ({}));
|
||||||
|
this.formError = data.detail || 'Speichern fehlgeschlagen.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.modalOpen = false;
|
||||||
|
await this.load();
|
||||||
|
},
|
||||||
|
|
||||||
|
async remove(u) {
|
||||||
|
if (!confirm(`Benutzer ${u.email} wirklich löschen?`)) return;
|
||||||
|
const res = await fetch(`/api/users/${u.id}`, { method: 'DELETE', credentials: 'same-origin' });
|
||||||
|
if (res.ok) await this.load();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
+29
-5
@@ -2,15 +2,39 @@
|
|||||||
<html lang="de">
|
<html lang="de">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Shoppingliste</title>
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>{% block title %}Tandur{% endblock %}</title>
|
||||||
|
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;600;700&display=swap" rel="stylesheet">
|
||||||
|
|
||||||
<!-- Tailwind via CDN -->
|
<!-- Tailwind via CDN -->
|
||||||
<script src="https://cdn.tailwindcss.com"></script>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
|
<script>
|
||||||
|
tailwind.config = {
|
||||||
|
theme: {
|
||||||
|
extend: {
|
||||||
|
colors: {
|
||||||
|
board: '#1F2B22', /* Hintergrund: dunkles Tafel-Grün */
|
||||||
|
panel: '#26332A', /* Flächen/Karten */
|
||||||
|
chalk: '#F2EFE6', /* Text */
|
||||||
|
muted: '#A9B3A6', /* Text, gedämpft */
|
||||||
|
marker: '#E8B93A', /* Akzent / primäre Aktion */
|
||||||
|
brick: '#C1553D', /* Warnung / destruktive Aktion */
|
||||||
|
},
|
||||||
|
fontFamily: {
|
||||||
|
display: ['"Space Grotesk"', 'sans-serif'],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
<!-- Alpine.js via CDN -->
|
<!-- Alpine.js via CDN -->
|
||||||
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
||||||
|
{% block head %}{% endblock %}
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-gray-100 p-8">
|
<body class="min-h-screen bg-board text-chalk antialiased">
|
||||||
<div class="max-w-2xl mx-auto bg-white p-6 rounded-lg shadow-md">
|
{% block content %}{% endblock %}
|
||||||
{% block content %}{% endblock %}
|
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
+12
-5
@@ -1,8 +1,15 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
{% block title %}Tandur{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div x-data="{ title: 'Meine Shoppingliste' }">
|
{% include "navbar.html" %}
|
||||||
<h1 class="text-2xl font-bold mb-4" x-text="title"></h1>
|
|
||||||
<p>Das Grundgerüst steht!</p>
|
<main class="mx-auto max-w-5xl px-4 py-10">
|
||||||
</div>
|
<h1 class="font-display text-2xl font-semibold mb-2">Einkaufsliste</h1>
|
||||||
|
<p class="mb-8 text-muted">Angemeldet als {{ user.email }}</p>
|
||||||
|
|
||||||
|
<button type="button" disabled title="Noch nicht implementiert"
|
||||||
|
class="cursor-not-allowed rounded bg-marker px-4 py-2 font-medium text-board opacity-60">
|
||||||
|
Daten laden
|
||||||
|
</button>
|
||||||
|
</main>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}Anmelden – Tandur{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<div class="flex min-h-screen items-center justify-center px-4">
|
||||||
|
<div class="w-full max-w-sm">
|
||||||
|
<h1 class="font-display text-2xl font-semibold mb-1">Tandur</h1>
|
||||||
|
<p class="mb-8 text-sm text-muted">Melde dich an, um deine Einkaufsliste zu sehen.</p>
|
||||||
|
|
||||||
|
<form x-data="loginForm()" @submit.prevent="submit" class="space-y-4" novalidate>
|
||||||
|
<div>
|
||||||
|
<label for="email" class="mb-1 block text-sm text-muted">E-Mail</label>
|
||||||
|
<input id="email" type="email" x-model="email" required autocomplete="username"
|
||||||
|
class="w-full rounded border border-panel bg-panel px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="password" class="mb-1 block text-sm text-muted">Passwort</label>
|
||||||
|
<input id="password" type="password" x-model="password" required autocomplete="current-password"
|
||||||
|
class="w-full rounded border border-panel bg-panel px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p x-show="error" x-text="error" class="text-sm text-brick" style="display:none"></p>
|
||||||
|
|
||||||
|
<button type="submit" :disabled="loading"
|
||||||
|
class="w-full rounded bg-marker py-2 font-medium text-board hover:brightness-95 disabled:opacity-60">
|
||||||
|
<span x-text="loading ? 'Anmelden …' : 'Anmelden'"></span>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function loginForm() {
|
||||||
|
return {
|
||||||
|
email: '',
|
||||||
|
password: '',
|
||||||
|
error: '',
|
||||||
|
loading: false,
|
||||||
|
async submit() {
|
||||||
|
this.error = '';
|
||||||
|
this.loading = true;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
credentials: 'same-origin',
|
||||||
|
body: JSON.stringify({ username: this.email, password: this.password })
|
||||||
|
});
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => ({}));
|
||||||
|
this.error = data.detail || 'Anmeldung fehlgeschlagen.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// "next" kommt aus der URL, die main.py beim Redirect zu /login gesetzt hat
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
window.location.href = params.get('next') || '/';
|
||||||
|
} catch (e) {
|
||||||
|
this.error = 'Server nicht erreichbar.';
|
||||||
|
} finally {
|
||||||
|
this.loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<header class="border-b border-panel">
|
||||||
|
<div class="mx-auto flex max-w-5xl items-center justify-between px-4 py-3">
|
||||||
|
<a href="/" class="font-display text-lg font-semibold tracking-tight">Tandur</a>
|
||||||
|
|
||||||
|
<div x-data="{ open: false }" class="relative">
|
||||||
|
<button @click="open = !open" @click.outside="open = false"
|
||||||
|
class="flex h-9 w-9 items-center justify-center rounded border border-panel transition-colors hover:border-marker"
|
||||||
|
aria-label="Menü öffnen" aria-haspopup="true" :aria-expanded="open">
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
|
||||||
|
<circle cx="12" cy="8" r="3.2"/>
|
||||||
|
<path d="M4.5 20c1.4-3.6 4.2-5.4 7.5-5.4s6.1 1.8 7.5 5.4"/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div x-show="open" x-transition.origin.top.right style="display: none;"
|
||||||
|
class="absolute right-0 mt-2 w-48 rounded border border-panel bg-panel py-1 shadow-lg shadow-black/30 z-10">
|
||||||
|
<a href="/profile" class="block px-4 py-2 text-sm hover:bg-board">Profil</a>
|
||||||
|
{% if user.role == "admin" %}
|
||||||
|
<a href="/admin" class="block px-4 py-2 text-sm hover:bg-board">Admin-Einstellungen</a>
|
||||||
|
{% endif %}
|
||||||
|
<div class="my-1 border-t border-board"></div>
|
||||||
|
<a href="/logout" class="block px-4 py-2 text-sm text-brick hover:bg-board">Abmelden</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}Profil – Tandur{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
{% include "navbar.html" %}
|
||||||
|
|
||||||
|
<main class="mx-auto max-w-md px-4 py-10">
|
||||||
|
<h1 class="font-display text-2xl font-semibold mb-8">Profil</h1>
|
||||||
|
|
||||||
|
<form x-data="profileForm()" x-init="init($el.dataset)"
|
||||||
|
data-user-id="{{ user.id }}" data-email="{{ user.email }}"
|
||||||
|
@submit.prevent="save" class="space-y-4">
|
||||||
|
<div>
|
||||||
|
<label class="mb-1 block text-sm text-muted">E-Mail</label>
|
||||||
|
<input type="email" x-model="email" required
|
||||||
|
class="w-full rounded border border-panel bg-panel px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="mb-1 block text-sm text-muted">Neues Passwort (optional)</label>
|
||||||
|
<input type="password" x-model="password" autocomplete="new-password"
|
||||||
|
class="w-full rounded border border-panel bg-panel px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
|
||||||
|
</div>
|
||||||
|
<div class="text-sm text-muted">Rolle: <span class="text-chalk">{{ user.role }}</span></div>
|
||||||
|
|
||||||
|
<p x-show="message" x-text="message" :class="success ? 'text-marker' : 'text-brick'" class="text-sm" style="display:none"></p>
|
||||||
|
|
||||||
|
<button type="submit" class="rounded bg-marker px-4 py-2 font-medium text-board hover:brightness-95">
|
||||||
|
Speichern
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function profileForm() {
|
||||||
|
return {
|
||||||
|
userId: null,
|
||||||
|
email: '',
|
||||||
|
password: '',
|
||||||
|
message: '',
|
||||||
|
success: false,
|
||||||
|
init(dataset) {
|
||||||
|
this.userId = dataset.userId;
|
||||||
|
this.email = dataset.email;
|
||||||
|
},
|
||||||
|
async save() {
|
||||||
|
this.message = '';
|
||||||
|
const payload = { email: this.email };
|
||||||
|
if (this.password) payload.password = this.password;
|
||||||
|
|
||||||
|
const res = await fetch(`/api/users/${this.userId}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
credentials: 'same-origin',
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.status === 401) {
|
||||||
|
window.location.href = '/login?next=/profile';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => ({}));
|
||||||
|
this.message = data.detail || 'Speichern fehlgeschlagen.';
|
||||||
|
this.success = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.password = '';
|
||||||
|
this.message = 'Gespeichert.';
|
||||||
|
this.success = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
Reference in new issue
Block a user