/** * Login Page Component * Professional login form connected to the API with visual improvements */ import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useLogin } from '../hooks/useAuth'; import { useNavigate } from 'react-router-dom'; import SmoothScheduleLogo from '../components/SmoothScheduleLogo'; import OAuthButtons from '../components/OAuthButtons'; import LanguageSelector from '../components/LanguageSelector'; import { DevQuickLogin } from '../components/DevQuickLogin'; import { AlertCircle, Loader2, User, Lock, ArrowRight } from 'lucide-react'; const LoginPage: React.FC = () => { const { t } = useTranslation(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const navigate = useNavigate(); const loginMutation = useLogin(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); loginMutation.mutate( { username, password }, { onSuccess: (data) => { const user = data.user; const currentHostname = window.location.hostname; const currentPort = window.location.port; // Check if we're on the root domain (no subdomain) const isRootDomain = currentHostname === 'lvh.me' || currentHostname === 'localhost'; // Roles allowed to login at the root domain const rootAllowedRoles = ['superuser', 'platform_manager', 'platform_support', 'owner']; // If on root domain, only allow specific roles if (isRootDomain && !rootAllowedRoles.includes(user.role)) { setError(t('auth.loginAtSubdomain')); return; } // Determine the correct subdomain based on user role let targetSubdomain: string | null = null; // Platform users (superuser, platform_manager, platform_support) if (['superuser', 'platform_manager', 'platform_support'].includes(user.role)) { targetSubdomain = 'platform'; } // Business users - redirect to their business subdomain else if (user.business_subdomain) { targetSubdomain = user.business_subdomain; } // Check if we need to redirect to a different subdomain // Need to redirect if we have a target subdomain AND we're not already on it const isOnTargetSubdomain = currentHostname === `${targetSubdomain}.lvh.me`; const needsRedirect = targetSubdomain && !isOnTargetSubdomain; if (needsRedirect) { // Pass tokens in URL to ensure they're available immediately on the new subdomain // This avoids race conditions where cookies might not be set before the page loads const portStr = currentPort ? `:${currentPort}` : ''; window.location.href = `http://${targetSubdomain}.lvh.me${portStr}/?access_token=${data.access}&refresh_token=${data.refresh}`; return; } // Already on correct subdomain - navigate to dashboard navigate('/'); }, onError: (err: any) => { setError(err.response?.data?.error || t('auth.invalidCredentials')); }, } ); }; return (
{/* Left Side - Image & Branding (Hidden on mobile) */}
Smooth Schedule

{t('marketing.tagline')}

{t('marketing.description')}

© {new Date().getFullYear()} {t('marketing.copyright')}
{/* Right Side - Login Form */}

{t('auth.welcomeBack')}

{t('auth.pleaseEnterDetails')}

{error && (

{t('auth.authError')}

{error}
)}
{/* Username */}
setUsername(e.target.value)} />
{/* Password */}
setPassword(e.target.value)} />
{/* OAuth Divider and Buttons */}
{t('auth.orContinueWith')}
{/* Language Selector */}
{/* Dev Quick Login */}
); }; export default LoginPage;