"use client"; import { useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Link from "next/link"; import { Eye, EyeOff, Mail, Lock, User, ArrowRight, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; export default function RegisterPage() { const router = useRouter(); const searchParams = useSearchParams(); const redirect = searchParams.get("redirect") || "/"; const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); if (password !== confirmPassword) { setError("Passwords do not match"); return; } if (password.length < 8) { setError("Password must be at least 8 characters"); return; } setLoading(true); try { const res = await fetch("http://localhost:8000/api/auth/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, email, password }), }); const data = await res.json(); if (!res.ok) { throw new Error(data.detail || "Registration failed"); } // Store tokens localStorage.setItem("token", data.access_token); localStorage.setItem("refresh_token", data.refresh_token); localStorage.setItem("user", JSON.stringify(data.user)); // Redirect router.push(redirect); } catch (err: any) { setError(err.message || "Registration failed"); } finally { setLoading(false); } }; return (
Start translating documents for free