import React, { useState, useEffect, useMemo } from 'react' import { useNavigate } from 'react-router-dom' import { useAuthStore } from '../store/authStore' import { useSettingsStore } from '../store/settingsStore' import { SUPPORTED_LANGUAGES, useTranslation } from '../i18n' import { authApi } from '../api/client' import { getApiErrorMessage } from '../types' import { Plane, Eye, EyeOff, Mail, Lock, MapPin, Calendar, Package, User, Globe, Zap, Users, Wallet, Map, CheckSquare, BookMarked, FolderOpen, Route, Shield, KeyRound } from 'lucide-react' interface AppConfig { has_users: boolean allow_registration: boolean setup_complete: boolean demo_mode: boolean oidc_configured: boolean oidc_display_name?: string oidc_only_mode: boolean } export default function LoginPage(): React.ReactElement { const { t, language } = useTranslation() const [mode, setMode] = useState<'login' | 'register'>('login') const [username, setUsername] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [showPassword, setShowPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') const [appConfig, setAppConfig] = useState(null) const [inviteToken, setInviteToken] = useState('') const [inviteValid, setInviteValid] = useState(false) const { login, register, demoLogin, completeMfaLogin, loadUser } = useAuthStore() const { setLanguageLocal } = useSettingsStore() const navigate = useNavigate() const redirectTarget = useMemo(() => { const params = new URLSearchParams(window.location.search) const redirect = params.get('redirect') // Only allow relative paths starting with / to prevent open redirect attacks if (redirect && redirect.startsWith('/') && !redirect.startsWith('//') && !redirect.startsWith('/\\')) { return redirect } return '/dashboard' }, []) useEffect(() => { const params = new URLSearchParams(window.location.search) const invite = params.get('invite') const oidcCode = params.get('oidc_code') const oidcError = params.get('oidc_error') if (invite) { setInviteToken(invite) setMode('register') authApi.validateInvite(invite).then(() => { setInviteValid(true) }).catch(() => { setError('Invalid or expired invite link') }) window.history.replaceState({}, '', window.location.pathname) } if (oidcCode) { setIsLoading(true) window.history.replaceState({}, '', '/login') fetch('/api/auth/oidc/exchange?code=' + encodeURIComponent(oidcCode), { credentials: 'include' }) .then(r => r.json()) .then(async data => { if (data.token) { await loadUser() navigate('/dashboard', { replace: true }) } else { setError(data.error || 'OIDC login failed') } }) .catch(() => setError('OIDC login failed')) .finally(() => setIsLoading(false)) return } if (oidcError) { const errorMessages: Record = { registration_disabled: t('login.oidc.registrationDisabled'), no_email: t('login.oidc.noEmail'), token_failed: t('login.oidc.tokenFailed'), invalid_state: t('login.oidc.invalidState'), } setError(errorMessages[oidcError] || oidcError) window.history.replaceState({}, '', '/login') return } authApi.getAppConfig?.().catch(() => null).then((config: AppConfig | null) => { if (config) { setAppConfig(config) if (!config.has_users) setMode('register') if (config.oidc_only_mode && config.oidc_configured && config.has_users && !invite) { window.location.href = '/api/auth/oidc/login' } } }) }, [navigate, t]) const handleDemoLogin = async (): Promise => { setError('') setIsLoading(true) try { await demoLogin() setShowTakeoff(true) setTimeout(() => navigate(redirectTarget), 2600) } catch (err: unknown) { setError(err instanceof Error ? err.message : t('login.demoFailed')) } finally { setIsLoading(false) } } const [showTakeoff, setShowTakeoff] = useState(false) const [mfaStep, setMfaStep] = useState(false) const [mfaToken, setMfaToken] = useState('') const [mfaCode, setMfaCode] = useState('') const [passwordChangeStep, setPasswordChangeStep] = useState(false) const [savedLoginPassword, setSavedLoginPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const handleSubmit = async (e: React.FormEvent): Promise => { e.preventDefault() setError('') setIsLoading(true) try { if (passwordChangeStep) { if (!newPassword) { setError(t('settings.passwordRequired')); setIsLoading(false); return } if (newPassword.length < 8) { setError(t('settings.passwordTooShort')); setIsLoading(false); return } if (newPassword !== confirmPassword) { setError(t('settings.passwordMismatch')); setIsLoading(false); return } await authApi.changePassword({ current_password: savedLoginPassword, new_password: newPassword }) await loadUser({ silent: true }) setShowTakeoff(true) setTimeout(() => navigate(redirectTarget), 2600) return } if (mode === 'login' && mfaStep) { if (!mfaCode.trim()) { setError(t('login.mfaCodeRequired')) setIsLoading(false) return } const mfaResult = await completeMfaLogin(mfaToken, mfaCode) if ('user' in mfaResult && mfaResult.user?.must_change_password) { setSavedLoginPassword(password) setPasswordChangeStep(true) setIsLoading(false) return } setShowTakeoff(true) setTimeout(() => navigate(redirectTarget), 2600) return } if (mode === 'register') { if (!username.trim()) { setError('Username is required'); setIsLoading(false); return } if (password.length < 8) { setError('Password must be at least 8 characters'); setIsLoading(false); return } await register(username, email, password, inviteToken || undefined) } else { const result = await login(email, password) if ('mfa_required' in result && result.mfa_required && 'mfa_token' in result) { setMfaToken(result.mfa_token) setMfaStep(true) setMfaCode('') setIsLoading(false) return } if ('user' in result && result.user?.must_change_password) { setSavedLoginPassword(password) setPasswordChangeStep(true) setIsLoading(false) return } } setShowTakeoff(true) setTimeout(() => navigate(redirectTarget), 2600) } catch (err: unknown) { setError(getApiErrorMessage(err, t('login.error'))) setIsLoading(false) } } const showRegisterOption = (appConfig?.allow_registration || !appConfig?.has_users || inviteValid) && !appConfig?.oidc_only_mode && (appConfig?.setup_complete !== false || !appConfig?.has_users) // In OIDC-only mode, show a minimal page that redirects directly to the IdP const oidcOnly = appConfig?.oidc_only_mode && appConfig?.oidc_configured const inputBase: React.CSSProperties = { width: '100%', padding: '11px 12px 11px 40px', border: '1px solid #e5e7eb', borderRadius: 12, fontSize: 14, fontFamily: 'inherit', outline: 'none', color: '#111827', background: 'white', boxSizing: 'border-box', transition: 'border-color 0.15s', } if (showTakeoff) { return (
{/* Sky gradient */}
{/* Stars */} {Array.from({ length: 60 }, (_, i) => (
0.7 ? 3 : 1.5, height: Math.random() > 0.7 ? 3 : 1.5, borderRadius: '50%', background: 'white', top: `${Math.random() * 100}%`, left: `${Math.random() * 100}%`, animationDelay: `${0.3 + Math.random() * 0.5}s, ${Math.random() * 1}s`, }} /> ))} {/* Clouds rushing past */} {[0, 1, 2, 3, 4].map(i => (
))} {/* Speed lines */} {Array.from({ length: 12 }, (_, i) => (
))} {/* Plane */}
{/* Contrail */}
{/* Logo fade in + burst */}
TREK

