import React, { useState, useEffect } from 'react'; import { createAccount, checkCredentials, } from '../backend/database'; import Settings from './settings/Settings'; // Import the Settings component const Login: React.FC = () => { // State to handle popup visibility const [showLoginPopup, setShowLoginPopup] = useState(false); const [showSignUpPopup, setShowSignUpPopup] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const [showSettingsPopup, setShowSettingsPopup] = useState(false); // Credentials state const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [accountName, setAccountName] = useState(''); const [newAccountEmail, setNewAccountEmail] = useState(''); const [newAccountPassword, setNewAccountPassword] = useState(''); const [newAccountName, setNewAccountName] = useState(''); // On component mount, check if there are credentials in localStorage useEffect(() => { const savedAccountName = localStorage.getItem('accountName'); const savedAccountEmail = localStorage.getItem('accountEmail'); const savedAccountPassword = localStorage.getItem('accountPassword'); // If credentials are found in localStorage, log the user in if (savedAccountName && savedAccountEmail && savedAccountPassword) { setAccountName(savedAccountName); setEmail(savedAccountEmail); setPassword(savedAccountPassword); const check = async () => { const success = await checkCredentials(savedAccountName, savedAccountPassword); setIsLoggedIn(success); // Automatically log in }; check(); } }, []); // Function to toggle the login popup const toggleLoginPopup = () => setShowLoginPopup(!showLoginPopup); // Function to toggle the sign-up popup const toggleSignUpPopup = () => { setShowSignUpPopup(!showSignUpPopup); setShowLoginPopup(false); }; // Function to handle login const handleLogin = async () => { const savedAccountEmail = localStorage.getItem('accountEmail'); const savedAccountPassword = localStorage.getItem('accountPassword'); const savedAccountName = localStorage.getItem('accountName'); // Check if savedAccountName or savedAccountEmail is not null before passing to checkCredentials var accountIdentifier = savedAccountName || savedAccountEmail; if (!accountIdentifier) { accountIdentifier = accountName } if (accountIdentifier && password) { const success = await checkCredentials(accountIdentifier, password); if (success) { setIsLoggedIn(true); // Successful login setShowLoginPopup(false); // Close the login popup // Save credentials to localStorage (optional in case of changes) localStorage.setItem('accountName', savedAccountName || accountName); localStorage.setItem('accountEmail', savedAccountEmail || email); localStorage.setItem('accountPassword', savedAccountPassword || password); } else { alert('Incorrect credentials'); } } else { alert('Incorrect credentials'); } }; // Function to handle account creation const handleCreateAccount = async () => { const success = await createAccount(newAccountName, newAccountEmail, newAccountPassword); if (success) { alert('Account created successfully! You can now log in.'); toggleSignUpPopup(); // Close sign-up popup } else { alert('Account creation failed. Please try again.'); } }; // Function to toggle the settings popup const toggleSettingsPopup = () => setShowSettingsPopup(!showSettingsPopup); return (
{/* Login or Settings Button */} {/* Conditional rendering of the Login Popup */} {showLoginPopup && (

Log In

{/* Close Button */} {/* Name or Email Input */}
{ const input = e.target.value; setEmail(input); // Update both email and accountName states setAccountName(input); }} />
{/* Password Input */}
setPassword(e.target.value)} />
{/* Log In Button */}
{/* Text for creating an account */}

Don't have an account yet? Create one{' '} here

)} {/* Conditional rendering of the Sign-Up Popup */} {showSignUpPopup && (

Create Account

{/* New Account Email Input */}
setNewAccountEmail(e.target.value)} />
{/* New Account Name Input */}
setNewAccountName(e.target.value)} />
{/* New Account Password Input */}
setNewAccountPassword(e.target.value)} />
{/* Create Account Button */}
{/* Close Button */}
)} {/* Conditional rendering of the Settings Popup */} {showSettingsPopup && }
); }; export default Login;