deploy / deploy (push) Successful in 48s
- Site à part (admin/, build:admin → dist-admin/), jamais dans l'app publique : tableau de bord, membres (suspendre / réactiver), avis (retirer), commentaires (supprimer), journal. Motif obligatoire. Session en mémoire seulement, 1 h maximum, déconnexion après 15 min d'inactivité. - Connexion : mot de passe + code de double authentification obligatoire (jeton aal2). - Fonction Edge « admin » : secret X-Admin-Gate (ajouté par nginx sur le VPS, sinon 404), jeton aal2, liste admin.admins ; accès à la base par connexion directe. Alertes e-mail si SMTP_PASSWORD est configuré. - Schéma « admin » (20261130150000, appliqué) non exposé par l'API : admins, journal en ajout seulement (modification, suppression et vidage refusés), fonctions de la console. - nginx : admin.conf sur 127.0.0.1:8081 (CSP stricte, sans cache, noindex), relais vers la fonction avec le secret lu dans /etc/nginx/bobinoo-admin-gate.conf (hors dépôt). Publié par « tailscale serve ». - Déploiement : build de la console et copie dans chaque release. Guide : docs/admin.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
178 lines
7.4 KiB
TypeScript
178 lines
7.4 KiB
TypeScript
import { LogOut, ShieldCheck } from 'lucide-react'
|
|
import { useCallback, useEffect, useRef, useState, type FormEvent } from 'react'
|
|
import { ApiError, call, type Card } from './api'
|
|
import { AuthError, expiresAt, signIn, signOut, verifyCode } from './auth'
|
|
import { Comments, Dashboard, Journal, Members, Reviews } from './screens'
|
|
|
|
/** Déconnexion automatique après 15 minutes sans toucher à la console. */
|
|
const IDLE_MS = 15 * 60_000
|
|
|
|
type Tab = 'dashboard' | 'members' | 'reviews' | 'comments' | 'journal'
|
|
const TABS: { id: Tab; label: string }[] = [
|
|
{ id: 'dashboard', label: 'Tableau de bord' },
|
|
{ id: 'members', label: 'Membres' },
|
|
{ id: 'reviews', label: 'Avis' },
|
|
{ id: 'comments', label: 'Commentaires' },
|
|
{ id: 'journal', label: 'Journal' },
|
|
]
|
|
|
|
export default function App() {
|
|
// Jeton gardé en mémoire seulement : recharger la page déconnecte
|
|
const [session, setSession] = useState<{ token: string; me: Card }>()
|
|
const [notice, setNotice] = useState<string>()
|
|
|
|
const logout = useCallback((why?: string) => {
|
|
setSession((s) => {
|
|
if (s) void signOut(s.token)
|
|
return undefined
|
|
})
|
|
setNotice(why)
|
|
}, [])
|
|
|
|
// Fin de validité du jeton, inactivité : retour à l'écran de connexion
|
|
useEffect(() => {
|
|
if (!session) return
|
|
const end = window.setTimeout(() => logout('Session terminée (1 h) : reconnecte-toi.'), Math.max(0, expiresAt(session.token) - Date.now() - 30_000))
|
|
let idle = window.setTimeout(() => logout('Déconnecté après 15 minutes sans activité.'), IDLE_MS)
|
|
const touch = () => {
|
|
window.clearTimeout(idle)
|
|
idle = window.setTimeout(() => logout('Déconnecté après 15 minutes sans activité.'), IDLE_MS)
|
|
}
|
|
for (const ev of ['pointerdown', 'keydown'] as const) window.addEventListener(ev, touch)
|
|
return () => {
|
|
window.clearTimeout(end)
|
|
window.clearTimeout(idle)
|
|
for (const ev of ['pointerdown', 'keydown'] as const) window.removeEventListener(ev, touch)
|
|
}
|
|
}, [session, logout])
|
|
|
|
if (!session) return <Login notice={notice} onReady={(token, me) => (setNotice(undefined), setSession({ token, me }))} />
|
|
return <Console token={session.token} me={session.me} onLogout={logout} />
|
|
}
|
|
|
|
function Login({ notice, onReady }: { notice?: string; onReady: (token: string, me: Card) => void }) {
|
|
const [step, setStep] = useState<{ token: string; factorId: string }>()
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [code, setCode] = useState('')
|
|
const [busy, setBusy] = useState(false)
|
|
const [error, setError] = useState<string>()
|
|
const codeRef = useRef<HTMLInputElement>(null)
|
|
|
|
const submit = async (e: FormEvent) => {
|
|
e.preventDefault()
|
|
if (busy) return
|
|
setBusy(true)
|
|
setError(undefined)
|
|
try {
|
|
if (!step) {
|
|
const r = await signIn(email, password)
|
|
setPassword('')
|
|
if (!r.factorId) {
|
|
await signOut(r.token)
|
|
throw new AuthError("Ce compte n'a pas la double authentification. Active-la d'abord dans l'app (Réglages → Mon compte), elle est obligatoire ici.")
|
|
}
|
|
setStep({ token: r.token, factorId: r.factorId })
|
|
setTimeout(() => codeRef.current?.focus(), 0)
|
|
} else {
|
|
const token = await verifyCode(step.token, step.factorId, code.replace(/\s/g, ''))
|
|
const me = await call<Card>(token, 'hello')
|
|
onReady(token, me)
|
|
}
|
|
} catch (err) {
|
|
setError(err instanceof AuthError || err instanceof ApiError ? err.message : 'Erreur inattendue.')
|
|
if (step) setCode('')
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<main className="mx-auto flex min-h-dvh max-w-sm flex-col justify-center px-5 py-10">
|
|
<div className="flex items-center gap-2 text-accent">
|
|
<ShieldCheck size={22} />
|
|
<span className="text-sm font-semibold uppercase tracking-wider">Console</span>
|
|
</div>
|
|
<h1 className="mt-2 text-3xl font-bold">Bobinoo</h1>
|
|
<p className="mt-2 text-sm text-ink-3">Accès réservé. Chaque connexion est inscrite au journal et signalée par e-mail.</p>
|
|
{notice && <p className="mt-5 border-s-2 border-accent ps-3 text-sm text-ink-2">{notice}</p>}
|
|
<form onSubmit={submit} className="mt-7 space-y-3">
|
|
{!step ? (
|
|
<>
|
|
<input className="field" type="email" autoComplete="username" placeholder="E-mail" value={email} onChange={(e) => setEmail(e.target.value)} required />
|
|
<input className="field" type="password" autoComplete="current-password" placeholder="Mot de passe" value={password} onChange={(e) => setPassword(e.target.value)} required />
|
|
</>
|
|
) : (
|
|
<>
|
|
<p className="text-sm text-ink-2">Code à 6 chiffres de ton application d'authentification :</p>
|
|
<input
|
|
ref={codeRef}
|
|
className="field text-center text-2xl tracking-[0.4em]"
|
|
inputMode="numeric"
|
|
autoComplete="one-time-code"
|
|
pattern="[0-9 ]{6,7}"
|
|
maxLength={7}
|
|
value={code}
|
|
onChange={(e) => setCode(e.target.value)}
|
|
required
|
|
/>
|
|
</>
|
|
)}
|
|
{error && <p className="text-sm text-danger">{error}</p>}
|
|
<button className="btn btn-primary w-full" disabled={busy}>
|
|
{busy ? '…' : step ? 'Valider le code' : 'Continuer'}
|
|
</button>
|
|
{step && (
|
|
<button type="button" className="btn w-full text-ink-3" onClick={() => (void signOut(step.token), setStep(undefined), setCode(''))}>
|
|
Annuler
|
|
</button>
|
|
)}
|
|
</form>
|
|
</main>
|
|
)
|
|
}
|
|
|
|
function Console({ token, me, onLogout }: { token: string; me: Card; onLogout: (why?: string) => void }) {
|
|
const [tab, setTab] = useState<Tab>('dashboard')
|
|
// Erreur de session renvoyée par le serveur : retour à la connexion
|
|
const onAuthError = useCallback(
|
|
(e: unknown) => {
|
|
if (e instanceof ApiError && (e.status === 401 || e.status === 403)) onLogout(e.message)
|
|
},
|
|
[onLogout],
|
|
)
|
|
const props = { token, onAuthError }
|
|
return (
|
|
<div className="mx-auto min-h-dvh max-w-5xl px-4 pb-16 sm:px-6">
|
|
<header className="sticky top-0 z-10 -mx-4 border-b border-line bg-bg px-4 pt-[max(0.75rem,env(safe-area-inset-top))] sm:-mx-6 sm:px-6">
|
|
<div className="flex items-center gap-3">
|
|
<ShieldCheck size={20} className="text-accent" />
|
|
<span className="font-bold">Console Bobinoo</span>
|
|
<span className="ms-auto truncate text-sm text-ink-3">@{me.username}</span>
|
|
<button onClick={() => onLogout('Déconnecté.')} className="grid size-9 place-items-center text-ink-2 active:text-ink" aria-label="Se déconnecter">
|
|
<LogOut size={18} />
|
|
</button>
|
|
</div>
|
|
<nav className="-mx-1 mt-2 flex gap-1 overflow-x-auto pb-2">
|
|
{TABS.map((t) => (
|
|
<button
|
|
key={t.id}
|
|
onClick={() => setTab(t.id)}
|
|
className={`shrink-0 rounded-md px-3 py-1.5 text-sm font-medium ${tab === t.id ? 'bg-surface-2 text-ink' : 'text-ink-3'}`}
|
|
>
|
|
{t.label}
|
|
</button>
|
|
))}
|
|
</nav>
|
|
</header>
|
|
<main className="pt-5">
|
|
{tab === 'dashboard' && <Dashboard {...props} />}
|
|
{tab === 'members' && <Members {...props} meId={me.id} />}
|
|
{tab === 'reviews' && <Reviews {...props} />}
|
|
{tab === 'comments' && <Comments {...props} />}
|
|
{tab === 'journal' && <Journal {...props} />}
|
|
</main>
|
|
</div>
|
|
)
|
|
}
|