import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { authAPI } from '../services/api'; import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline'; import LoadingSpinner from '../components/LoadingSpinner'; import toast from 'react-hot-toast'; const Setup = () => { const [formData, setFormData] = useState({ firstName: '', lastName: '', email: '', password: '', confirmPassword: '', phone: '' }); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const { updateUser } = useAuth(); const navigate = useNavigate(); const handleChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleSubmit = async (e) => { e.preventDefault(); setLoading(true); setError(''); // Validation if (formData.password !== formData.confirmPassword) { setError('Passwords do not match'); setLoading(false); return; } if (formData.password.length < 6) { setError('Password must be at least 6 characters long'); setLoading(false); return; } if (!formData.phone || formData.phone.trim() === '') { setError('Phone number is required'); setLoading(false); return; } try { const response = await authAPI.setup({ firstName: formData.firstName, lastName: formData.lastName, email: formData.email, password: formData.password, phone: formData.phone || null }); const { token, user } = response.data; console.log('Setup successful, user:', user); console.log('About to update user in auth context...'); // Store token and set user localStorage.setItem('token', token); // Update the auth context user state directly updateUser(user); console.log('User updated in auth context, about to redirect...'); toast.success('System setup completed successfully!'); // Redirect to dashboard setTimeout(() => { console.log('Redirecting to dashboard...'); navigate('/dashboard'); }, 1000); } catch (error) { console.error('Setup error:', error); const message = error.response?.data?.error || 'Setup failed'; setError(message); toast.error(message); } finally { setLoading(false); } }; return (

CourseWorx Setup

Create your Super Admin account to get started

{error && (

Setup Error

{error}
)}
); }; export default Setup;