Add multi-user support with export feature

- New users table (migration 004) with user_id on exercises, training_sets, sessions
- User CRUD endpoints (GET/POST /api/v1/users, DELETE /api/v1/users/{id})
- All existing endpoints scoped to X-User-ID header
- CSV export endpoint (GET /api/v1/export) for completed sessions
- UserGate in PageShell: blocks app until a user is selected
- Settings page for managing users (create, switch, delete)
- BottomNav/Sidebar updated with settings navigation
- Fix: nil pointer panic in handleDeleteUser on success path
- Fix: export download now uses fetch with X-User-ID header instead of window.location.href

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Christoph K.
2026-03-21 23:55:51 +01:00
parent bff85908c3
commit a954f2c59d
24 changed files with 793 additions and 95 deletions

View File

@@ -1,4 +1,5 @@
import { NavLink } from 'react-router-dom';
import { useUserStore } from '../../stores/userStore';
const navItems = [
{
@@ -37,6 +38,16 @@ const navItems = [
</svg>
),
},
{
to: '/settings',
label: 'Einstellungen',
icon: (
<svg className="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
<path strokeLinecap="round" strokeLinejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
),
},
];
export function BottomNav() {
@@ -64,10 +75,12 @@ export function BottomNav() {
}
export function Sidebar() {
const { activeUser } = useUserStore();
return (
<nav className="hidden md:flex flex-col w-56 bg-gray-900 border-r border-gray-800 min-h-screen p-4">
<h1 className="text-xl font-bold text-blue-500 mb-8">Krafttrainer</h1>
<div className="flex flex-col gap-1">
<div className="flex flex-col gap-1 flex-1">
{navItems.map((item) => (
<NavLink
key={item.to}
@@ -86,6 +99,14 @@ export function Sidebar() {
</NavLink>
))}
</div>
{activeUser && (
<div className="pt-4 border-t border-gray-800 flex items-center gap-2">
<div className="w-7 h-7 rounded-full bg-blue-600 flex items-center justify-center text-white text-xs font-bold flex-shrink-0">
{activeUser.name.charAt(0).toUpperCase()}
</div>
<span className="text-sm text-gray-300 truncate">{activeUser.name}</span>
</div>
)}
</nav>
);
}

View File

@@ -1,18 +1,108 @@
import { useEffect, useState } from 'react';
import { Outlet } from 'react-router-dom';
import { BottomNav, Sidebar } from './BottomNav';
import { ToastContainer } from './Toast';
import { useUserStore } from '../../stores/userStore';
function UserGate({ children }: { children: React.ReactNode }) {
const { users, activeUser, setActiveUser, fetchUsers, createUser } = useUserStore();
const [newName, setNewName] = useState('');
const [loading, setLoading] = useState(false);
const [fetched, setFetched] = useState(false);
useEffect(() => {
fetchUsers().then(() => setFetched(true));
}, [fetchUsers]);
if (!fetched) {
return (
<div className="min-h-screen bg-gray-950 flex items-center justify-center">
<div className="text-gray-400">Laden</div>
</div>
);
}
if (!activeUser) {
async function handleCreate(e: React.FormEvent) {
e.preventDefault();
const name = newName.trim();
if (!name) return;
setLoading(true);
const user = await createUser(name);
setLoading(false);
if (user) setActiveUser(user);
}
return (
<div className="min-h-screen bg-gray-950 flex items-center justify-center px-4">
<div className="w-full max-w-sm space-y-6">
<div className="text-center">
<h1 className="text-3xl font-bold text-blue-500 mb-1">Krafttrainer</h1>
<p className="text-gray-400">Wer trainiert heute?</p>
</div>
{users.length > 0 && (
<ul className="space-y-2">
{users.map((user) => (
<li key={user.id}>
<button
onClick={() => setActiveUser(user)}
className="w-full flex items-center gap-3 bg-gray-900 hover:bg-gray-800 rounded-lg px-4 py-3 min-h-[56px] transition-colors text-left"
>
<div className="w-9 h-9 rounded-full bg-blue-600 flex items-center justify-center text-white font-bold text-sm flex-shrink-0">
{user.name.charAt(0).toUpperCase()}
</div>
<span className="text-gray-100 font-medium">{user.name}</span>
</button>
</li>
))}
</ul>
)}
<form onSubmit={handleCreate} className="space-y-3">
<p className="text-sm text-gray-400">
{users.length === 0 ? 'Ersten Nutzer anlegen:' : 'Oder neuen Nutzer anlegen:'}
</p>
<div className="flex gap-2">
<input
type="text"
value={newName}
onChange={(e) => setNewName(e.target.value)}
placeholder="Name"
maxLength={50}
autoFocus
className="flex-1 bg-gray-900 border border-gray-700 rounded-lg px-3 py-2 text-gray-100 placeholder-gray-500 focus:outline-none focus:border-blue-500 min-h-[44px]"
/>
<button
type="submit"
disabled={loading || !newName.trim()}
className="px-4 py-2 bg-blue-600 hover:bg-blue-500 disabled:opacity-50 text-white rounded-lg font-medium min-h-[44px] transition-colors"
>
Los
</button>
</div>
</form>
</div>
</div>
);
}
return <>{children}</>;
}
export function PageShell() {
return (
<div className="flex min-h-screen">
<Sidebar />
<main className="flex-1 pb-20 md:pb-0">
<div className="max-w-2xl mx-auto px-4 py-6">
<Outlet />
</div>
</main>
<BottomNav />
<ToastContainer />
</div>
<UserGate>
<div className="flex min-h-screen">
<Sidebar />
<main className="flex-1 pb-20 md:pb-0">
<div className="max-w-2xl mx-auto px-4 py-6">
<Outlet />
</div>
</main>
<BottomNav />
<ToastContainer />
</div>
</UserGate>
);
}