diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-12-06 18:36:10 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-12-06 18:36:10 +0900 |
| commit | a2569837aa07ef48f27884fc2869b5be47087a4e (patch) | |
| tree | 5f2236441d751f6a3a7d0865c644e9a8c3259960 /src/client/pages/RegisterPage.tsx | |
| parent | 3923eb2f86c304bbd90c4eae9a338f7bc21c9e90 (diff) | |
| download | kioku-a2569837aa07ef48f27884fc2869b5be47087a4e.tar.gz kioku-a2569837aa07ef48f27884fc2869b5be47087a4e.tar.zst kioku-a2569837aa07ef48f27884fc2869b5be47087a4e.zip | |
feat(client): implement Register page with form validation
Add functional registration form with:
- Username and password fields with confirm password
- Client-side validation (password match, minimum length)
- Error display for API failures
- Redirect to home when already authenticated
- Loading state during submission
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'src/client/pages/RegisterPage.tsx')
| -rw-r--r-- | src/client/pages/RegisterPage.tsx | 99 |
1 files changed, 98 insertions, 1 deletions
diff --git a/src/client/pages/RegisterPage.tsx b/src/client/pages/RegisterPage.tsx index a7fbb59..e6783bd 100644 --- a/src/client/pages/RegisterPage.tsx +++ b/src/client/pages/RegisterPage.tsx @@ -1,8 +1,105 @@ +import { type FormEvent, useEffect, useState } from "react"; +import { Link, useLocation } from "wouter"; +import { ApiClientError, useAuth } from "../stores"; + export function RegisterPage() { + const [, navigate] = useLocation(); + const { register, isAuthenticated } = useAuth(); + const [username, setUsername] = useState(""); + const [password, setPassword] = useState(""); + const [confirmPassword, setConfirmPassword] = useState(""); + const [error, setError] = useState<string | null>(null); + const [isSubmitting, setIsSubmitting] = useState(false); + + // Redirect if already authenticated + useEffect(() => { + if (isAuthenticated) { + navigate("/", { replace: true }); + } + }, [isAuthenticated, navigate]); + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + setError(null); + + if (password !== confirmPassword) { + setError("Passwords do not match"); + return; + } + + if (password.length < 8) { + setError("Password must be at least 8 characters"); + return; + } + + setIsSubmitting(true); + + try { + await register(username, password); + navigate("/", { replace: true }); + } catch (err) { + if (err instanceof ApiClientError) { + setError(err.message); + } else { + setError("Registration failed. Please try again."); + } + } finally { + setIsSubmitting(false); + } + }; + return ( <div> <h1>Register</h1> - <p>Registration page coming soon</p> + <form onSubmit={handleSubmit}> + {error && ( + <div role="alert" style={{ color: "red" }}> + {error} + </div> + )} + <div> + <label htmlFor="username">Username</label> + <input + id="username" + type="text" + value={username} + onChange={(e) => setUsername(e.target.value)} + required + autoComplete="username" + disabled={isSubmitting} + /> + </div> + <div> + <label htmlFor="password">Password</label> + <input + id="password" + type="password" + value={password} + onChange={(e) => setPassword(e.target.value)} + required + autoComplete="new-password" + disabled={isSubmitting} + /> + </div> + <div> + <label htmlFor="confirmPassword">Confirm Password</label> + <input + id="confirmPassword" + type="password" + value={confirmPassword} + onChange={(e) => setConfirmPassword(e.target.value)} + required + autoComplete="new-password" + disabled={isSubmitting} + /> + </div> + <button type="submit" disabled={isSubmitting}> + {isSubmitting ? "Registering..." : "Register"} + </button> + </form> + <p> + Already have an account? <Link href="/login">Login</Link> + </p> </div> ); } |
