import React, { useState } from 'react'; import { useNavigate, Link } from 'react-router-dom'; import { useAuth } from '../services/AuthContext'; import loginBg from '../../public/images/salesbuddy_logo.jpg'; const Register = () => { const [username, setUsername] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const navigate = useNavigate(); const { login } = useAuth(); const handleSubmit = async (e) => { e.preventDefault(); setError(''); try { const response = await fetch('http://localhost:8000/register', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, email, password, }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.detail || 'Registration failed'); } login(data.access_token); navigate('/'); } catch (err) { setError(err.message); } }; return ( <>
setUsername(e.target.value)} className="mt-1 block w-full rounded-md border-gray-300 shadow-sm p-2" required />
setEmail(e.target.value)} className="mt-1 block w-full rounded-md border-gray-300 shadow-sm p-2" required />
setPassword(e.target.value)} className="mt-1 block w-full rounded-md border-gray-300 shadow-sm p-2" required />
Already have an account? Login
); }; export default Register;