|
2 | 2 |
|
3 | 3 | import { useRouter } from "next/navigation"; |
4 | 4 | import { useState } from "react"; |
| 5 | +import { toast } from "sonner"; |
5 | 6 |
|
6 | 7 | import { Button } from "@/components/ui/button"; |
7 | 8 | import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
8 | 9 | import { Input } from "@/components/ui/input"; |
9 | 10 | import { Label } from "@/components/ui/label"; |
10 | 11 | import { authClient } from "@/lib/auth-client"; |
11 | 12 |
|
| 13 | +function createRandomCredentials() { |
| 14 | + const id = crypto.randomUUID().slice(0, 8); |
| 15 | + const email = `test-${Date.now()}-${id}@example.com`; |
| 16 | + const password = `PayKit-test-${crypto.randomUUID()}`; |
| 17 | + |
| 18 | + return { email, password }; |
| 19 | +} |
| 20 | + |
| 21 | +function formatCredentials(credentials: { email: string; password: string }) { |
| 22 | + return `Email: ${credentials.email}\nPassword: ${credentials.password}`; |
| 23 | +} |
| 24 | + |
| 25 | +async function copyCredentials(credentials: { email: string; password: string }) { |
| 26 | + const text = formatCredentials(credentials); |
| 27 | + |
| 28 | + if (navigator.clipboard) { |
| 29 | + try { |
| 30 | + await navigator.clipboard.writeText(text); |
| 31 | + return; |
| 32 | + } catch { |
| 33 | + // Fall back to the textarea path when clipboard permissions reject writes. |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + const textarea = document.createElement("textarea"); |
| 38 | + textarea.value = text; |
| 39 | + textarea.style.position = "fixed"; |
| 40 | + textarea.style.opacity = "0"; |
| 41 | + document.body.append(textarea); |
| 42 | + textarea.select(); |
| 43 | + |
| 44 | + try { |
| 45 | + if (!document.execCommand("copy")) { |
| 46 | + throw new Error("Clipboard copy failed"); |
| 47 | + } |
| 48 | + } finally { |
| 49 | + textarea.remove(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
12 | 53 | export function AuthForm({ redirectTo }: { redirectTo: string }) { |
13 | 54 | const router = useRouter(); |
14 | 55 | const [email, setEmail] = useState(""); |
15 | 56 | const [password, setPassword] = useState(""); |
16 | 57 | const [isSignUp, setIsSignUp] = useState(false); |
17 | 58 | const [error, setError] = useState(""); |
18 | 59 | const [loading, setLoading] = useState(false); |
| 60 | + const [randomAccountLoading, setRandomAccountLoading] = useState(false); |
19 | 61 |
|
20 | 62 | async function handleSubmit(event: React.FormEvent<HTMLFormElement>) { |
21 | 63 | event.preventDefault(); |
| 64 | + if (loading || randomAccountLoading) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
22 | 68 | setError(""); |
23 | 69 | setLoading(true); |
24 | 70 |
|
@@ -49,6 +95,65 @@ export function AuthForm({ redirectTo }: { redirectTo: string }) { |
49 | 95 | } |
50 | 96 | } |
51 | 97 |
|
| 98 | + async function handleRandomAccount() { |
| 99 | + if (loading || randomAccountLoading) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + setError(""); |
| 104 | + setRandomAccountLoading(true); |
| 105 | + |
| 106 | + const credentials = createRandomCredentials(); |
| 107 | + let credentialsCopied = false; |
| 108 | + |
| 109 | + try { |
| 110 | + setEmail(credentials.email); |
| 111 | + setPassword(credentials.password); |
| 112 | + |
| 113 | + try { |
| 114 | + await copyCredentials(credentials); |
| 115 | + credentialsCopied = true; |
| 116 | + } catch { |
| 117 | + credentialsCopied = false; |
| 118 | + } |
| 119 | + |
| 120 | + const result = await authClient.signUp.email({ |
| 121 | + email: credentials.email, |
| 122 | + password: credentials.password, |
| 123 | + name: "Demo User", |
| 124 | + }); |
| 125 | + |
| 126 | + if (result.error) { |
| 127 | + const message = result.error.message ?? "Random account sign up failed"; |
| 128 | + setError(message); |
| 129 | + toast.error(message, { |
| 130 | + description: credentialsCopied |
| 131 | + ? "Generated credentials were copied, but the account was not created." |
| 132 | + : undefined, |
| 133 | + }); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (credentialsCopied) { |
| 138 | + toast.success("Signed up with a random test account", { |
| 139 | + description: "Credentials copied to clipboard.", |
| 140 | + }); |
| 141 | + } else { |
| 142 | + toast.warning("Signed up with a random test account", { |
| 143 | + description: `Clipboard copy failed. ${formatCredentials(credentials)}`, |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + router.replace(redirectTo); |
| 148 | + } catch (err) { |
| 149 | + const message = err instanceof Error ? err.message : "Something went wrong"; |
| 150 | + setError(message); |
| 151 | + toast.error(message); |
| 152 | + } finally { |
| 153 | + setRandomAccountLoading(false); |
| 154 | + } |
| 155 | + } |
| 156 | + |
52 | 157 | return ( |
53 | 158 | <Card> |
54 | 159 | <CardHeader> |
@@ -80,9 +185,17 @@ export function AuthForm({ redirectTo }: { redirectTo: string }) { |
80 | 185 | /> |
81 | 186 | </div> |
82 | 187 | {error ? <p className="text-destructive text-sm">{error}</p> : null} |
83 | | - <Button disabled={loading} type="submit"> |
| 188 | + <Button disabled={loading || randomAccountLoading} type="submit"> |
84 | 189 | {loading ? "Loading..." : isSignUp ? "Sign up" : "Sign in"} |
85 | 190 | </Button> |
| 191 | + <Button |
| 192 | + disabled={loading || randomAccountLoading} |
| 193 | + onClick={handleRandomAccount} |
| 194 | + type="button" |
| 195 | + variant="secondary" |
| 196 | + > |
| 197 | + {randomAccountLoading ? "Creating account..." : "Sign up with random account"} |
| 198 | + </Button> |
86 | 199 | <Button |
87 | 200 | className="text-muted-foreground" |
88 | 201 | onClick={() => { |
|
0 commit comments