import { useNavigate, useLocation } from 'react-router-dom'; import { useState, useEffect } from 'react'; import { FaGoogle, FaMicrosoft } from 'react-icons/fa'; import { useAuth, useMsalAuth } from '../hooks/useAuthentication'; import styles from './Login.module.css'; function Login() { const navigate = useNavigate(); const location = useLocation(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [usernameFocused, setUsernameFocused] = useState(false); const [passwordFocused, setPasswordFocused] = useState(false); const { login, error: loginError, isLoading: isLoginLoading } = useAuth(); const { loginWithMsal, error: msalError, isLoading: isMsalLoading } = useMsalAuth(); // Get the page the user was trying to visit const from = location.state?.from?.pathname || "/"; // Set page title useEffect(() => { document.title = "PowerOn AI Platform - Login"; }, []); // Check for autofilled inputs useEffect(() => { const checkAutofill = () => { const usernameInput = document.querySelector('input[type="text"]') as HTMLInputElement; const passwordInput = document.querySelector('input[type="password"]') as HTMLInputElement; if (usernameInput && usernameInput.value) { setUsername(usernameInput.value); } if (passwordInput && passwordInput.value) { setPassword(passwordInput.value); } }; // Check immediately and after a short delay checkAutofill(); const timer = setTimeout(checkAutofill, 100); return () => clearTimeout(timer); }, []); const handleMsalLogin = async () => { try { console.log("Attempting MSAL login..."); const response = await loginWithMsal(); console.log("MSAL login successful:", response); navigate(from, { replace: true }); } catch (error) { console.error("MSAL login failed:", error); } }; const handleCredentialLogin = async (e?: React.MouseEvent) => { e?.preventDefault(); // Prevent default form submission try { console.log("Attempting login with:", username); await login(username, password); console.log("Login successful, navigating to:", from); // Only navigate if login was successful navigate(from, { replace: true }); } catch (error) { console.error("Login failed:", error); // Stay on login page to show error message // The error will be displayed via the loginError state from useAuth hook } }; return (
Power On
{(loginError || msalError) && (
{loginError || msalError}
)}
setUsername(e.target.value)} onFocus={() => setUsernameFocused(true)} onBlur={() => setUsernameFocused(false)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleCredentialLogin(); } }} className={`${styles.input} ${usernameFocused || username ? styles.focused : ''}`} />
setPassword(e.target.value)} onFocus={() => setPasswordFocused(true)} onBlur={() => setPasswordFocused(false)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleCredentialLogin(); } }} className={`${styles.input} ${passwordFocused || password ? styles.focused : ''}`} />

Mit der Anmeldung stimmen Sie unseren Datenschutzbestimmungen zur KI-Nutzung zu.

oder
Du hast noch keinen Konto?
); } export default Login;