import React, { useState, useEffect } from 'react'; import { Link, useNavigate, useLocation } from 'react-router-dom'; import { useForm } from 'react-hook-form'; import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline'; import { useAuth } from '../../hooks/useAuth'; import LoadingSpinner from '../../components/UI/LoadingSpinner'; import apiClient from '../../services/api'; const Login = () => { const [showPassword, setShowPassword] = useState(false); const [registrationEnabled, setRegistrationEnabled] = useState(true); const [settingsLoading, setSettingsLoading] = useState(true); const { login, loading } = useAuth(); const navigate = useNavigate(); const location = useLocation(); const from = location.state?.from?.pathname || '/dashboard'; const { register, handleSubmit, formState: { errors }, setError, } = useForm(); // Fetch registration settings useEffect(() => { const fetchRegistrationSetting = async () => { try { const response = await apiClient.get('/auth/registration-status'); setRegistrationEnabled(response.data.data.enabled); } catch (error) { // If there's an error, default to allowing registration console.error('Failed to fetch registration setting:', error); setRegistrationEnabled(true); } finally { setSettingsLoading(false); } }; fetchRegistrationSetting(); }, []); const onSubmit = async (data) => { console.log('Login form submitted:', data.email); const result = await login(data); console.log('Login result:', result); if (result.success) { console.log('Login successful, navigating to:', from); navigate(from, { replace: true }); } else { console.log('Login failed, showing error:', result.error); setError('root', { type: 'manual', message: result.error }); } }; const handleAuthentikLogin = () => { // Redirect to Authentik OAuth endpoint window.location.href = `${process.env.REACT_APP_API_URL || 'http://localhost:5000'}/api/auth/authentik`; }; return (

Sign in to your account

{!settingsLoading && registrationEnabled && (

Or{' '} create a new account

)}
{errors.root && (

{errors.root.message}

)}
{errors.email && (

{errors.email.message}

)}
{errors.password && (

{errors.password.message}

)}
Forgot your password?
Or continue with
); }; export default Login;