import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Text } from '@mantine/core'; import LoginHeader from '@app/routes/login/LoginHeader'; import ErrorMessage from '@app/routes/login/ErrorMessage'; import EmailPasswordForm from '@app/routes/login/EmailPasswordForm'; import DividerWithText from '@app/components/shared/DividerWithText'; import { DesktopOAuthButtons } from '@app/components/SetupWizard/DesktopOAuthButtons'; import { UserInfo } from '@app/services/authService'; import { SSOProviderConfig } from '@app/services/connectionModeService'; import '@app/routes/authShared/auth.css'; interface SelfHostedLoginScreenProps { serverUrl: string; enabledOAuthProviders?: SSOProviderConfig[]; loginMethod?: string; onLogin: (username: string, password: string) => Promise; onOAuthSuccess: (userInfo: UserInfo) => Promise; mfaCode: string; setMfaCode: (value: string) => void; requiresMfa: boolean; loading: boolean; error: string | null; } export const SelfHostedLoginScreen: React.FC = ({ serverUrl, enabledOAuthProviders, loginMethod = 'all', onLogin, onOAuthSuccess, mfaCode, setMfaCode, requiresMfa, loading, error, }) => { const { t } = useTranslation(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [validationError, setValidationError] = useState(null); // Check if username/password authentication is allowed const isUserPassAllowed = loginMethod === 'all' || loginMethod === 'normal'; console.log('[SelfHostedLoginScreen] Props:', { serverUrl, enabledOAuthProviders, loginMethod, isUserPassAllowed, shouldShowOAuth: !!(enabledOAuthProviders && enabledOAuthProviders.length > 0) }); const handleSubmit = async () => { // Validation if (!username.trim()) { setValidationError(t('setup.login.error.emptyUsername', 'Please enter your username')); return; } if (!password) { setValidationError(t('setup.login.error.emptyPassword', 'Please enter your password')); return; } if (requiresMfa && !mfaCode.trim()) { setValidationError(t('login.mfaRequired', 'Two-factor code required')); return; } setValidationError(null); await onLogin(username.trim(), password); }; const handleOAuthError = (errorMessage: string) => { setValidationError(errorMessage); }; const displayError = error || validationError; return ( <> {t('setup.login.connectingTo', 'Connecting to:')} {serverUrl} {/* Show OAuth buttons if providers are available */} {enabledOAuthProviders && enabledOAuthProviders.length > 0 && ( <> {/* Only show divider if username/password auth is also allowed */} {isUserPassAllowed && ( )} )} {/* Only show email/password form if username/password auth is allowed */} {isUserPassAllowed && ( { setUsername(value); setValidationError(null); }} setPassword={(value) => { setPassword(value); setValidationError(null); }} mfaCode={mfaCode} setMfaCode={(value) => { setMfaCode(value); setValidationError(null); }} showMfaField={requiresMfa || Boolean(mfaCode)} requiresMfa={requiresMfa} onSubmit={handleSubmit} isSubmitting={loading} submitButtonText={t('setup.login.submit', 'Login')} /> )} ); };