"use client" import React, { FormEvent, useState } from "react"; import ReCAPTCHA from "react-google-recaptcha"; import { useRouter } from "next/navigation"; import { useToast } from "../ui/use-toast"; import Link from "next/link"; export const RegisterForm = () => { const [captcha, setCaptcha] = useState(); const router = useRouter(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [confpassword, setConfPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const { toast } = useToast() const validateForm = () => { if (!username || !password || !confpassword) { if(password !== confpassword){ setError('Password and Confirm Password doesnt match'); return false; } setError('Username and password are required'); return false; } setError(''); return true; }; const handleSubmit = async (event: FormEvent) => { event.preventDefault(); setLoading(true); if (captcha) { if (!validateForm()) return; try { const toSend = { username: username, password: password, apisecretkey: process.env.NEXT_PUBLIC_API_SECRET_KEY! } const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL!}/register`, { method: 'POST', headers: { "Content-Type": "application/json", }, body: JSON.stringify(toSend), }); setLoading(false); if (response.ok) { toast({ title: "Registered Successfully", description: "Redirecting to Login", }) router.push('/login'); } else { const errorData = await response.json(); setError(errorData.detail || 'Authentication failed!'); } } catch (error) { setLoading(false); setError('An error occurred. Please try again later.'); } } else { setLoading(false); setError('Recaptcha Failed'); } } return (
logo SurfSense

Create an account

setUsername(e.target.value)} type="username" name="username" id="username" className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="username" />
setPassword(e.target.value)} type="password" name="password" id="password" placeholder="••••••••" className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />
setConfPassword(e.target.value)} type="password" name="confpassword" id="confpassword" placeholder="••••••••" className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />

Already have an account? Login here

{error &&

{error}

}
) }