Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Vision8 * File : web/src/pages/Setup.tsx9 * Module : pages10 * Repository : https://git.spboucher.ai/maclustr-vision11 * Project Website : https://camera.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { useState } from 'react';43import { api, ApiError } from '../api';44import { Mark } from '../components/Mark';45import { IconLock } from '../icons';4647/** Lit le jeton d'activation dans le fragment de l'URL puis l'efface aussitôt de la barre d'adresse et de l'historique. */48export function takeSetupToken(): string | null {49 if (location.pathname !== '/setup') return null;50 const t = location.hash.slice(1);51 history.replaceState(null, '', '/');52 return /^[A-Za-z0-9_-]{20,100}$/.test(t) ? t : null;53}5455export function SetupPage({ token, onDone }: { token: string; onDone: () => void }) {56 const [busy, setBusy] = useState(false);57 const [key, setKey] = useState<string | null>(null);58 const [err, setErr] = useState<string | null>(null);59 const [copied, setCopied] = useState(false);6061 async function activate() {62 setBusy(true);63 setErr(null);64 try {65 const r = await api<{ key: string }>('/api/auth/enroll', { method: 'POST', body: { token, trust: true }, quiet401: true });66 setKey(r.key);67 } catch (e) {68 const x = e as ApiError;69 setErr(x.status === 401 ? 'Ce lien est invalide, expiré ou déjà utilisé. Demandez-en un nouveau.' : x.status === 429 ? 'Trop de tentatives. Réessayez dans quelques instants.' : 'Activation impossible pour le moment.');70 } finally {71 setBusy(false);72 }73 }7475 return (76 <div className="login">77 <form onSubmit={(e) => e.preventDefault()}>78 <div className="mark">79 <Mark size={76} animated />80 <b>MacLustr Vision</b>81 <span>Activation de l’accès privé</span>82 </div>83 {!key ? (84 <>85 <p className="muted" style={{ textAlign: 'center', fontSize: 14 }}>86 Ce lien à usage unique crée votre clé d’accès personnelle et connecte cet appareil. Toute clé précédente et toutes les sessions seront révoquées.87 </p>88 <button className="btn primary" onClick={() => void activate()} disabled={busy || !!err}>89 <IconLock />90 {busy ? 'Création…' : 'Créer ma clé d’accès'}91 </button>92 <div className="state" role="status" aria-live="polite">93 {err}94 </div>95 {err && (96 <button className="btn" onClick={onDone}>97 Aller à la connexion98 </button>99 )}100 </>101 ) : (102 <>103 <div className="notice warn">Voici votre clé. Elle n’est affichée qu’une fois : rangez-la maintenant dans votre gestionnaire de mots de passe.</div>104 {/* champs reconnus par les gestionnaires de mots de passe pour proposer l'enregistrement */}105 <input type="text" name="username" autoComplete="username" value="proprietaire" readOnly hidden />106 <input type="password" name="password" autoComplete="new-password" value={key} readOnly hidden />107 <div className="keybox" aria-label="Clé d’accès">108 {key}109 </div>110 <button className="btn" onClick={() => void navigator.clipboard?.writeText(key).then(() => setCopied(true))}>111 {copied ? 'Clé copiée' : 'Copier la clé'}112 </button>113 <button className="btn primary" onClick={onDone}>114 J’ai rangé la clé — ouvrir Vision115 </button>116 </>117 )}118 </form>119 </div>120 );121}122