cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / components / CredentialsAuthForm.tsx
src/components/CredentialsAuthForm.tsx · 171 lines · 4.9 KB← back to folder
| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | import { useRouter } from "next/navigation"; |
| 5 | |
| 6 | export function CredentialsAuthForm({ next }: { next: string }) { |
| 7 | const router = useRouter(); |
| 8 | const [mode, setMode] = useState<"login" | "register">("login"); |
| 9 | const [identifier, setIdentifier] = useState(""); |
| 10 | const [username, setUsername] = useState(""); |
| 11 | const [email, setEmail] = useState(""); |
| 12 | const [password, setPassword] = useState(""); |
| 13 | const [loading, setLoading] = useState(false); |
| 14 | const [error, setError] = useState<string | null>(null); |
| 15 | |
| 16 | async function handleSubmit(e: React.FormEvent) { |
| 17 | e.preventDefault(); |
| 18 | setError(null); |
| 19 | setLoading(true); |
| 20 | |
| 21 | try { |
| 22 | const endpoint = mode === "login" ? "/api/auth/login" : "/api/auth/register"; |
| 23 | const payload = |
| 24 | mode === "login" |
| 25 | ? { identifier, password } |
| 26 | : { username, email, password }; |
| 27 | |
| 28 | const res = await fetch(endpoint, { |
| 29 | method: "POST", |
| 30 | headers: { "Content-Type": "application/json" }, |
| 31 | body: JSON.stringify(payload), |
| 32 | }); |
| 33 | |
| 34 | const data = (await res.json().catch(() => ({}))) as { |
| 35 | user?: { id?: string; username?: string; email?: string }; |
| 36 | error?: string; |
| 37 | }; |
| 38 | if (!res.ok) { |
| 39 | setError(data.error || "Authentication failed."); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (data.user?.id) { |
| 44 | window.minilytics?.("identify", { |
| 45 | user_id: data.user.id, |
| 46 | email: data.user.email ?? (mode === "register" ? email : undefined), |
| 47 | name: data.user.username, |
| 48 | }); |
| 49 | } |
| 50 | if (mode === "register") { |
| 51 | window.minilytics?.("goal", "signup_completed"); |
| 52 | } |
| 53 | |
| 54 | router.push(next); |
| 55 | router.refresh(); |
| 56 | } catch { |
| 57 | setError("Network error. Please try again."); |
| 58 | } finally { |
| 59 | setLoading(false); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return ( |
| 64 | <div style={{ maxWidth: 380 }}> |
| 65 | <div className="tabs-row" style={{ marginBottom: 18 }}> |
| 66 | <button |
| 67 | type="button" |
| 68 | className="link-button" |
| 69 | style={{ fontWeight: mode === "login" ? 700 : 400 }} |
| 70 | onClick={() => { |
| 71 | setMode("login"); |
| 72 | setError(null); |
| 73 | }} |
| 74 | > |
| 75 | sign in |
| 76 | </button> |
| 77 | <span className="nav-sep">/</span> |
| 78 | <button |
| 79 | type="button" |
| 80 | className="link-button" |
| 81 | style={{ fontWeight: mode === "register" ? 700 : 400 }} |
| 82 | onClick={() => { |
| 83 | setMode("register"); |
| 84 | setError(null); |
| 85 | }} |
| 86 | > |
| 87 | create account |
| 88 | </button> |
| 89 | </div> |
| 90 | |
| 91 | {error && ( |
| 92 | <div className="form-error" role="alert"> |
| 93 | {error} |
| 94 | </div> |
| 95 | )} |
| 96 | |
| 97 | <form onSubmit={handleSubmit}> |
| 98 | {mode === "login" ? ( |
| 99 | <div className="form-field"> |
| 100 | <label htmlFor="identifier">Email or username</label> |
| 101 | <input |
| 102 | id="identifier" |
| 103 | type="text" |
| 104 | autoComplete="username" |
| 105 | required |
| 106 | value={identifier} |
| 107 | onChange={(e) => setIdentifier(e.target.value)} |
| 108 | placeholder="you@example.com" |
| 109 | /> |
| 110 | </div> |
| 111 | ) : ( |
| 112 | <> |
| 113 | <div className="form-field"> |
| 114 | <label htmlFor="reg-username">Username</label> |
| 115 | <input |
| 116 | id="reg-username" |
| 117 | type="text" |
| 118 | autoComplete="username" |
| 119 | required |
| 120 | minLength={2} |
| 121 | maxLength={39} |
| 122 | value={username} |
| 123 | onChange={(e) => setUsername(e.target.value)} |
| 124 | placeholder="arden" |
| 125 | /> |
| 126 | <p className="form-hint">Lowercase letters, numbers, hyphens, or underscores.</p> |
| 127 | </div> |
| 128 | |
| 129 | <div className="form-field"> |
| 130 | <label htmlFor="reg-email">Email</label> |
| 131 | <input |
| 132 | id="reg-email" |
| 133 | type="email" |
| 134 | autoComplete="email" |
| 135 | required |
| 136 | value={email} |
| 137 | onChange={(e) => setEmail(e.target.value)} |
| 138 | placeholder="you@example.com" |
| 139 | /> |
| 140 | </div> |
| 141 | </> |
| 142 | )} |
| 143 | |
| 144 | <div className="form-field"> |
| 145 | <label htmlFor="password">Password</label> |
| 146 | <input |
| 147 | id="password" |
| 148 | type="password" |
| 149 | autoComplete={mode === "login" ? "current-password" : "new-password"} |
| 150 | required |
| 151 | minLength={mode === "register" ? 8 : 1} |
| 152 | value={password} |
| 153 | onChange={(e) => setPassword(e.target.value)} |
| 154 | placeholder={mode === "register" ? "At least 8 characters" : ""} |
| 155 | /> |
| 156 | </div> |
| 157 | |
| 158 | <button type="submit" className="btn" disabled={loading}> |
| 159 | {loading |
| 160 | ? mode === "login" |
| 161 | ? "Signing in..." |
| 162 | : "Creating account..." |
| 163 | : mode === "login" |
| 164 | ? "Sign in" |
| 165 | : "Create account"} |
| 166 | </button> |
| 167 | </form> |
| 168 | </div> |
| 169 | ); |
| 170 | } |
| 171 |