feat(auth): add form login with session

This commit is contained in:
u1
2026-01-06 16:05:23 +01:00
parent 77122e0428
commit e20a1f5198
6 changed files with 472 additions and 66 deletions

View File

@@ -1,53 +1,18 @@
import { useEffect, useState } from 'react';
import Button from '../ui/Button';
type WhoamiResponse = {
ok?: boolean;
user?: string | null;
type Props = {
user: string;
onLogout: () => void;
};
export default function AuthStatus() {
const [user, setUser] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
let cancelled = false;
fetch('/whoami', { cache: 'no-store' })
.then(async (res) => {
const json = (await res.json().catch(() => null)) as WhoamiResponse | null;
const u = typeof json?.user === 'string' ? json.user.trim() : '';
return u || null;
})
.then((u) => {
if (cancelled) return;
setUser(u);
})
.catch(() => {
if (cancelled) return;
setUser(null);
})
.finally(() => {
if (cancelled) return;
setLoading(false);
});
return () => {
cancelled = true;
};
}, []);
const logout = () => {
window.location.href = '/logout';
};
export default function AuthStatus({ user, onLogout }: Props) {
return (
<div className="authStatus">
<div className="authStatus__user" aria-label="Zalogowany użytkownik">
<div className="authStatus__userLabel">Zalogowany</div>
<div className="authStatus__userName">{loading ? '…' : user || '—'}</div>
<div className="authStatus__userName">{user}</div>
</div>
<Button size="sm" variant="ghost" type="button" onClick={logout}>
<Button size="sm" variant="ghost" type="button" onClick={onLogout}>
Wyloguj
</Button>
</div>