AI Resource Pro / website / scripts / debug-blog-api.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import { readFileSync, existsSync } from 'fs'; |
| 3 | import * as path from 'path'; |
| 4 | import * as dotenv from 'dotenv'; |
| 5 | import fetch from 'node-fetch'; |
| 6 | |
| 7 | // Load environment variables from .env.local |
| 8 | const envLocalPath = path.resolve(process.cwd(), '.env.local'); |
| 9 | if (existsSync(envLocalPath)) { |
| 10 | console.log(`Loading environment from: ${envLocalPath}`); |
| 11 | dotenv.config({ path: envLocalPath }); |
| 12 | } else { |
| 13 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 14 | dotenv.config(); // Try loading from default .env |
| 15 | } |
| 16 | |
| 17 | // Get Supabase credentials from environment variables |
| 18 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; |
| 19 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || ''; |
| 20 | |
| 21 | if (!supabaseUrl || !supabaseServiceKey) { |
| 22 | console.error('Missing Supabase environment variables'); |
| 23 | process.exit(1); |
| 24 | } |
| 25 | |
| 26 | // Create a service role client to bypass RLS policies |
| 27 | const supabase = createClient(supabaseUrl, supabaseServiceKey, { |
| 28 | auth: { |
| 29 | persistSession: false, |
| 30 | }, |
| 31 | }); |
| 32 | |
| 33 | async function main() { |
| 34 | // Step 1: Check if the API is running |
| 35 | try { |
| 36 | console.log('Step 1: Testing connection to API...'); |
| 37 | const apiUrl = 'http://localhost:3000/api/blog'; |
| 38 | const response = await fetch(apiUrl); |
| 39 | |
| 40 | if (!response.ok) { |
| 41 | console.error(`API returned error status: ${response.status}`); |
| 42 | process.exit(1); |
| 43 | } |
| 44 | |
| 45 | const data = await response.json(); |
| 46 | console.log(`API response status: ${response.status}`); |
| 47 | console.log(`API returned ${Array.isArray(data) ? data.length : 'non-array'} blog posts`); |
| 48 | if (Array.isArray(data) && data.length === 0) { |
| 49 | console.log('API returned an empty array'); |
| 50 | } |
| 51 | |
| 52 | // Step 2: Check auth bypass status |
| 53 | console.log('\nStep 2: Checking auth bypass status...'); |
| 54 | const bypassAuth = process.env.BYPASS_AUTH_IN_DEV === 'true'; |
| 55 | console.log(`Auth bypass is ${bypassAuth ? 'ENABLED' : 'DISABLED'}`); |
| 56 | console.log(`This means the API should be running in ${bypassAuth ? 'admin mode' : 'regular user mode'}`); |
| 57 | |
| 58 | // Step 3: Check published posts directly |
| 59 | console.log('\nStep 3: Checking published posts directly from database...'); |
| 60 | const { data: publishedPosts, error: publishedError } = await supabase |
| 61 | .from('blog_posts') |
| 62 | .select('id, title, slug, published') |
| 63 | .eq('published', true); |
| 64 | |
| 65 | if (publishedError) { |
| 66 | console.error('Error fetching published posts:', publishedError); |
| 67 | } else { |
| 68 | console.log(`Found ${publishedPosts.length} published posts in database:`); |
| 69 | publishedPosts.forEach((post, i) => { |
| 70 | console.log(`${i+1}. ${post.title} (${post.slug})`); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | // Step 4: Check specific API route |
| 75 | console.log('\nStep 4: Analyzing API route behavior...'); |
| 76 | console.log('The GET function in src/app/api/blog/route.ts should:'); |
| 77 | console.log('- Check if the request is from an admin via validateRequest()'); |
| 78 | console.log('- If user is admin: return ALL posts'); |
| 79 | console.log('- If user is NOT admin: return only published posts'); |
| 80 | console.log('- If BYPASS_AUTH_IN_DEV=true: it should assume admin rights'); |
| 81 | |
| 82 | // Step 5: Try a direct fetch with admin headers |
| 83 | console.log('\nStep 5: Testing API with explicit admin headers...'); |
| 84 | try { |
| 85 | const adminResponse = await fetch(apiUrl, { |
| 86 | headers: { |
| 87 | 'x-admin-override': 'true', |
| 88 | 'x-debug-mode': 'true' |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | const adminData = await adminResponse.json(); |
| 93 | console.log(`API with admin headers returned ${Array.isArray(adminData) ? adminData.length : 'non-array'} blog posts`); |
| 94 | } catch (error) { |
| 95 | console.log('API does not support custom admin headers (this is expected)'); |
| 96 | } |
| 97 | |
| 98 | // Step 6: Check for server logs |
| 99 | console.log('\nStep 6: Looking for server logs...'); |
| 100 | console.log('Please check your Next.js server logs for any of these messages:'); |
| 101 | console.log('- "Fetching blog posts..."'); |
| 102 | console.log('- "Admin request: showing all posts including unpublished"'); |
| 103 | console.log('- "Non-admin request: filtering to only show published posts"'); |
| 104 | console.log('- "Successfully fetched X blog posts (admin view)" or "(public view)"'); |
| 105 | |
| 106 | // Step 7: Recommendations |
| 107 | console.log('\nRecommendations:'); |
| 108 | console.log('1. Ensure your dev server was restarted after making changes'); |
| 109 | console.log('2. Verify BYPASS_AUTH_IN_DEV=true in .env.local'); |
| 110 | console.log('3. Add more console logs in src/app/api/blog/route.ts to debug'); |
| 111 | console.log('4. Check browser console/network tab for any errors'); |
| 112 | console.log('5. Confirm there are no issues with the Supabase connection'); |
| 113 | |
| 114 | } catch (error) { |
| 115 | console.error('Error with API connection:', error); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | main(); |