'use client'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle, CardDescription, } from '@/components/ui/card'; import { LanguageSelector } from '@/components/ui/language-selector'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import * as z from 'zod'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { useEffect, useState } from 'react'; import { httpClient } from '@/app/infra/http/HttpClient'; import { useRouter } from 'next/navigation'; import { Mail, Lock, Loader2 } from 'lucide-react'; import langbotIcon from '@/app/assets/langbot-logo.webp'; import { toast } from 'sonner'; import { useTranslation } from 'react-i18next'; import Link from 'next/link'; import { ThemeToggle } from '@/components/ui/theme-toggle'; const formSchema = (t: (key: string) => string) => z.object({ email: z.string().email(t('common.invalidEmail')), password: z.string().min(1, t('common.emptyPassword')), }); export default function Login() { const router = useRouter(); const { t } = useTranslation(); const [spaceLoading, setSpaceLoading] = useState(false); const form = useForm>>({ resolver: zodResolver(formSchema(t)), defaultValues: { email: '', password: '', }, }); useEffect(() => { getIsInitialized(); checkIfAlreadyLoggedIn(); }, []); function getIsInitialized() { httpClient .checkIfInited() .then((res) => { if (!res.initialized) { router.push('/register'); } }) .catch(() => {}); } function checkIfAlreadyLoggedIn() { httpClient .checkUserToken() .then((res) => { if (res.token) { localStorage.setItem('token', res.token); router.push('/home'); } }) .catch(() => {}); } function onSubmit(values: z.infer>) { handleLogin(values.email, values.password); } function handleLogin(username: string, password: string) { httpClient .authUser(username, password) .then((res) => { localStorage.setItem('token', res.token); localStorage.setItem('userEmail', username); router.push('/home'); toast.success(t('common.loginSuccess')); }) .catch(() => { toast.error(t('common.loginFailed')); }); } // Space OAuth redirect handler const handleSpaceLoginClick = async () => { setSpaceLoading(true); try { // Build the redirect URI to the OAuth callback page const currentOrigin = window.location.origin; const redirectUri = `${currentOrigin}/auth/space/callback`; // Get the authorization URL from backend const response = await httpClient.getSpaceAuthorizeUrl(redirectUri); // Redirect to Space authorization page window.location.href = response.authorize_url; } catch { toast.error(t('common.spaceLoginFailed')); setSpaceLoading(false); } }; return (
LangBot {t('common.welcome')} {t('common.continueToLogin')}
{/* Space Login - Recommended */}

{t('common.spaceLoginRecommended')}

{t('common.or')}
{/* Local Account Login */}
( {t('common.email')}
)} /> (
{t('common.password')} {t('common.forgotPassword')}
)} />
); }