{t('login.tagline')}

) } return (
{/* Language toggle */} {/* Left — branding */}
{/* Stars */}
{Array.from({ length: 40 }, (_, i) => (
0.7 ? 2 : 1, height: Math.random() > 0.7 ? 2 : 1, borderRadius: '50%', background: 'white', opacity: 0.15 + Math.random() * 0.25, top: `${Math.random() * 70}%`, left: `${Math.random() * 100}%`, animationDelay: `${Math.random() * 4}s`, }} /> ))}
{/* Animated glow orbs */}
{/* Animated planes — realistic silhouettes at different sizes/speeds */}
{/* Plane 1 — large, slow, foreground */} {/* Plane 2 — small, faster, higher */} {/* Plane 3 — medium, mid-speed */} {/* Plane 4 — tiny, fast, high */} {/* Plane 5 — medium, right to left, lower */} {/* Plane 6 — tiny distant */}
{/* Logo */}
TREK

{t('login.tagline')}

{t('login.description')}

{[ { Icon: Map, label: t('login.features.maps'), desc: t('login.features.mapsDesc') }, { Icon: Zap, label: t('login.features.realtime'), desc: t('login.features.realtimeDesc') }, { Icon: Wallet, label: t('login.features.budget'), desc: t('login.features.budgetDesc') }, { Icon: Users, label: t('login.features.collab'), desc: t('login.features.collabDesc') }, { Icon: CheckSquare, label: t('login.features.packing'), desc: t('login.features.packingDesc') }, { Icon: BookMarked, label: t('login.features.bookings'), desc: t('login.features.bookingsDesc') }, { Icon: FolderOpen, label: t('login.features.files'), desc: t('login.features.filesDesc') }, { Icon: Route, label: t('login.features.routes'), desc: t('login.features.routesDesc') }, ].map(({ Icon, label, desc }) => (
) => { e.currentTarget.style.background = 'rgba(255,255,255,0.08)'; e.currentTarget.style.borderColor = 'rgba(255,255,255,0.12)' }} onMouseLeave={(e: React.MouseEvent) => { e.currentTarget.style.background = 'rgba(255,255,255,0.04)'; e.currentTarget.style.borderColor = 'rgba(255,255,255,0.06)' }}>
{label}
{desc}
))}

