Backend (FastAPI + PostgreSQL): Chargen mit MHD, FEFO-Auslagern, Einheiten-Umrechnung (Stueck/g/ml + Packungen), Open-Food-Facts-Lookup mit lokalem Fallback, JWT-Auth mit Rollen (Admin/Nutzer), erster Admin beim Setup, Einkaufsliste, Ablaufwarnung, Lagerorte, pytest fuer FEFO. Web-UI (React/Vite): Login, Dashboard, Ein-/Auslagern, Produkte, Lagerorte, Benutzerverwaltung, Einkaufsliste - rollenabhaengig. Deploy: docker-compose + install.sh (Docker-Autoinstall, Secrets), README und Roadmap fuer Schritt 2 (iOS) und Schritt 3. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
import { createContext, useContext, useEffect, useState } from "react";
|
|
import { api, getToken, setToken } from "./api";
|
|
|
|
const AuthContext = createContext(null);
|
|
|
|
export function AuthProvider({ children }) {
|
|
const [user, setUser] = useState(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
async function boot() {
|
|
if (getToken()) {
|
|
try {
|
|
setUser(await api.me());
|
|
} catch {
|
|
setToken(null);
|
|
}
|
|
}
|
|
setLoading(false);
|
|
}
|
|
boot();
|
|
}, []);
|
|
|
|
async function login(username, password) {
|
|
const res = await api.login(username, password);
|
|
setToken(res.access_token);
|
|
const me = await api.me();
|
|
setUser(me);
|
|
return me;
|
|
}
|
|
|
|
function logout() {
|
|
setToken(null);
|
|
setUser(null);
|
|
}
|
|
|
|
const isAdmin = user?.role === "admin";
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ user, isAdmin, loading, login, logout }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAuth() {
|
|
return useContext(AuthContext);
|
|
}
|