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
@@ -1,7 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
import { RouterView } from 'vue-router'
|
||||
|
||||
import AppHeader from '@/components/AppHeader.vue'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
const auth = useAuthStore()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AppHeader v-if="auth.user" />
|
||||
<RouterView />
|
||||
</template>
|
||||
@@ -7,13 +7,60 @@ export class ApiError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export async function apiGet<T>(path: string): Promise<T> {
|
||||
const response = await fetch(`/api${path}`, {
|
||||
credentials: 'same-origin',
|
||||
headers: { Accept: 'application/json' },
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw new ApiError(response.status, `${response.status} ${response.statusText}`)
|
||||
type UnauthorizedHandler = () => void
|
||||
|
||||
let onUnauthorized: UnauthorizedHandler | null = null
|
||||
|
||||
/** Called on 401 responses (except for the login request itself). */
|
||||
export function setUnauthorizedHandler(handler: UnauthorizedHandler): void {
|
||||
onUnauthorized = handler
|
||||
}
|
||||
|
||||
function readCookie(name: string): string | null {
|
||||
const match = document.cookie.split('; ').find((c) => c.startsWith(`${name}=`))
|
||||
return match ? decodeURIComponent(match.slice(name.length + 1)) : null
|
||||
}
|
||||
|
||||
async function errorMessage(response: Response): Promise<string> {
|
||||
try {
|
||||
const data: unknown = await response.json()
|
||||
if (data && typeof data === 'object' && 'detail' in data) {
|
||||
const detail = (data as { detail: unknown }).detail
|
||||
if (typeof detail === 'string') return detail
|
||||
if (Array.isArray(detail)) return 'Ungültige Eingabe.'
|
||||
}
|
||||
} catch {
|
||||
// body is not JSON
|
||||
}
|
||||
return `${response.status} ${response.statusText}`
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(method: string, path: string, body?: unknown): Promise<T> {
|
||||
const headers: Record<string, string> = { Accept: 'application/json' }
|
||||
if (body !== undefined) headers['Content-Type'] = 'application/json'
|
||||
if (method !== 'GET') {
|
||||
const csrf = readCookie('lc_csrf')
|
||||
if (csrf) headers['X-CSRF-Token'] = csrf
|
||||
}
|
||||
|
||||
const response = await fetch(`/api${path}`, {
|
||||
method,
|
||||
headers,
|
||||
credentials: 'same-origin',
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401 && path !== '/auth/login') onUnauthorized?.()
|
||||
throw new ApiError(response.status, await errorMessage(response))
|
||||
}
|
||||
if (response.status === 204) return undefined as T
|
||||
return (await response.json()) as T
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: <T>(path: string) => apiRequest<T>('GET', path),
|
||||
post: <T>(path: string, body?: unknown) => apiRequest<T>('POST', path, body),
|
||||
patch: <T>(path: string, body?: unknown) => apiRequest<T>('PATCH', path, body),
|
||||
delete: <T>(path: string) => apiRequest<T>('DELETE', path),
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
export type Role = 'admin' | 'user'
|
||||
|
||||
export interface User {
|
||||
id: number
|
||||
username: string
|
||||
role: Role
|
||||
is_active: boolean
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface SessionInfo {
|
||||
id: number
|
||||
created_at: string
|
||||
last_seen_at: string
|
||||
expires_at: string
|
||||
ip: string | null
|
||||
user_agent: string | null
|
||||
current: boolean
|
||||
}
|
||||
@@ -36,3 +36,120 @@ body {
|
||||
padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom)
|
||||
env(safe-area-inset-left);
|
||||
}
|
||||
|
||||
/* --- Shared building blocks ------------------------------------------------ */
|
||||
|
||||
.page {
|
||||
max-width: 48rem;
|
||||
margin: 0 auto;
|
||||
padding: 1.25rem 1rem 3rem;
|
||||
}
|
||||
|
||||
.page > h1 {
|
||||
font-size: 1.5rem;
|
||||
margin: 0 0 1rem;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
padding: 1rem 1.25rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.card > h2 {
|
||||
font-size: 1.1rem;
|
||||
margin: 0 0 0.75rem;
|
||||
}
|
||||
|
||||
.form {
|
||||
display: grid;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.form label {
|
||||
display: grid;
|
||||
gap: 0.25rem;
|
||||
color: var(--muted);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
input,
|
||||
select {
|
||||
min-height: 44px;
|
||||
width: 100%;
|
||||
padding: 0 0.75rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
/* 16px prevents iOS Safari from zooming into focused inputs */
|
||||
font: inherit;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
input:focus,
|
||||
select:focus {
|
||||
outline: 2px solid var(--accent);
|
||||
outline-offset: 1px;
|
||||
}
|
||||
|
||||
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.primary {
|
||||
background: var(--accent);
|
||||
color: var(--bg);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
button.danger {
|
||||
border-color: var(--error);
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
button.small {
|
||||
min-height: 36px;
|
||||
padding: 0 0.75rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.ok {
|
||||
color: var(--ok);
|
||||
}
|
||||
|
||||
.error {
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 0 0.5rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 999px;
|
||||
font-size: 0.8rem;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.badge.accent {
|
||||
border-color: var(--accent);
|
||||
color: var(--accent);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<script setup lang="ts">
|
||||
import { RouterLink, useRouter } from 'vue-router'
|
||||
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
const auth = useAuthStore()
|
||||
const router = useRouter()
|
||||
|
||||
async function logout(): Promise<void> {
|
||||
await auth.logout()
|
||||
await router.push({ name: 'login' })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<header class="header">
|
||||
<RouterLink to="/" class="brand">LAN-Clipboard</RouterLink>
|
||||
<nav class="nav">
|
||||
<RouterLink to="/account">Konto</RouterLink>
|
||||
<RouterLink v-if="auth.isAdmin" to="/admin/users">Benutzer</RouterLink>
|
||||
<button type="button" class="small" @click="logout">Abmelden</button>
|
||||
</nav>
|
||||
</header>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.header {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 0.5rem 1rem;
|
||||
padding: 0.5rem 1rem;
|
||||
background: var(--surface);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.brand {
|
||||
color: var(--text);
|
||||
font-weight: 700;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.nav {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.nav a {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
min-height: 44px;
|
||||
padding: 0 0.75rem;
|
||||
border-radius: var(--radius);
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.nav a.router-link-active {
|
||||
color: var(--accent);
|
||||
}
|
||||
</style>
|
||||
@@ -3,12 +3,22 @@ import './assets/main.css'
|
||||
import { createPinia } from 'pinia'
|
||||
import { createApp } from 'vue'
|
||||
|
||||
import { setUnauthorizedHandler } from './api/client'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
import { useAuthStore } from './stores/auth'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
app.use(createPinia())
|
||||
app.use(router)
|
||||
|
||||
// Session expired or revoked elsewhere: drop local state and go to the login page
|
||||
setUnauthorizedHandler(() => {
|
||||
useAuthStore().clear()
|
||||
if (router.currentRoute.value.name !== 'login') {
|
||||
void router.push({ name: 'login', query: { redirect: router.currentRoute.value.fullPath } })
|
||||
}
|
||||
})
|
||||
|
||||
app.mount('#app')
|
||||
@@ -1,10 +1,50 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import HomeView from '@/views/HomeView.vue'
|
||||
|
||||
declare module 'vue-router' {
|
||||
interface RouteMeta {
|
||||
public?: boolean
|
||||
admin?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [{ path: '/', name: 'home', component: HomeView }],
|
||||
routes: [
|
||||
{ path: '/', name: 'home', component: HomeView },
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import('@/views/LoginView.vue'),
|
||||
meta: { public: true },
|
||||
},
|
||||
{ path: '/account', name: 'account', component: () => import('@/views/AccountView.vue') },
|
||||
{
|
||||
path: '/admin/users',
|
||||
name: 'admin-users',
|
||||
component: () => import('@/views/AdminUsersView.vue'),
|
||||
meta: { admin: true },
|
||||
},
|
||||
{ path: '/:pathMatch(.*)*', redirect: '/' },
|
||||
],
|
||||
})
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
const auth = useAuthStore()
|
||||
await auth.ensureLoaded()
|
||||
|
||||
if (to.meta.public) {
|
||||
return auth.user && to.name === 'login' ? { name: 'home' } : true
|
||||
}
|
||||
if (!auth.user) {
|
||||
return { name: 'login', query: to.fullPath !== '/' ? { redirect: to.fullPath } : {} }
|
||||
}
|
||||
if (to.meta.admin && !auth.isAdmin) {
|
||||
return { name: 'home' }
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,46 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
import { ApiError, api } from '@/api/client'
|
||||
import type { User } from '@/api/types'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const user = ref<User | null>(null)
|
||||
const loaded = ref(false)
|
||||
|
||||
const isAdmin = computed(() => user.value?.role === 'admin')
|
||||
|
||||
async function fetchMe(): Promise<void> {
|
||||
try {
|
||||
user.value = await api.get<User>('/auth/me')
|
||||
} catch (e) {
|
||||
if (!(e instanceof ApiError) || e.status !== 401) throw e
|
||||
user.value = null
|
||||
} finally {
|
||||
loaded.value = true
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureLoaded(): Promise<void> {
|
||||
if (!loaded.value) await fetchMe()
|
||||
}
|
||||
|
||||
async function login(username: string, password: string): Promise<void> {
|
||||
user.value = await api.post<User>('/auth/login', { username, password })
|
||||
loaded.value = true
|
||||
}
|
||||
|
||||
async function logout(): Promise<void> {
|
||||
try {
|
||||
await api.post('/auth/logout')
|
||||
} finally {
|
||||
clear()
|
||||
}
|
||||
}
|
||||
|
||||
function clear(): void {
|
||||
user.value = null
|
||||
}
|
||||
|
||||
return { user, loaded, isAdmin, fetchMe, ensureLoaded, login, logout, clear }
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
import { apiGet } from '@/api/client'
|
||||
import { api } from '@/api/client'
|
||||
|
||||
export interface Health {
|
||||
status: 'ok' | 'error'
|
||||
@@ -18,7 +18,7 @@ export const useHealthStore = defineStore('health', () => {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
health.value = await apiGet<Health>('/health')
|
||||
health.value = await api.get<Health>('/health')
|
||||
} catch (e) {
|
||||
health.value = null
|
||||
error.value = e instanceof Error ? e.message : String(e)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
const dateTimeFormat = new Intl.DateTimeFormat('de-DE', {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: 'short',
|
||||
})
|
||||
|
||||
export function formatDateTime(iso: string): string {
|
||||
return dateTimeFormat.format(new Date(iso))
|
||||
}
|
||||
|
||||
// Order matters: iPad/iPhone before macOS, Edge/Chrome before Safari
|
||||
const DEVICES: [RegExp, string][] = [
|
||||
[/iPad/, 'iPad'],
|
||||
[/iPhone/, 'iPhone'],
|
||||
[/Android/, 'Android'],
|
||||
[/Windows/, 'Windows'],
|
||||
[/Mac OS X|Macintosh/, 'macOS'],
|
||||
[/Linux/, 'Linux'],
|
||||
]
|
||||
|
||||
const BROWSERS: [RegExp, string][] = [
|
||||
[/Edg\//, 'Edge'],
|
||||
[/Firefox\/|FxiOS/, 'Firefox'],
|
||||
[/Chrome\/|CriOS/, 'Chrome'],
|
||||
[/Safari\//, 'Safari'],
|
||||
]
|
||||
|
||||
function firstMatch(ua: string, table: [RegExp, string][]): string | null {
|
||||
return table.find(([pattern]) => pattern.test(ua))?.[1] ?? null
|
||||
}
|
||||
|
||||
/** Short human-readable device description from a User-Agent string. */
|
||||
export function describeUserAgent(ua: string | null): string {
|
||||
if (!ua) return 'Unbekanntes Gerät'
|
||||
const device = firstMatch(ua, DEVICES)
|
||||
const browser = firstMatch(ua, BROWSERS)
|
||||
if (device && browser) return `${browser} auf ${device}`
|
||||
return device ?? browser ?? ua.slice(0, 40)
|
||||
}
|
||||
@@ -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