import { useRef, useState, FormEvent, useEffect } from 'react' import SingleColumnLayout from '../../../layouts/SingleColumnLayout.tsx' import TextInput from '../../../components/inputs/TextInput.tsx' import Button from '../../../components/buttons/Button.tsx' import { AuthService } from '../authService.ts' import { useNavigate } from 'react-router-dom' import { useUser } from '../../user/user.ts' import NavBar from '../../../components/NavBar.tsx' import NavButton from '../../../components/buttons/NavButton.tsx' import LinkButton from '../../../components/buttons/LinkButton.tsx' import { useTranslations } from '../../i18n/translations.ts' interface LoginPageProps { authService: AuthService } export default function LoginPage({ authService }: LoginPageProps) { const { t } = useTranslations() const [isSubmitting, setIsSubmitting] = useState(false) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [rememberMe, setRememberMe] = useState(false) const [error, setError] = useState(null) const usernameInputRef = useRef(null) const passwordInputRef = useRef(null) const navigate = useNavigate() const user = useUser() useEffect(() => { if (user) { const search = new URLSearchParams(window.location.search) navigate(search.get('t') || '/') } }, [user, navigate]) const onSubmit = async (e: FormEvent) => { e.preventDefault() if (!username) { setError("it's not username :(") return } if (!password) { setError("it's not password :(") return } setError(null) setIsSubmitting(true) try { await authService.login(username, password, rememberMe) } catch (error: unknown) { setError(error instanceof Error ? error.message : 'something went terribly wrong') } finally { setIsSubmitting(false) } } return ( {t('nav.home')} } >
setRememberMe(e.target.checked)} className="h-4 w-4" />
{t('auth.login.register_instead')} {error}
) }