AI Resource Pro / website / scripts / delete-user.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import dotenv from 'dotenv'; |
| 3 | |
| 4 | // Load environment variables from .env.local and .env |
| 5 | dotenv.config({ path: '.env.local' }); |
| 6 | dotenv.config({ path: '.env' }); |
| 7 | |
| 8 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; |
| 9 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || ''; |
| 10 | |
| 11 | if (!supabaseUrl || !supabaseServiceKey) { |
| 12 | console.error('Missing Supabase environment variables. Please check your .env file.'); |
| 13 | process.exit(1); |
| 14 | } |
| 15 | |
| 16 | // Create Supabase client with service role key for admin operations |
| 17 | const supabase = createClient(supabaseUrl, supabaseServiceKey, { |
| 18 | auth: { |
| 19 | persistSession: false, |
| 20 | }, |
| 21 | }); |
| 22 | |
| 23 | async function deleteUser(email: string) { |
| 24 | try { |
| 25 | console.log(`Attempting to delete user with email: ${email}`); |
| 26 | |
| 27 | // First, find the user by email |
| 28 | const { data: users, error: findError } = await supabase |
| 29 | .from('users') |
| 30 | .select('id, email, is_admin') |
| 31 | .eq('email', email) |
| 32 | .limit(1); |
| 33 | |
| 34 | if (findError) { |
| 35 | console.error('Error finding user:', findError.message); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (!users || users.length === 0) { |
| 40 | console.error(`User with email ${email} not found.`); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | const user = users[0]; |
| 45 | console.log('Found user:', user); |
| 46 | |
| 47 | // Confirm before deletion |
| 48 | if (user.is_admin) { |
| 49 | console.warn('WARNING: You are about to delete an admin user!'); |
| 50 | } |
| 51 | |
| 52 | // Delete the user from the users table |
| 53 | const { error: deleteError } = await supabase |
| 54 | .from('users') |
| 55 | .delete() |
| 56 | .eq('id', user.id); |
| 57 | |
| 58 | if (deleteError) { |
| 59 | console.error('Error deleting user from users table:', deleteError.message); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // Also delete the user from auth.users if they exist there |
| 64 | // Note: This requires the service role key and may not work if the auth schema is restricted |
| 65 | try { |
| 66 | const { error: authDeleteError } = await supabase.auth.admin.deleteUser(user.id); |
| 67 | |
| 68 | if (authDeleteError) { |
| 69 | console.error('Error deleting user from auth.users:', authDeleteError.message); |
| 70 | } else { |
| 71 | console.log('User successfully deleted from auth.users'); |
| 72 | } |
| 73 | } catch (authError) { |
| 74 | console.error('Error accessing auth.admin API:', authError); |
| 75 | } |
| 76 | |
| 77 | console.log(`User ${email} successfully deleted from the users table.`); |
| 78 | } catch (error) { |
| 79 | console.error('Unexpected error:', error); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Get email from command line argument |
| 84 | const email = process.argv[2]; |
| 85 | |
| 86 | if (!email) { |
| 87 | console.error('Please provide an email address as a command line argument.'); |
| 88 | console.error('Usage: npx ts-node scripts/delete-user.ts <email>'); |
| 89 | process.exit(1); |
| 90 | } |
| 91 | |
| 92 | // Execute the delete function |
| 93 | deleteUser(email) |
| 94 | .then(() => { |
| 95 | console.log('Operation completed.'); |
| 96 | process.exit(0); |
| 97 | }) |
| 98 | .catch((error) => { |
| 99 | console.error('Fatal error:', error); |
| 100 | process.exit(1); |
| 101 | }); |