SPB Git

Repository Public

Passkey.tsx 4.4 KB 91 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/components/admin/Passkey.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *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: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";44import { useState } from "react";45import { startAuthentication, startRegistration, WebAuthnError } from "@simplewebauthn/browser";46import { api, ApiClientError } from "./client";4748type Opts<T> = { challengeId: string; options: T };4950function message(e: unknown): string {51  if (e instanceof WebAuthnError || (e instanceof Error && e.name === "NotAllowedError")) {52    const code = e instanceof WebAuthnError ? e.code : "";53    if (code === "ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED") return "This device already has a passkey for ORIX Admin.";54    if (code === "ERROR_INVALID_DOMAIN" || code === "ERROR_INVALID_RP_ID") return "Passkeys only work on the site’s official address, over HTTPS.";55    return "The verification was cancelled or timed out. Touch ID, Face ID or the device passcode is required; try again.";56  }57  if (e instanceof ApiClientError) {58    if (e.code === "locked_out" || e.code === "rate_limited") return "Too many attempts. Wait before trying again.";59    if (e.code === "login_failed") return "Sign-in refused: this passkey is not recognised.";60    if (e.code === "invalid_enroll_token") return "This enrolment link is invalid, expired or already used.";61    return e.message;62  }63  return "An unexpected error occurred.";64}65const deviceName = () => { const ua = navigator.userAgent; return /iPhone/.test(ua) ? "iPhone" : /iPad/.test(ua) ? "iPad" : /Macintosh/.test(ua) ? "Mac" : "Passkey"; };6667export function PasskeyButton({ mode, token }: { mode: "login" | "enroll"; token?: string }) {68  const [busy, setBusy] = useState(false), [error, setError] = useState("");69  const run = async () => {70    setBusy(true); setError("");71    try {72      if (mode === "login") {73        const { challengeId, options } = await api<Opts<Parameters<typeof startAuthentication>[0]["optionsJSON"]>>("/api/auth/login/options", { body: {} });74        await api("/api/auth/login/verify", { body: { challengeId, response: await startAuthentication({ optionsJSON: options }) } });75      } else {76        const { challengeId, options } = await api<Opts<Parameters<typeof startRegistration>[0]["optionsJSON"]>>("/api/auth/register/options", { body: { token } });77        await api("/api/auth/register/verify", { body: { challengeId, token, name: deviceName(), response: await startRegistration({ optionsJSON: options }) } });78      }79      location.assign("/admin");80    } catch (e) { setError(message(e)); setBusy(false); }81  };82  return (83    <div>84      <button type="button" className="btn btn-primary w-full" disabled={busy} onClick={run} data-testid={`passkey-${mode}`}>85        {busy ? "Waiting for your device…" : mode === "login" ? "Sign in with a passkey" : "Create a passkey on this device"}86      </button>87      {error && <p role="alert" className="mt-4 text-sm text-[var(--bad)]" data-testid="passkey-error">{error}</p>}88    </div>89  );90}91
Files main
Menu

Theme