SPB Git

Repository Public

page.tsx 3.6 KB 47 lines tsx

1"use client";2import { useEffect, useState } from "react";3import { KeyRound, Loader2 } from "lucide-react";4import { startAuthentication } from "@simplewebauthn/browser";5import { api, ApiError } from "@/lib/client";6import { btnCls, inputCls } from "@/components/ui-static";78export default function Login() {9  const [username, setUsername] = useState("");10  const [password, setPassword] = useState("");11  const [error, setError] = useState<string | null>(null);12  const [busy, setBusy] = useState<"pw" | "pk" | null>(null);13  const [passkeys, setPasskeys] = useState(false);14  useEffect(() => { setPasskeys(typeof window.PublicKeyCredential !== "undefined"); }, []);15  const done = () => { const next = new URLSearchParams(location.search).get("next"); location.href = next && next.startsWith("/") && !next.startsWith("//") ? next : "/"; };1617  const submit = async (e: React.FormEvent) => {18    e.preventDefault(); setBusy("pw"); setError(null);19    try { await api("/auth/login", { body: { username, password } }); done(); }20    catch (err) { setError((err as Error).message); setBusy(null); }21  };22  const passkey = async () => {23    setBusy("pk"); setError(null);24    try {25      const { options, available } = await api<{ options: Parameters<typeof startAuthentication>[0]["optionsJSON"]; available: boolean }>("/auth/passkey/options", { method: "POST" });26      if (!available) throw new Error("Aucune clé d'accès n'est enregistrée. Connectez-vous avec le mot de passe, puis ajoutez-en une dans Admin → Sécurité.");27      const response = await startAuthentication({ optionsJSON: options });28      await api("/auth/passkey/verify", { body: { response } }); done();29    } catch (err) { setError(err instanceof ApiError || !(err as Error).name?.includes("NotAllowed") ? (err as Error).message : "Connexion par clé d'accès annulée."); setBusy(null); }30  };3132  return (33    <main className="grid min-h-dvh place-items-center px-5 py-10" style={{ background: "radial-gradient(80% 50% at 50% 0%, #1b1b1f 0%, #0a0a0b 70%)" }}>34      <div className="w-full max-w-sm">35        <div className="mb-8 text-center"><img src="/icons/icon.svg" alt="" className="mx-auto h-[4.5rem] w-[4.5rem] rounded-[1.35rem] shadow-2xl ring-1 ring-white/10" /><h1 className="mt-5 text-[1.75rem] font-semibold tracking-tight">SP <span className="grad-text">TV</span></h1><p className="mt-1 text-sm text-dim">Télévision personnelle · accès privé</p></div>36        <form onSubmit={submit} className="surface space-y-3 rounded-[1.6rem] p-5">37          <label className="block text-sm font-medium">Identifiant<input className={`${inputCls} mt-1.5`} name="username" autoComplete="username" autoCapitalize="none" autoCorrect="off" spellCheck={false} required value={username} onChange={(e) => setUsername(e.target.value)} /></label>38          <label className="block text-sm font-medium">Mot de passe<input className={`${inputCls} mt-1.5`} name="password" type="password" autoComplete="current-password" required value={password} onChange={(e) => setPassword(e.target.value)} /></label>39          {error && <p role="alert" className="rounded-xl bg-down/10 px-3 py-2 text-sm text-down">{error}</p>}40          <button type="submit" disabled={!!busy} className={`${btnCls} grad w-full`}>{busy === "pw" && <Loader2 className="h-4 w-4 animate-spin" />}Se connecter</button>41          {passkeys && <button type="button" onClick={passkey} disabled={!!busy} className={`${btnCls} w-full bg-white/10 hover:bg-white/15`}>{busy === "pk" ? <Loader2 className="h-4 w-4 animate-spin" /> : <KeyRound className="h-4 w-4" />}Utiliser une clé d'accès</button>}42        </form>43      </div>44    </main>45  );46}47
Files main
Menu

Theme