feat: frontend für login, konto und benutzerverwaltung
- Login-Seite, Auth-Store und Router-Guard mit Weiterleitung nach dem Login - API-Client sendet X-CSRF-Token und reagiert auf 401 - Konto: Passwort ändern, angemeldete Geräte anzeigen und widerrufen - Benutzerverwaltung für Admins: anlegen, Rolle, aktiv, Passwort-Reset, löschen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e9585b395c
commit
4c2500797b
14 files changed
+873
-63
No files matched your search
@@ -0,0 +1,190 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
|
||||
import { api } from '@/api/client'
|
||||
import type { SessionInfo } from '@/api/types'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { describeUserAgent, formatDateTime } from '@/utils/format'
|
||||
|
||||
const auth = useAuthStore()
|
||||
const router = useRouter()
|
||||
|
||||
// --- Password -----------------------------------------------------------------
|
||||
|
||||
const currentPassword = ref('')
|
||||
const newPassword = ref('')
|
||||
const repeatPassword = ref('')
|
||||
const passwordMessage = ref<{ ok: boolean; text: string } | null>(null)
|
||||
const passwordBusy = ref(false)
|
||||
|
||||
async function changePassword(): Promise<void> {
|
||||
if (newPassword.value !== repeatPassword.value) {
|
||||
passwordMessage.value = { ok: false, text: 'Die neuen Passwörter stimmen nicht überein.' }
|
||||
return
|
||||
}
|
||||
passwordBusy.value = true
|
||||
passwordMessage.value = null
|
||||
try {
|
||||
await api.post('/auth/password', {
|
||||
current_password: currentPassword.value,
|
||||
new_password: newPassword.value,
|
||||
})
|
||||
currentPassword.value = newPassword.value = repeatPassword.value = ''
|
||||
passwordMessage.value = {
|
||||
ok: true,
|
||||
text: 'Passwort geändert. Alle anderen Geräte wurden abgemeldet.',
|
||||
}
|
||||
await loadSessions()
|
||||
} catch (e) {
|
||||
passwordMessage.value = { ok: false, text: e instanceof Error ? e.message : String(e) }
|
||||
} finally {
|
||||
passwordBusy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// --- Sessions -----------------------------------------------------------------
|
||||
|
||||
const sessions = ref<SessionInfo[]>([])
|
||||
const sessionError = ref<string | null>(null)
|
||||
|
||||
async function loadSessions(): Promise<void> {
|
||||
try {
|
||||
sessions.value = await api.get<SessionInfo[]>('/auth/sessions')
|
||||
sessionError.value = null
|
||||
} catch (e) {
|
||||
sessionError.value = e instanceof Error ? e.message : String(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function revoke(session: SessionInfo): Promise<void> {
|
||||
if (session.current) {
|
||||
await auth.logout()
|
||||
await router.push({ name: 'login' })
|
||||
return
|
||||
}
|
||||
await api.delete(`/auth/sessions/${session.id}`)
|
||||
await loadSessions()
|
||||
}
|
||||
|
||||
async function revokeOthers(): Promise<void> {
|
||||
if (!confirm('Alle anderen Geräte abmelden?')) return
|
||||
await api.delete('/auth/sessions')
|
||||
await loadSessions()
|
||||
}
|
||||
|
||||
onMounted(loadSessions)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="page">
|
||||
<h1>Konto</h1>
|
||||
|
||||
<section class="card">
|
||||
<h2>Angemeldet als</h2>
|
||||
<p>
|
||||
{{ auth.user?.username }}
|
||||
<span class="badge" :class="{ accent: auth.isAdmin }">{{ auth.user?.role }}</span>
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2>Passwort ändern</h2>
|
||||
<form class="form" @submit.prevent="changePassword">
|
||||
<!-- Hidden username helps password managers associate the new password -->
|
||||
<input
|
||||
type="text"
|
||||
name="username"
|
||||
autocomplete="username"
|
||||
:value="auth.user?.username"
|
||||
hidden
|
||||
/>
|
||||
<label>
|
||||
Aktuelles Passwort
|
||||
<input
|
||||
v-model="currentPassword"
|
||||
type="password"
|
||||
autocomplete="current-password"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Neues Passwort (mindestens 12 Zeichen)
|
||||
<input
|
||||
v-model="newPassword"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
minlength="12"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Neues Passwort wiederholen
|
||||
<input
|
||||
v-model="repeatPassword"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
minlength="12"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<p v-if="passwordMessage" :class="passwordMessage.ok ? 'ok' : 'error'" role="status">
|
||||
{{ passwordMessage.text }}
|
||||
</p>
|
||||
<button type="submit" class="primary" :disabled="passwordBusy">Passwort ändern</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2>Angemeldete Geräte</h2>
|
||||
<p v-if="sessionError" class="error">{{ sessionError }}</p>
|
||||
<ul class="sessions">
|
||||
<li v-for="s in sessions" :key="s.id">
|
||||
<div>
|
||||
<strong>{{ describeUserAgent(s.user_agent) }}</strong>
|
||||
<span v-if="s.current" class="badge accent">dieses Gerät</span>
|
||||
<div class="muted small">
|
||||
{{ s.ip ?? 'IP unbekannt' }} · zuletzt aktiv {{ formatDateTime(s.last_seen_at) }}
|
||||
</div>
|
||||
<div class="muted small">angemeldet seit {{ formatDateTime(s.created_at) }}</div>
|
||||
</div>
|
||||
<button type="button" class="small danger" @click="revoke(s)">
|
||||
{{ s.current ? 'Abmelden' : 'Widerrufen' }}
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
<button v-if="sessions.length > 1" type="button" class="danger" @click="revokeOthers">
|
||||
Alle anderen Geräte abmelden
|
||||
</button>
|
||||
</section>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.sessions {
|
||||
list-style: none;
|
||||
margin: 0 0 1rem;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.sessions li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
padding: 0.75rem 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.sessions li:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.small {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.badge {
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,199 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
|
||||
import { api } from '@/api/client'
|
||||
import type { Role, User } from '@/api/types'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { formatDateTime } from '@/utils/format'
|
||||
|
||||
const auth = useAuthStore()
|
||||
|
||||
const users = ref<User[]>([])
|
||||
const error = ref<string | null>(null)
|
||||
const notice = ref<string | null>(null)
|
||||
|
||||
function showError(e: unknown): void {
|
||||
notice.value = null
|
||||
error.value = e instanceof Error ? e.message : String(e)
|
||||
}
|
||||
|
||||
async function load(): Promise<void> {
|
||||
try {
|
||||
users.value = await api.get<User[]>('/admin/users')
|
||||
} catch (e) {
|
||||
showError(e)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Create -------------------------------------------------------------------
|
||||
|
||||
const newUsername = ref('')
|
||||
const newPassword = ref('')
|
||||
const newRole = ref<Role>('user')
|
||||
const creating = ref(false)
|
||||
|
||||
async function createUser(): Promise<void> {
|
||||
creating.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const user = await api.post<User>('/admin/users', {
|
||||
username: newUsername.value,
|
||||
password: newPassword.value,
|
||||
role: newRole.value,
|
||||
})
|
||||
notice.value = `Benutzer „${user.username}“ angelegt.`
|
||||
newUsername.value = newPassword.value = ''
|
||||
newRole.value = 'user'
|
||||
await load()
|
||||
} catch (e) {
|
||||
showError(e)
|
||||
} finally {
|
||||
creating.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// --- Update / delete ----------------------------------------------------------
|
||||
|
||||
async function update(user: User, changes: Partial<User> & { password?: string }): Promise<void> {
|
||||
error.value = null
|
||||
try {
|
||||
await api.patch<User>(`/admin/users/${user.id}`, changes)
|
||||
await load()
|
||||
} catch (e) {
|
||||
showError(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function resetPassword(user: User): Promise<void> {
|
||||
const password = prompt(`Neues Passwort für „${user.username}“ (mindestens 12 Zeichen):`)
|
||||
if (!password) return
|
||||
await update(user, { password })
|
||||
if (!error.value) notice.value = `Passwort für „${user.username}“ gesetzt.`
|
||||
}
|
||||
|
||||
async function remove(user: User): Promise<void> {
|
||||
if (!confirm(`Benutzer „${user.username}“ endgültig löschen?`)) return
|
||||
error.value = null
|
||||
try {
|
||||
await api.delete(`/admin/users/${user.id}`)
|
||||
notice.value = `Benutzer „${user.username}“ gelöscht.`
|
||||
await load()
|
||||
} catch (e) {
|
||||
showError(e)
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(load)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="page">
|
||||
<h1>Benutzer</h1>
|
||||
|
||||
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
||||
<p v-if="notice" class="ok" role="status">{{ notice }}</p>
|
||||
|
||||
<section class="card">
|
||||
<h2>Neuer Benutzer</h2>
|
||||
<form class="form" @submit.prevent="createUser">
|
||||
<label>
|
||||
Benutzername
|
||||
<input
|
||||
v-model="newUsername"
|
||||
autocomplete="off"
|
||||
autocapitalize="none"
|
||||
spellcheck="false"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Passwort (mindestens 12 Zeichen)
|
||||
<input
|
||||
v-model="newPassword"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
minlength="12"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Rolle
|
||||
<select v-model="newRole">
|
||||
<option value="user">Benutzer</option>
|
||||
<option value="admin">Administrator</option>
|
||||
</select>
|
||||
</label>
|
||||
<button type="submit" class="primary" :disabled="creating">Anlegen</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2>Alle Benutzer</h2>
|
||||
<ul class="users">
|
||||
<li v-for="u in users" :key="u.id" :class="{ inactive: !u.is_active }">
|
||||
<div class="info">
|
||||
<strong>{{ u.username }}</strong>
|
||||
<span class="badge" :class="{ accent: u.role === 'admin' }">{{ u.role }}</span>
|
||||
<span v-if="!u.is_active" class="badge">deaktiviert</span>
|
||||
<div class="muted small">angelegt {{ formatDateTime(u.created_at) }}</div>
|
||||
</div>
|
||||
<div v-if="u.id !== auth.user?.id" class="actions">
|
||||
<button
|
||||
type="button"
|
||||
class="small"
|
||||
@click="update(u, { role: u.role === 'admin' ? 'user' : 'admin' })"
|
||||
>
|
||||
{{ u.role === 'admin' ? 'Zu Benutzer' : 'Zu Admin' }}
|
||||
</button>
|
||||
<button type="button" class="small" @click="update(u, { is_active: !u.is_active })">
|
||||
{{ u.is_active ? 'Deaktivieren' : 'Aktivieren' }}
|
||||
</button>
|
||||
<button type="button" class="small" @click="resetPassword(u)">Passwort</button>
|
||||
<button type="button" class="small danger" @click="remove(u)">Löschen</button>
|
||||
</div>
|
||||
<span v-else class="muted small">das bist du</span>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.users {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.users li {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 0.5rem 1rem;
|
||||
padding: 0.75rem 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.users li:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.users li.inactive .info {
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.info .badge {
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.small {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
</style>
|
||||
@@ -1,16 +1,18 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue'
|
||||
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { useHealthStore } from '@/stores/health'
|
||||
|
||||
const auth = useAuthStore()
|
||||
const store = useHealthStore()
|
||||
|
||||
onMounted(store.refresh)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="home">
|
||||
<h1>LAN-Clipboard</h1>
|
||||
<main class="page">
|
||||
<h1>Hallo {{ auth.user?.username }}</h1>
|
||||
<section class="card">
|
||||
<h2>Systemstatus</h2>
|
||||
<p v-if="store.loading" class="muted">Prüfe …</p>
|
||||
@@ -29,29 +31,6 @@ onMounted(store.refresh)
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.home {
|
||||
max-width: 40rem;
|
||||
margin: 0 auto;
|
||||
padding: 1.5rem 1rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.6rem;
|
||||
margin: 0 0 1rem;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
padding: 1rem 1.25rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.1rem;
|
||||
margin: 0 0 0.75rem;
|
||||
}
|
||||
|
||||
dl {
|
||||
display: grid;
|
||||
grid-template-columns: max-content 1fr;
|
||||
@@ -66,32 +45,4 @@ dt {
|
||||
dd {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.ok {
|
||||
color: var(--ok);
|
||||
}
|
||||
|
||||
.error {
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
button {
|
||||
min-height: 44px;
|
||||
padding: 0 1rem;
|
||||
border: 1px solid var(--accent);
|
||||
border-radius: var(--radius);
|
||||
background: transparent;
|
||||
color: var(--accent);
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,81 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
const auth = useAuthStore()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const username = ref('')
|
||||
const password = ref('')
|
||||
const error = ref<string | null>(null)
|
||||
const busy = ref(false)
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
busy.value = true
|
||||
error.value = null
|
||||
try {
|
||||
await auth.login(username.value, password.value)
|
||||
const redirect = typeof route.query.redirect === 'string' ? route.query.redirect : '/'
|
||||
// Only allow app-internal redirects
|
||||
await router.replace(redirect.startsWith('/') && !redirect.startsWith('//') ? redirect : '/')
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : String(e)
|
||||
password.value = ''
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="login">
|
||||
<h1>LAN-Clipboard</h1>
|
||||
<form class="card form" @submit.prevent="submit">
|
||||
<label>
|
||||
Benutzername
|
||||
<input
|
||||
v-model="username"
|
||||
name="username"
|
||||
autocomplete="username"
|
||||
autocapitalize="none"
|
||||
autocorrect="off"
|
||||
spellcheck="false"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Passwort
|
||||
<input
|
||||
v-model="password"
|
||||
name="password"
|
||||
type="password"
|
||||
autocomplete="current-password"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
||||
<button type="submit" class="primary" :disabled="busy">Anmelden</button>
|
||||
</form>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.login {
|
||||
max-width: 24rem;
|
||||
margin: 0 auto;
|
||||
padding: 15vh 1rem 2rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
font-size: 1.6rem;
|
||||
margin: 0 0 1.25rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user