'use client' import { useEffect, useState } from 'react' import { createAuthClient } from 'better-auth/react' const authClient = createAuthClient({ // Keep auth requests on the current origin (important for tenant subdomains). baseURL: typeof window !== 'undefined' ? window.location.origin : (process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3010'), }) interface LoginFormProps { primaryColor?: string } export function LoginForm({ primaryColor = '#C99738' }: LoginFormProps) { const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [successMessage, setSuccessMessage] = useState('') useEffect(() => { const params = new URLSearchParams(window.location.search) const emailParam = params.get('email') if (emailParam) setEmail(emailParam) const messageParam = params.get('message') if (messageParam === 'password_changed') { setSuccessMessage('Passwort erfolgreich geƤndert. Bitte melden Sie sich mit Ihrem neuen Passwort an.') } }, []) async function handleSubmit(e: React.FormEvent) { e.preventDefault() setLoading(true) setError('') const result = await authClient.signIn.email({ email, password, callbackURL: '/dashboard', }) setLoading(false) if (result.error) { setError(result.error.message ?? 'E-Mail oder Passwort falsch.') return } // Use callbackUrl if present, otherwise go to dashboard // mustChangePassword is handled by the dashboard ForcePasswordChange component const params = new URLSearchParams(window.location.search) const callbackUrl = params.get('callbackUrl') let target = '/dashboard' if (callbackUrl?.startsWith('/')) { target = callbackUrl // Normalize stale tenant-prefixed callback URLs like /test/dashboard // when already on the tenant subdomain test.localhost. const hostname = window.location.hostname const parts = hostname.split('.') const isTenantSubdomain = parts.length > 2 || (parts.length === 2 && parts[1] === 'localhost') const tenantSlug = isTenantSubdomain ? parts[0] : null if (tenantSlug && target.startsWith(`/${tenantSlug}/`)) { target = target.slice(tenantSlug.length + 1) || '/dashboard' } } window.location.href = target } return (
{successMessage && (

{successMessage}

)}
setEmail(e.target.value)} placeholder="admin@ihre-innung.de" className="w-full px-3 py-2.5 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:border-transparent" style={{ '--tw-ring-color': primaryColor } as any} />
setPassword(e.target.value)} placeholder="********" className="w-full px-3 py-2.5 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:border-transparent" style={{ '--tw-ring-color': primaryColor } as any} />
{error && (

{error}

)}
) }