"use client"; import React, { useState } from 'react'; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Mail, CheckCircle, AlertCircle, ArrowLeft } from "lucide-react"; import { supabase } from '@/lib/supabase'; import { colors } from '@/lib/colors'; interface EmailVerificationProps { email: string; onVerificationComplete: () => void; onBack: () => void; } export default function EmailVerification({ email, onVerificationComplete, onBack }: EmailVerificationProps) { const [verificationEmail, setVerificationEmail] = useState(email); const [loading, setLoading] = useState(false); const [sent, setSent] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [rateLimited, setRateLimited] = useState(false); const [cooldownTime, setCooldownTime] = useState(0); const handleSendVerification = async () => { if (!verificationEmail || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(verificationEmail)) { setError('Bitte geben Sie eine gültige E-Mail-Adresse ein.'); return; } if (rateLimited) { setError(`Rate Limit aktiv. Bitte warten Sie ${cooldownTime} Sekunden oder verwenden Sie die manuelle Bestätigung.`); return; } setLoading(true); setError(null); try { console.log('=== E-MAIL VERIFICATION DEBUG ==='); console.log('Sending verification email to:', verificationEmail); console.log('Current origin:', window.location.origin); console.log('Redirect URL:', `${window.location.origin}/auth/callback`); // Use signInWithOtp with proper configuration for email verification const { data, error } = await supabase.auth.signInWithOtp({ email: verificationEmail, options: { shouldCreateUser: true, data: { // Custom metadata for appointment booking appointment_booking: true, email: verificationEmail } } }); console.log('Supabase email verification response:', { data, error }); if (error) { console.error('Supabase email verification error:', error); if (error.message.includes('rate limit')) { setRateLimited(true); setCooldownTime(60); // 60 seconds cooldown // Start countdown const countdown = setInterval(() => { setCooldownTime(prev => { if (prev <= 1) { clearInterval(countdown); setRateLimited(false); return 0; } return prev - 1; }); }, 1000); setError(`E-Mail-Rate-Limit erreicht. Bitte warten Sie 60 Sekunden oder verwenden Sie die manuelle Bestätigung.`); } else if (error.message.includes('expired') || error.message.includes('invalid')) { setError('Der E-Mail-Link ist abgelaufen oder ungültig. Bitte fordern Sie einen neuen Link an.'); } else { setError(`E-Mail-Fehler: ${error.message}`); } } else { console.log('Verification email sent successfully'); setSent(true); } } catch (err) { console.error('Unexpected error sending verification email:', err); setError('Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.'); } finally { setLoading(false); } }; const handleEmailChange = (e: React.ChangeEvent) => { setVerificationEmail(e.target.value); if (error) setError(null); }; const handleResend = () => { setSent(false); setError(null); }; return (

E-Mail senden

{!sent ? (

Um Ihren Termin zu bestätigen, senden wir Ihnen eine Bestätigungs-E-Mail. Klicken Sie auf den Link in der E-Mail, um sich zu authentifizieren.

{error && (

{error}

)} {rateLimited && (

⚠️ Rate Limit aktiv: Bitte warten Sie {cooldownTime} Sekunden oder verwenden Sie die manuelle Bestätigung.

)}
) : (

E-Mail gesendet!

Wir haben eine Bestätigungs-E-Mail an {verificationEmail} gesendet. Bitte überprüfen Sie Ihren Posteingang und klicken Sie auf den Link in der E-Mail.

)}
); }