import { createClient } from '@supabase/supabase-js'; import { readFileSync, existsSync } from 'fs'; import * as path from 'path'; import * as dotenv from 'dotenv'; import fetch from 'node-fetch'; // Load environment variables from .env.local const envLocalPath = path.resolve(process.cwd(), '.env.local'); if (existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); // Try loading from default .env } // Get Supabase credentials from environment variables const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || ''; if (!supabaseUrl || !supabaseServiceKey) { console.error('Missing Supabase environment variables'); process.exit(1); } // Create a service role client to bypass RLS policies const supabase = createClient(supabaseUrl, supabaseServiceKey, { auth: { persistSession: false, }, }); async function main() { // Step 1: Check if the API is running try { console.log('Step 1: Testing connection to API...'); const apiUrl = 'http://localhost:3000/api/blog'; const response = await fetch(apiUrl); if (!response.ok) { console.error(`API returned error status: ${response.status}`); process.exit(1); } const data = await response.json(); console.log(`API response status: ${response.status}`); console.log(`API returned ${Array.isArray(data) ? data.length : 'non-array'} blog posts`); if (Array.isArray(data) && data.length === 0) { console.log('API returned an empty array'); } // Step 2: Check auth bypass status console.log('\nStep 2: Checking auth bypass status...'); const bypassAuth = process.env.BYPASS_AUTH_IN_DEV === 'true'; console.log(`Auth bypass is ${bypassAuth ? 'ENABLED' : 'DISABLED'}`); console.log(`This means the API should be running in ${bypassAuth ? 'admin mode' : 'regular user mode'}`); // Step 3: Check published posts directly console.log('\nStep 3: Checking published posts directly from database...'); const { data: publishedPosts, error: publishedError } = await supabase .from('blog_posts') .select('id, title, slug, published') .eq('published', true); if (publishedError) { console.error('Error fetching published posts:', publishedError); } else { console.log(`Found ${publishedPosts.length} published posts in database:`); publishedPosts.forEach((post, i) => { console.log(`${i+1}. ${post.title} (${post.slug})`); }); } // Step 4: Check specific API route console.log('\nStep 4: Analyzing API route behavior...'); console.log('The GET function in src/app/api/blog/route.ts should:'); console.log('- Check if the request is from an admin via validateRequest()'); console.log('- If user is admin: return ALL posts'); console.log('- If user is NOT admin: return only published posts'); console.log('- If BYPASS_AUTH_IN_DEV=true: it should assume admin rights'); // Step 5: Try a direct fetch with admin headers console.log('\nStep 5: Testing API with explicit admin headers...'); try { const adminResponse = await fetch(apiUrl, { headers: { 'x-admin-override': 'true', 'x-debug-mode': 'true' } }); const adminData = await adminResponse.json(); console.log(`API with admin headers returned ${Array.isArray(adminData) ? adminData.length : 'non-array'} blog posts`); } catch (error) { console.log('API does not support custom admin headers (this is expected)'); } // Step 6: Check for server logs console.log('\nStep 6: Looking for server logs...'); console.log('Please check your Next.js server logs for any of these messages:'); console.log('- "Fetching blog posts..."'); console.log('- "Admin request: showing all posts including unpublished"'); console.log('- "Non-admin request: filtering to only show published posts"'); console.log('- "Successfully fetched X blog posts (admin view)" or "(public view)"'); // Step 7: Recommendations console.log('\nRecommendations:'); console.log('1. Ensure your dev server was restarted after making changes'); console.log('2. Verify BYPASS_AUTH_IN_DEV=true in .env.local'); console.log('3. Add more console logs in src/app/api/blog/route.ts to debug'); console.log('4. Check browser console/network tab for any errors'); console.log('5. Confirm there are no issues with the Supabase connection'); } catch (error) { console.error('Error with API connection:', error); } } main();