* Add new logos * fixup! Add new logos * fixup! fixup! Add new logos * Setup login screen * fixup! Setup login screen
151 lines
6.7 KiB
TypeScript
151 lines
6.7 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import i18n from 'i18next';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
const Login: React.FC = () => {
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState<string | null>(null);
|
|
const navigate = useNavigate();
|
|
const { t } = useTranslation();
|
|
const [isDarkMode] = useState<boolean>(() => {
|
|
const storedPreference = localStorage.getItem('isDarkMode');
|
|
return storedPreference !== null
|
|
? storedPreference === 'true'
|
|
: window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
});
|
|
|
|
useEffect(() => {
|
|
document.documentElement.classList.toggle('dark', isDarkMode);
|
|
}, [isDarkMode]);
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
try {
|
|
const response = await fetch('/api/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ email, password }),
|
|
credentials: 'include',
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok) {
|
|
if (data.user && data.user.language) {
|
|
await i18n.changeLanguage(data.user.language);
|
|
}
|
|
|
|
window.dispatchEvent(
|
|
new CustomEvent('userLoggedIn', { detail: data.user })
|
|
);
|
|
|
|
navigate('/today');
|
|
} else {
|
|
setError(data.errors[0] || 'Login failed. Please try again.');
|
|
}
|
|
} catch (err) {
|
|
setError('An error occurred. Please try again.');
|
|
console.error('Error during login:', err);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
{/* Navbar */}
|
|
<nav className="fixed top-0 left-0 right-0 z-50 text-gray-900 dark:text-white">
|
|
<div className="h-16 flex items-center px-4 sm:px-6 lg:px-8">
|
|
<img
|
|
src={
|
|
isDarkMode
|
|
? '/wide-logo-light.png'
|
|
: '/wide-logo-dark.png'
|
|
}
|
|
alt="tududi"
|
|
className="h-9 w-auto"
|
|
/>
|
|
</div>
|
|
</nav>
|
|
|
|
{/* Main Content */}
|
|
<div className="bg-gray-100 dark:bg-gray-900 min-h-screen px-4 pt-16 flex items-center justify-center">
|
|
<div className="w-full max-w-7xl flex flex-col lg:flex-row items-center justify-center gap-12 lg:gap-16">
|
|
{/* Left side - Login Form */}
|
|
<div className="w-full lg:w-auto flex flex-col items-center">
|
|
<div className="bg-white dark:bg-gray-800 p-10 rounded-lg shadow-md w-full max-w-2xl">
|
|
<h2 className="text-center text-2xl font-semibold text-gray-700 dark:text-gray-200 mb-12">
|
|
{t('auth.login', 'Login')}
|
|
</h2>
|
|
{error && (
|
|
<div className="mb-4 text-center text-red-500">
|
|
{error}
|
|
</div>
|
|
)}
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="mb-4">
|
|
<label
|
|
htmlFor="email"
|
|
className="block text-gray-600 dark:text-gray-300 mb-1"
|
|
>
|
|
{t('auth.email', 'Email')}
|
|
</label>
|
|
<input
|
|
type="email"
|
|
id="email"
|
|
name="email"
|
|
value={email}
|
|
onChange={(e) =>
|
|
setEmail(e.target.value)
|
|
}
|
|
className="w-full px-4 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100"
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="mb-4">
|
|
<label
|
|
htmlFor="password"
|
|
className="block text-gray-600 dark:text-gray-300 mb-1"
|
|
>
|
|
{t('auth.password', 'Password')}
|
|
</label>
|
|
<input
|
|
type="password"
|
|
id="password"
|
|
name="password"
|
|
value={password}
|
|
onChange={(e) =>
|
|
setPassword(e.target.value)
|
|
}
|
|
className="w-full px-4 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100"
|
|
required
|
|
/>
|
|
</div>
|
|
<button
|
|
type="submit"
|
|
className="w-full bg-blue-500 text-white py-2 rounded-lg hover:bg-blue-600 transition-colors"
|
|
>
|
|
{t('auth.login', 'Login')}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Right side - Graphic */}
|
|
<div className="hidden lg:flex items-center justify-center">
|
|
<img
|
|
src="/login-gfx.png"
|
|
alt="Login illustration"
|
|
className="max-w-md w-full h-auto"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default Login;
|