'use client';

import { useEffect } from 'react';
import { createClient } from '@/lib/supabase/client';

export default function AuthListener() {
    const supabase = createClient();

    useEffect(() => {
        // Listener für Auth-Status Änderungen
        const {
            data: { subscription },
        } = supabase.auth.onAuthStateChange((event) => {
            if (event === 'SIGNED_OUT') {
                console.log(`[Auth] Event ${event} detected, forcing reload...`);
                // Zwinge den Browser zum kompletten Neuladen, um Caches zu leeren
                // Wir nutzen window.location.href statt router.push für einen harten Reload
                window.location.href = '/';
            }
        });

        return () => {
            subscription.unsubscribe();
        };
    }, [supabase]);

    return null;
}
