AI Resource Pro / website / scripts / check-admin-users.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import * as dotenv from 'dotenv'; |
| 3 | import * as path from 'path'; |
| 4 | import * as fs from 'fs'; |
| 5 | |
| 6 | // Load environment variables from .env.local |
| 7 | const envLocalPath = path.resolve(process.cwd(), '.env.local'); |
| 8 | if (fs.existsSync(envLocalPath)) { |
| 9 | console.log(`Loading environment from: ${envLocalPath}`); |
| 10 | dotenv.config({ path: envLocalPath }); |
| 11 | } else { |
| 12 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 13 | dotenv.config(); // Try loading from default .env |
| 14 | } |
| 15 | |
| 16 | async function checkAdminUsers() { |
| 17 | console.log('=== Checking Supabase Admin Users ==='); |
| 18 | |
| 19 | // Get Supabase URL and service role key |
| 20 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 21 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 22 | |
| 23 | console.log('Environment variables:'); |
| 24 | console.log(`NEXT_PUBLIC_SUPABASE_URL: ${supabaseUrl ? 'Found (starts with ' + supabaseUrl.substring(0, 10) + '...)' : 'Not found'}`); |
| 25 | console.log(`SUPABASE_SERVICE_ROLE_KEY: ${supabaseServiceKey ? 'Found (starts with ' + supabaseServiceKey.substring(0, 10) + '...)' : 'Not found'}`); |
| 26 | |
| 27 | if (!supabaseUrl || !supabaseServiceKey) { |
| 28 | console.error('Missing Supabase environment variables. Please set NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY.'); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Create Supabase client with service role key |
| 33 | const supabase = createClient(supabaseUrl, supabaseServiceKey); |
| 34 | |
| 35 | try { |
| 36 | // First check if the users table exists |
| 37 | console.log('\nChecking if users table exists...'); |
| 38 | const { data: tableData, error: tableError } = await supabase |
| 39 | .from('users') |
| 40 | .select('id') |
| 41 | .limit(1); |
| 42 | |
| 43 | if (tableError) { |
| 44 | if (tableError.code === '42P01') { |
| 45 | console.error('Error: The users table does not exist.'); |
| 46 | return; |
| 47 | } |
| 48 | console.error('Error checking users table:', tableError); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | console.log('Users table exists.'); |
| 53 | |
| 54 | // Check for admin users |
| 55 | console.log('\nChecking for admin users...'); |
| 56 | const { data: adminUsers, error: adminError } = await supabase |
| 57 | .from('users') |
| 58 | .select('*') |
| 59 | .eq('is_admin', true); |
| 60 | |
| 61 | if (adminError) { |
| 62 | console.error('Error checking admin users:', adminError); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if (!adminUsers || adminUsers.length === 0) { |
| 67 | console.log('No admin users found in the database.'); |
| 68 | } else { |
| 69 | console.log(`Found ${adminUsers.length} admin users:`); |
| 70 | adminUsers.forEach((user, i) => { |
| 71 | console.log(`\nAdmin User ${i+1}:`); |
| 72 | console.log(`ID: ${user.id}`); |
| 73 | console.log(`Email: ${user.email}`); |
| 74 | console.log(`Name: ${user.name || '(not set)'}`); |
| 75 | console.log(`Created At: ${user.created_at || '(not set)'}`); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | // Check all users in auth.users |
| 80 | console.log('\nChecking all auth users...'); |
| 81 | try { |
| 82 | const { data: authData, error: authError } = await supabase.auth.admin.listUsers(); |
| 83 | |
| 84 | if (authError) { |
| 85 | console.error('Error listing auth users:', authError); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (!authData || !authData.users || authData.users.length === 0) { |
| 90 | console.log('No users found in auth.users.'); |
| 91 | } else { |
| 92 | console.log(`Found ${authData.users.length} users in auth.users:`); |
| 93 | |
| 94 | // For each auth user, check if they are in the users table and if they are an admin |
| 95 | for (const authUser of authData.users) { |
| 96 | const { data: userData, error: userError } = await supabase |
| 97 | .from('users') |
| 98 | .select('*') |
| 99 | .eq('id', authUser.id) |
| 100 | .single(); |
| 101 | |
| 102 | console.log(`\nAuth User:`); |
| 103 | console.log(`ID: ${authUser.id}`); |
| 104 | console.log(`Email: ${authUser.email}`); |
| 105 | console.log(`Confirmed: ${authUser.email_confirmed_at ? 'Yes' : 'No'}`); |
| 106 | console.log(`Last Sign In: ${authUser.last_sign_in_at || '(never)'}`); |
| 107 | |
| 108 | if (userError) { |
| 109 | console.log(` - Not found in users table`); |
| 110 | } else { |
| 111 | console.log(` - Found in users table`); |
| 112 | console.log(` - Admin: ${userData.is_admin ? 'Yes' : 'No'}`); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } catch (e) { |
| 117 | console.error('Error accessing auth users:', e); |
| 118 | } |
| 119 | |
| 120 | } catch (error) { |
| 121 | console.error('Error checking admin users:', error); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Run the function |
| 126 | checkAdminUsers().catch((error) => { |
| 127 | console.error('Unhandled error:'); |
| 128 | console.error(error); |
| 129 | process.exit(1); |
| 130 | }); |