'use client' import { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' import { useAuth } from '@/lib/hooks/use-auth' import { useAuthStore } from '@/lib/stores/auth-store' import { getConfig } from '@/lib/config' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { AlertCircle } from 'lucide-react' import { LoadingSpinner } from '@/components/common/LoadingSpinner' import { useTranslation } from '@/lib/hooks/use-translation' export function LoginForm() { const { t, language } = useTranslation() const [password, setPassword] = useState('') const { login, isLoading, error } = useAuth() const { authRequired, checkAuthRequired, hasHydrated, isAuthenticated } = useAuthStore() const [isCheckingAuth, setIsCheckingAuth] = useState(true) const [configInfo, setConfigInfo] = useState<{ apiUrl: string; version: string; buildTime: string } | null>(null) const router = useRouter() // Load config info for debugging useEffect(() => { getConfig().then(cfg => { setConfigInfo({ apiUrl: cfg.apiUrl, version: cfg.version, buildTime: cfg.buildTime, }) }).catch(err => { console.error('Failed to load config:', err) }) }, []) // Check if authentication is required on mount useEffect(() => { if (!hasHydrated) { return } const checkAuth = async () => { try { const required = await checkAuthRequired() // If auth is not required, redirect to notebooks if (!required) { router.push('/notebooks') } } catch (error) { console.error('Error checking auth requirement:', error) // On error, assume auth is required to be safe } finally { setIsCheckingAuth(false) } } // If we already know auth status, use it if (authRequired !== null) { if (!authRequired && isAuthenticated) { router.push('/notebooks') } else { setIsCheckingAuth(false) } } else { void checkAuth() } }, [hasHydrated, authRequired, checkAuthRequired, router, isAuthenticated]) // Show loading while checking if auth is required if (!hasHydrated || isCheckingAuth) { return (
) } // If we still don't know if auth is required (connection error), show error if (authRequired === null) { return (
{t('common.connectionError')} {t('common.unableToConnect')}
{error || t('auth.connectErrorHint')}
{configInfo && (
{t('common.diagnosticInfo')}:
{t('common.version')}: {configInfo.version}
{t('common.built')}: {new Date(configInfo.buildTime).toLocaleString(language === 'zh-CN' ? 'zh-CN' : language === 'zh-TW' ? 'zh-TW' : 'en-US')}
{t('common.apiUrl')}: {configInfo.apiUrl}
{t('common.frontendUrl')}: {typeof window !== 'undefined' ? window.location.href : 'N/A'}
{t('common.checkConsoleLogs')}
)}
) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (password.trim()) { try { await login(password) } catch (error) { console.error('Unhandled error during login:', error) // The auth store should handle most errors, but this catches any unhandled ones } } } return (
{t('auth.loginTitle')} {t('auth.loginDesc')}
setPassword(e.target.value)} disabled={isLoading} />
{error && (
{error}
)} {configInfo && (
{t('common.version')} {configInfo.version}
{configInfo.apiUrl}
)}
) }