"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 (

Create Account

Sign up for Location Tracker

setFormData({ ...formData, username: e.target.value })} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" required minLength={3} />

At least 3 characters

setFormData({ ...formData, email: e.target.value })} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" required />
setFormData({ ...formData, password: e.target.value })} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" required minLength={6} />

At least 6 characters

setFormData({ ...formData, confirmPassword: e.target.value })} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" required minLength={6} />
{error && (
{error}
)}
Already have an account? Sign in
← Back to Home
); }