import { createClient } from '@supabase/supabase-js'; // Öffentliche Browser-Werte (zur Build-Zeit von Vite eingesetzt). Der anon-Key // ist per Design öffentlich; die echte Autorität liegt server-seitig. const url = import.meta.env.VITE_SUPABASE_URL; const anonKey = import.meta.env.VITE_SUPABASE_ANON_KEY; // Mit Supabase-URL: echter Client (openbureau, unverändert). Ohne (DB-loser core, // auth: 'local', z. B. kgva): ein Shim mit DERSELBEN auth-Schnittstelle, die App/ // api nutzen — getSession / onAuthStateChange / signInWithPassword / signOut — // gegen /api/auth/login, Token in localStorage. So bleibt der Supabase-Pfad // völlig unangetastet, und der lokale Pfad braucht keine Änderung an App/api. function localAuthClient() { const KEY = 'cms_local_session'; const read = () => { try { return JSON.parse(localStorage.getItem(KEY) || 'null'); } catch { return null; } }; let listeners = []; const emit = (s) => listeners.forEach((cb) => { try { cb(s ? 'SIGNED_IN' : 'SIGNED_OUT', s); } catch { /* ignore */ } }); return { auth: { async getSession() { return { data: { session: read() } }; }, onAuthStateChange(cb) { listeners.push(cb); return { data: { subscription: { unsubscribe() { listeners = listeners.filter((l) => l !== cb); } } } }; }, async signInWithPassword({ email, password }) { try { const r = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); const j = await r.json().catch(() => ({})); if (!r.ok) return { error: { message: j.error || `HTTP ${r.status}` } }; const session = { access_token: j.access_token, user: { id: j.user.id, email: j.user.email } }; localStorage.setItem(KEY, JSON.stringify(session)); emit(session); return { data: { session }, error: null }; } catch (e) { return { error: { message: String(e.message || e) } }; } }, async signOut() { localStorage.removeItem(KEY); emit(null); return { error: null }; }, }, }; } export const supabase = url ? createClient(url, anonKey) : localAuthClient();