import React, { useEffect, useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { useForm } from 'react-hook-form'; import { useAuthStore } from '../store/authStore'; import { LogIn, Mail, Lock, Building } from 'lucide-react'; import { apiClient } from '../services/api/config'; import { useLanguage } from '../context/LanguageContext'; interface LoginFormData { tenantName?: string; userNameOrEmailAddress: string; password: string; rememberMe?: boolean; } interface TenantInfo { success: boolean; tenantId?: string; name?: string; isActive?: boolean; } const LoginWithTenant: React.FC = () => { const navigate = useNavigate(); const { login, isLoading } = useAuthStore(); const { t } = useLanguage(); const [isMultiTenancyEnabled, setIsMultiTenancyEnabled] = useState(false); const [tenantInfo, setTenantInfo] = useState(null); const { register, handleSubmit, formState: { errors }, watch, } = useForm({ defaultValues: { rememberMe: true } }); const tenantName = watch('tenantName'); useEffect(() => { checkMultiTenancy(); }, []); useEffect(() => { if (tenantName) { checkTenant(tenantName); } else { setTenantInfo(null); localStorage.removeItem('tenant_id'); } }, [tenantName]); const checkMultiTenancy = async () => { try { const response = await apiClient.get('/api/abp/application-configuration'); setIsMultiTenancyEnabled(response.data.multiTenancy?.isEnabled || false); } catch (error) { console.error('Failed to check multi-tenancy:', error); } }; const checkTenant = async (name: string) => { try { const response = await apiClient.post('/api/abp/multi-tenancy/tenants/by-name/' + name); if (response.data.success && response.data.tenantId) { setTenantInfo(response.data); localStorage.setItem('tenant_id', response.data.tenantId); } else { setTenantInfo({ success: false }); localStorage.removeItem('tenant_id'); } } catch (error) { setTenantInfo({ success: false }); localStorage.removeItem('tenant_id'); } }; const onSubmit = async (data: LoginFormData) => { try { await login({ username: data.userNameOrEmailAddress, password: data.password, }); navigate('/'); } catch (error) { // Error is handled in the store } }; return (

{t('login.welcome')}

{t('login.subtitle')}{' '} {t('login.createAccount')}

{/* Tenant Field - Only show if multi-tenancy is enabled */} {isMultiTenancyEnabled && (
{tenantInfo && !tenantInfo.success && tenantName && (

{t('login.organizationNotFound')}

)} {tenantInfo && tenantInfo.success && (

✓ {tenantInfo.name} {t('login.organizationFound')}

)}
)} {/* Username/Email Field */}
{errors.userNameOrEmailAddress && (

{errors.userNameOrEmailAddress.message}

)}
{/* Password Field */}
{errors.password && (

{errors.password.message}

)}
); }; export default LoginWithTenant;