Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/social-web/src/screens/Auth.tsx9 * Module : screens10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *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 { useEffect, useState, type ReactNode } from 'react';43import { consumeHandoff, passkeyLogin, passkeyRegister } from '../../../shared/api.ts';44import { LANGS } from '../i18n.ts';45import { usePrefs } from '../prefs.tsx';46import { ActionStatus, ErrorNote, Field, useAction } from '../ui/core.tsx';4748/** Frame for the pages shown before a session exists. */49export function PublicFrame({ title, children }: { title: string; children: ReactNode }) {50 const { t, lang, setLang } = usePrefs();51 return (52 <div className="public">53 <main className="public-card">54 <p className="public-mark">{t('app.name')}<span>{t('app.tagline')}</span></p>55 <h1>{title}</h1>56 {children}57 <p className="public-lang">{LANGS.map((l) => <button key={l} type="button" className="link-btn" aria-pressed={lang === l} onClick={() => setLang(l)}>{t(l === 'fr' ? 'c.lang.fr' : 'c.lang.en')}</button>)}</p>58 </main>59 </div>60 );61}6263export function SignIn({ onSignedIn }: { onSignedIn: () => Promise<void> }) {64 const { t } = usePrefs();65 const a = useAction();66 const go = () => a.run(async () => { await passkeyLogin(); await onSignedIn(); });67 return (68 <PublicFrame title={t('auth.title')}>69 <p className="lead">{t('auth.lead')}</p>70 <p><button type="button" className="primary big" onClick={go} disabled={a.busy}>{t('auth.signin')}</button></p>71 <ActionStatus a={a} />72 <p className="small">{t('auth.noAccount')}</p>73 </PublicFrame>74 );75}7677/** /setup#<token> — the token stays in the fragment, so it is never sent in a request line or a Referer. */78export function Setup() {79 const { t } = usePrefs();80 const [token] = useState(() => window.location.hash.replace(/^#/, ''));81 const [label, setLabel] = useState('');82 const a = useAction();83 const go = () => a.run(async () => { await passkeyRegister(token, label.trim()); window.location.replace('/'); });84 return (85 <PublicFrame title={t('auth.setup.title')}>86 <p className="lead">{t('auth.setup.lead')}</p>87 {!token ? <div className="note note-error" role="alert"><p>{t('auth.setup.missing')}</p></div> : (88 <form onSubmit={(e) => { e.preventDefault(); void go(); }}>89 <Field label={t('auth.setup.label')} wide><input value={label} onChange={(e) => setLabel(e.target.value)} maxLength={60} autoComplete="off" /></Field>90 <p><button type="submit" className="primary big" disabled={a.busy}>{t('auth.setup.go')}</button></p>91 </form>92 )}93 <ActionStatus a={a} />94 </PublicFrame>95 );96}9798// A ticket is single-use: the exchange must happen once even if the component mounts twice.99const exchanges = new Map<string, Promise<void>>();100/** /handoff#<ticket> */101export function Handoff() {102 const { t } = usePrefs();103 const [ticket] = useState(() => window.location.hash.replace(/^#/, ''));104 const [error, setError] = useState<unknown>(null);105 useEffect(() => {106 if (!ticket) return;107 let p = exchanges.get(ticket);108 if (!p) { p = consumeHandoff(ticket); exchanges.set(ticket, p); }109 let live = true;110 p.then(() => { window.location.replace('/'); }, (e) => { if (live) setError(e); });111 return () => { live = false; };112 }, [ticket]);113 return (114 <PublicFrame title={t('auth.handoff.title')}>115 {!ticket ? <div className="note note-error" role="alert"><p>{t('auth.handoff.missing')}</p></div> : error ? <ErrorNote error={error} /> : <p role="status" aria-live="polite">{t('auth.handoff.working')}</p>}116 {!ticket || error ? <p><a href="/">{t('auth.handoff.retry')}</a></p> : null}117 </PublicFrame>118 );119}120