From 3bd7b3d5a8c6d42db6cb357fb20e660a5040dda4 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 30 Jun 2026 04:39:34 +0400 Subject: [PATCH 01/13] Add random demo account signup --- apps/demo/src/app/_components/auth-form.tsx | 103 +++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/apps/demo/src/app/_components/auth-form.tsx b/apps/demo/src/app/_components/auth-form.tsx index 24def59e..1444633d 100644 --- a/apps/demo/src/app/_components/auth-form.tsx +++ b/apps/demo/src/app/_components/auth-form.tsx @@ -2,6 +2,7 @@ import { useRouter } from "next/navigation"; import { useState } from "react"; +import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; @@ -9,6 +10,42 @@ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { authClient } from "@/lib/auth-client"; +function createRandomCredentials() { + const id = crypto.randomUUID().slice(0, 8); + const email = `test-${Date.now()}-${id}@example.com`; + const password = `PayKit-test-${crypto.randomUUID()}`; + + return { email, password }; +} + +function formatCredentials(credentials: { email: string; password: string }) { + return `Email: ${credentials.email}\nPassword: ${credentials.password}`; +} + +async function copyCredentials(credentials: { email: string; password: string }) { + const text = formatCredentials(credentials); + + if (navigator.clipboard) { + await navigator.clipboard.writeText(text); + return; + } + + const textarea = document.createElement("textarea"); + textarea.value = text; + textarea.style.position = "fixed"; + textarea.style.opacity = "0"; + document.body.append(textarea); + textarea.select(); + + try { + if (!document.execCommand("copy")) { + throw new Error("Clipboard copy failed"); + } + } finally { + textarea.remove(); + } +} + export function AuthForm({ redirectTo }: { redirectTo: string }) { const router = useRouter(); const [email, setEmail] = useState(""); @@ -16,6 +53,7 @@ export function AuthForm({ redirectTo }: { redirectTo: string }) { const [isSignUp, setIsSignUp] = useState(false); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); + const [randomAccountLoading, setRandomAccountLoading] = useState(false); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); @@ -49,6 +87,61 @@ export function AuthForm({ redirectTo }: { redirectTo: string }) { } } + async function handleRandomAccount() { + setError(""); + setRandomAccountLoading(true); + + const credentials = createRandomCredentials(); + let credentialsCopied = false; + + try { + setEmail(credentials.email); + setPassword(credentials.password); + + try { + await copyCredentials(credentials); + credentialsCopied = true; + } catch { + credentialsCopied = false; + } + + const result = await authClient.signUp.email({ + email: credentials.email, + password: credentials.password, + name: "Demo User", + }); + + if (result.error) { + const message = result.error.message ?? "Random account sign up failed"; + setError(message); + toast.error(message, { + description: credentialsCopied + ? "Generated credentials were copied, but the account was not created." + : undefined, + }); + return; + } + + if (credentialsCopied) { + toast.success("Signed up with a random test account", { + description: "Credentials copied to clipboard.", + }); + } else { + toast.warning("Signed up with a random test account", { + description: `Clipboard copy failed. ${formatCredentials(credentials)}`, + }); + } + + router.replace(redirectTo); + } catch (err) { + const message = err instanceof Error ? err.message : "Something went wrong"; + setError(message); + toast.error(message); + } finally { + setRandomAccountLoading(false); + } + } + return ( @@ -80,9 +173,17 @@ export function AuthForm({ redirectTo }: { redirectTo: string }) { /> {error ?

{error}

: null} - +