SPB Git

Repository Public

client.ts 1.5 KB 26 lines typescript

1import { apiPost, type Schemas } from "@/api/client";2import { getPasskey } from "@/lib/webauthn";3import { unwrapPublicKey } from "@/shell/stepup";45/** Human label of this browser for the device list ("Chrome · macOS"). Not an identifier. */6export function clientInfo(): Schemas["ClientInfo"] {7  const ua = typeof navigator === "undefined" ? "" : navigator.userAgent;8  const browser = /Edg\//.test(ua) ? "Edge" : /Firefox\//.test(ua) ? "Firefox" : /Chrome\//.test(ua) ? "Chrome" : /Safari\//.test(ua) ? "Safari" : "Web";9  const os = /iPhone|iPad/.test(ua) ? "iOS" : /Mac OS X/.test(ua) ? "macOS" : /Android/.test(ua) ? "Android" : /Windows/.test(ua) ? "Windows" : /Linux/.test(ua) ? "Linux" : "";10  return { kind: "web", label: os ? `${browser} · ${os}` : browser };11}1213export interface PendingChallenge {14  challenge_id: string;15  publicKey: Record<string, unknown>;16}17export async function beginSignIn(): Promise<PendingChallenge> {18  const c = await apiPost("/auth/webauthn/authentication/begin");19  return { challenge_id: c.challenge_id, publicKey: unwrapPublicKey(c.public_key) };20}21/** Passkey assertion + session opening. The challenge may come from a conditional-mediation attempt. */22export async function finishSignIn(challenge: PendingChallenge, opts: { conditional?: boolean; signal?: AbortSignal } = {}): Promise<Schemas["AuthResult"]> {23  const credential = await getPasskey(challenge.publicKey, opts);24  return apiPost("/auth/webauthn/authentication/finish", { body: { challenge_id: challenge.challenge_id, credential, client: clientInfo() } });25}26
Files main
Menu

Theme