import { NextResponse } from 'next/server' import { createClient } from '@supabase/supabase-js' export const dynamic = 'force-dynamic' // Retourne les credentials WP pour le client authentifié via Bearer token export async function GET(request: Request) { try { const token = request.headers.get('Authorization')?.replace('Bearer ', '') if (!token) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 }) const supabase = createClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY! ) const { data: { user } } = await supabase.auth.getUser(token) if (!user) return NextResponse.json({ error: 'Token invalide' }, { status: 401 }) const { data: profile } = await supabase .from('profiles') .select('client_id') .eq('id', user.id) .single() if (!profile?.client_id) return NextResponse.json({ error: 'Profil introuvable' }, { status: 404 }) const { data: conn } = await supabase .from('wordpress_connections') .select('wp_url, wp_username, wp_app_password') .eq('client_id', profile.client_id) .single() if (!conn) return NextResponse.json({ error: 'Aucune connexion WordPress configurée' }, { status: 404 }) return NextResponse.json({ wp_url: conn.wp_url, wp_username: conn.wp_username, wp_app_password: conn.wp_app_password }) } catch (err) { return NextResponse.json({ error: (err as Error).message }, { status: 500 }) } }