"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; export default function RegisterPage() { const router = useRouter(); const [formData, setFormData] = useState({ username: "", email: "", password: "", confirmPassword: "", }); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); // Validation if (formData.password !== formData.confirmPassword) { setError("Passwords do not match"); return; } if (formData.password.length < 6) { setError("Password must be at least 6 characters long"); return; } setLoading(true); try { const response = await fetch("/api/auth/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username: formData.username, email: formData.email, password: formData.password, }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Registration failed"); } // Success - redirect to login router.push("/login?registered=true"); } catch (err: any) { setError(err.message || "Registration failed. Please try again."); } finally { setLoading(false); } }; return (
Sign up for Location Tracker