{t('login.selfHosted')}

{/* Right — form */}
{/* Mobile logo */}
TREK

{t('login.tagline')}

{oidcOnly ? ( <>

{t('login.title')}

{t('login.oidcOnly')}

{error && (
{error}
)} ) => { e.currentTarget.style.background = '#1f2937' }} onMouseLeave={(e: React.MouseEvent) => { e.currentTarget.style.background = '#111827' }} > {t('login.oidcSignIn', { name: appConfig?.oidc_display_name || 'SSO' })} ) : ( <>

{passwordChangeStep ? t('login.setNewPassword') : mode === 'login' && mfaStep ? t('login.mfaTitle') : mode === 'register' ? (!appConfig?.has_users ? t('login.createAdmin') : t('login.createAccount')) : t('login.title')}

{passwordChangeStep ? t('login.setNewPasswordHint') : mode === 'login' && mfaStep ? t('login.mfaSubtitle') : mode === 'register' ? (!appConfig?.has_users ? t('login.createAdminHint') : t('login.createAccountHint')) : t('login.subtitle')}

{error && (
{error}
)} {passwordChangeStep && ( <>
{t('settings.mustChangePassword')}
) => setNewPassword(e.target.value)} required placeholder={t('settings.newPassword')} style={inputBase} onFocus={(e: React.FocusEvent) => e.target.style.borderColor = '#111827'} onBlur={(e: React.FocusEvent) => e.target.style.borderColor = '#e5e7eb'} />
) => setConfirmPassword(e.target.value)} required placeholder={t('settings.confirmPassword')} style={inputBase} onFocus={(e: React.FocusEvent) => e.target.style.borderColor = '#111827'} onBlur={(e: React.FocusEvent) => e.target.style.borderColor = '#e5e7eb'} />
)} {mode === 'login' && mfaStep && !passwordChangeStep && (
) => setMfaCode(e.target.value.toUpperCase().slice(0, 24))} placeholder="000000 or XXXX-XXXX" required style={inputBase} onFocus={(e: React.FocusEvent) => e.target.style.borderColor = '#111827'} onBlur={(e: React.FocusEvent) => e.target.style.borderColor = '#e5e7eb'} />

{t('login.mfaHint')}

)} {/* Username (register only) */} {mode === 'register' && !passwordChangeStep && (
) => setUsername(e.target.value)} required placeholder="admin" style={inputBase} onFocus={(e: React.FocusEvent) => e.target.style.borderColor = '#111827'} onBlur={(e: React.FocusEvent) => e.target.style.borderColor = '#e5e7eb'} />
)} {/* Email */} {!(mode === 'login' && mfaStep) && !passwordChangeStep && (
) => setEmail(e.target.value)} required placeholder={t('login.emailPlaceholder')} style={inputBase} onFocus={(e: React.FocusEvent) => e.target.style.borderColor = '#111827'} onBlur={(e: React.FocusEvent) => e.target.style.borderColor = '#e5e7eb'} />
)} {/* Password */} {!(mode === 'login' && mfaStep) && !passwordChangeStep && (
) => setPassword(e.target.value)} required placeholder="••••••••" style={{ ...inputBase, paddingRight: 44 }} onFocus={(e: React.FocusEvent) => e.target.style.borderColor = '#111827'} onBlur={(e: React.FocusEvent) => e.target.style.borderColor = '#e5e7eb'} />
)}
{/* Toggle login/register */} {showRegisterOption && appConfig?.has_users && !appConfig?.demo_mode && !passwordChangeStep && (

{mode === 'login' ? t('login.noAccount') + ' ' : t('login.hasAccount') + ' '}

)} )}
{/* OIDC / SSO login button (only when OIDC is configured but not in oidc-only mode) */} {appConfig?.oidc_configured && !oidcOnly && ( <>
{t('common.or')}
) => { e.currentTarget.style.background = '#f9fafb'; e.currentTarget.style.borderColor = '#9ca3af' }} onMouseLeave={(e: React.MouseEvent) => { e.currentTarget.style.background = 'white'; e.currentTarget.style.borderColor = '#d1d5db' }} > {t('login.oidcSignIn', { name: appConfig.oidc_display_name })} )} {/* Demo login button */} {appConfig?.demo_mode && ( )}
) }