Repository Public
1// Écrans d'authentification : connexion par clé d'accès (un seul bouton) et enrôlement par lien à usage unique.2import { useEffect, useId, useState, type ReactNode } from "react"3import { t } from "../i18n"4import { S } from "../state/app"5import { authErrorMessage, enroll, enrollCheck, signIn, type EnrollInfo } from "../state/session"6import { useStore } from "../state/store"7import { Icon, Spinner } from "./kit"89function AuthCard({ children, title, subtitle }: { children: ReactNode; title: string; subtitle?: string }) {10 return (11 <main className="auth" id="main">12 <div className="auth-card">13 <div className="auth-brand" aria-hidden="true">14 <Icon name="mail" />15 </div>16 <h1>{title}</h1>17 {subtitle ? <p className="muted">{subtitle}</p> : null}18 {children}19 </div>20 <p className="auth-foot muted small">SP Mail</p>21 </main>22 )23}2425export function SignIn() {26 const expired = useStore(S, (s) => s.expired)27 const hasDraft = useStore(S, (s) => !!s.draft)28 const [busy, setBusy] = useState(false)29 const [error, setError] = useState<string | null>(null)30 const supported = typeof window.PublicKeyCredential !== "undefined"31 const go = async () => {32 setBusy(true)33 setError(null)34 try {35 await signIn()36 } catch (e) {37 setError(authErrorMessage(e))38 } finally {39 setBusy(false)40 }41 }42 return (43 <AuthCard title={t("auth.title")} subtitle={t("auth.subtitle")}>44 {expired ? (45 <div className="banner warn" role="status">46 <Icon name="clock" small />47 <span>48 {t("auth.expired")}49 {hasDraft ? ` ${t("auth.expiredDraftKept")}` : ""}50 </span>51 </div>52 ) : null}53 {!supported ? (54 <div className="banner danger" role="alert">55 <Icon name="warn" small />56 <span>{t("auth.unsupported")}</span>57 </div>58 ) : null}59 <button type="button" className="btn primary block auth-main" onClick={() => void go()} disabled={busy || !supported} autoFocus>60 {busy ? <span className="spinner" aria-hidden="true" /> : <Icon name="key" />}61 <span>{busy ? t("auth.waiting") : t("auth.signIn")}</span>62 </button>63 {error ? (64 <div className="banner danger" role="alert">65 <Icon name="warn" small />66 <span>{error}</span>67 </div>68 ) : null}69 <p className="muted small">{t("auth.help")}</p>70 </AuthCard>71 )72}7374export function Enroll() {75 const [token] = useState(() => decodeURIComponent(location.hash.replace(/^#/, "")).trim())76 const [info, setInfo] = useState<EnrollInfo | null>(null)77 const [checkError, setCheckError] = useState<string | null>(null)78 const [handle, setHandle] = useState("")79 const [name, setName] = useState("")80 const [label, setLabel] = useState("")81 const [busy, setBusy] = useState(false)82 const [error, setError] = useState<string | null>(null)83 const idHandle = useId()84 const idName = useId()85 const idLabel = useId()8687 useEffect(() => {88 let alive = true89 if (!token) {90 setCheckError(t("auth.invalidToken"))91 return92 }93 enrollCheck(token)94 .then((r) => alive && setInfo(r))95 .catch((e) => alive && setCheckError(authErrorMessage(e)))96 return () => {97 alive = false98 }99 }, [token])100101 const needsIdentity = info !== null && info.purpose !== "recover" && !info.user102 const handleOk = /^[a-z0-9_.-]{2,32}$/.test(handle.trim().toLowerCase())103 const canSubmit = !!info && !busy && (!needsIdentity || (handleOk && name.trim().length > 0))104105 const submit = async () => {106 if (!canSubmit) return107 setBusy(true)108 setError(null)109 try {110 await enroll(token, needsIdentity ? handle.trim().toLowerCase() : undefined, needsIdentity ? name.trim() : undefined, label.trim() || undefined)111 } catch (e) {112 setError(authErrorMessage(e))113 } finally {114 setBusy(false)115 }116 }117118 if (checkError)119 return (120 <AuthCard title={t("enroll.title")}>121 <div className="banner danger" role="alert">122 <Icon name="warn" small />123 <span>{checkError}</span>124 </div>125 <p className="muted small">{t("enroll.askNewLink")}</p>126 <a className="btn block" href="/">127 {t("enroll.goSignIn")}128 </a>129 </AuthCard>130 )131 if (!info)132 return (133 <AuthCard title={t("enroll.title")}>134 <Spinner label={t("enroll.checking")} />135 </AuthCard>136 )137 const subtitle = info.purpose === "bootstrap" ? t("enroll.bootstrapIntro") : info.purpose === "recover" ? t("enroll.recoverIntro", { name: info.user?.display_name ?? "" }) : t("enroll.inviteIntro")138 return (139 <AuthCard title={info.purpose === "recover" ? t("enroll.recoverTitle") : t("enroll.title")} subtitle={subtitle}>140 <form141 className="stack"142 onSubmit={(e) => {143 e.preventDefault()144 void submit()145 }}146 >147 {needsIdentity ? (148 <>149 <div className="field">150 <label htmlFor={idName}>{t("enroll.displayName")}</label>151 <input id={idName} className="input" value={name} onChange={(e) => setName(e.target.value)} maxLength={64} autoComplete="name" required autoFocus />152 </div>153 <div className="field">154 <label htmlFor={idHandle}>{t("enroll.handle")}</label>155 <input id={idHandle} className="input" value={handle} onChange={(e) => setHandle(e.target.value)} maxLength={32} autoComplete="username" autoCapitalize="none" spellCheck={false} required aria-invalid={handle !== "" && !handleOk} aria-describedby={`${idHandle}-hint`} />156 <span className="hint" id={`${idHandle}-hint`}>157 {t("enroll.handleHint")}158 </span>159 </div>160 </>161 ) : null}162 <div className="field">163 <label htmlFor={idLabel}>{t("enroll.passkeyLabel")}</label>164 <input id={idLabel} className="input" value={label} onChange={(e) => setLabel(e.target.value)} maxLength={60} placeholder={t("enroll.passkeyLabelPlaceholder")} />165 </div>166 <button type="submit" className="btn primary block auth-main" disabled={!canSubmit}>167 {busy ? <span className="spinner" aria-hidden="true" /> : <Icon name="key" />}168 <span>{busy ? t("auth.waiting") : t("enroll.create")}</span>169 </button>170 {error ? (171 <div className="banner danger" role="alert">172 <Icon name="warn" small />173 <span>{error}</span>174 </div>175 ) : null}176 <p className="muted small">{t("enroll.help")}</p>177 </form>178 </AuthCard>179 )180}181