"use client"; import { useCallback, useEffect, useId, useRef, useState } from "react"; import "./number-match-sign-in.css"; /** * Number Match Sign-in * Two-step sign-in by number matching, with both screens in view. The * laptop shows a number; a moment later the phone beside it lights up with * a request and three numbers to choose from. Pick the one on the laptop * and a pulse travels from the phone to the screen and you're in; pick the * wrong one and a new number is issued. The request expires on a ring. */ type Props = { product?: string; device?: string; place?: string; /** Seconds before a request expires. */ ttl?: number; theme?: "paper" | "night"; motion?: "full" | "reduced"; className?: string; }; type State = "waiting" | "asked" | "approved" | "wrong" | "denied" | "expired"; const pick3 = (n: number) => { const set = new Set([n]); while (set.size < 3) set.add(10 + Math.floor(Math.random() * 89)); return [...set].sort(() => Math.random() - 0.5); }; export function NumberMatchSignIn({ product = "Vitrine", device = "MacBook Air", place = "Muscat, OM", ttl = 60, theme = "paper", motion = "full", className = "" }: Props) { const id = useId(); const [num, setNum] = useState(47); const [opts, setOpts] = useState([83, 47, 12]); const [state, setState] = useState("waiting"); const [left, setLeft] = useState(ttl); const [tries, setTries] = useState(0); const [wrongPick, setWrongPick] = useState(null); const timers = useRef([]); const resultRef = useRef(null); const reduced = () => motion === "reduced" || matchMedia("(prefers-reduced-motion: reduce)").matches; const later = (fn: () => void, ms: number) => { timers.current.push(window.setTimeout(fn, ms)); }; /** Issue a fresh request: new number, new choices, full clock; the phone rings a beat later. */ const issue = useCallback((first = false) => { const n = first ? 47 : 10 + Math.floor(Math.random() * 89); setNum(n); setOpts(first ? [83, 47, 12] : pick3(n)); setLeft(ttl); setWrongPick(null); setState("waiting"); timers.current.push(window.setTimeout(() => setState("asked"), first ? 1200 : 900)); }, [ttl]); useEffect(() => { issue(true); const t = timers.current; return () => t.forEach(clearTimeout); }, [issue]); // The request clock runs while it's open. useEffect(() => { if (state !== "asked" && state !== "waiting" && state !== "wrong") return; if (left <= 0) { setState("expired"); return; } const t = window.setTimeout(() => setLeft((s) => s - 1), 1000); return () => clearTimeout(t); }, [left, state]); useEffect(() => { if (state === "approved" || state === "denied" || state === "expired") resultRef.current?.focus(); }, [state]); const choose = (n: number) => { if (state !== "asked") return; if (n === num) { setState("approved"); return; } setWrongPick(n); setTries((t) => t + 1); setState("wrong"); later(() => issue(), reduced() ? 300 : 1400); }; const ring = Math.max(0, left / ttl); const done = state === "approved"; const end = state === "denied" || state === "expired"; return (
{/* The screen you're signing in on */}

{product}

{!done && !end && (

Approve on your phone

Open the {product} notification and tap this number.

Your number is {num}

{state === "wrong" ? "That didn’t match — here’s a new number." : `Expires in ${left}s`}

)} {done && (

Signed in

Approved from your phone{tries ? ` after ${tries} retr${tries > 1 ? "ies" : "y"}` : ""}. Welcome back.

)} {end && (

{state === "expired" ? "Request expired" : "Sign-in stopped"}

{state === "expired" ? "Nothing was approved in time. Send a new request when your phone is with you." : "No one is signed in. If this wasn’t you, change your password."}

)}
{/* The pulse that crosses from the phone when you approve */} {/* The phone in your hand */}
); }