/**
* Check if a user exists in both auth.users and user_profiles
* Run with: node check-user.js <email>
*/
const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
// Read .env file manually (handle Windows line endings)
const envContent = fs.readFileSync('.env', 'utf-8');
const envVars = {};
envContent.split(/\r?\n/).forEach(line => {
// Skip comments and empty lines
if (line.trim().startsWith('#') || !line.trim()) return;
const match = line.match(/^([^=]+)=(.*)$/);
if (match) {
const key = match[1].trim();
const value = match[2].trim().replace(/^["']|["']$/g, '');
envVars[key] = value;
}
});
const supabaseUrl = envVars.NEXT_PUBLIC_SUPABASE_URL;
const serviceRoleKey = envVars.SUPABASE_SERVICE_ROLE_KEY;
if (!supabaseUrl || !serviceRoleKey) {
console.error('❌ Missing environment variables');
console.error('NEXT_PUBLIC_SUPABASE_URL:', supabaseUrl ? '✓' : '✗');
console.error('SUPABASE_SERVICE_ROLE_KEY:', serviceRoleKey ? '✓' : '✗');
process.exit(1);
}
const email = process.argv[2];
if (!email) {
console.error('❌ Please provide an email: node check-user.js dev@test.com');
process.exit(1);
}
const supabase = createClient(supabaseUrl, serviceRoleKey, {
auth: { autoRefreshToken: false, persistSession: false }
});
async function checkUser(email) {
console.log(`\n🔍 Checking user: ${email}\n`);
// Check auth.users (via admin API)
console.log('1️⃣ Checking auth.users table...');
const { data: { users }, error: authError } = await supabase.auth.admin.listUsers();
if (authError) {
console.error('❌ Error fetching auth users:', authError);
return;
}
const authUser = users.find(u => u.email === email);
if (authUser) {
console.log('✅ User found in auth.users:');
console.log(' ID:', authUser.id);
console.log(' Email:', authUser.email);
console.log(' Email confirmed:', authUser.email_confirmed_at ? 'Yes' : 'No');
console.log(' Created:', authUser.created_at);
} else {
console.log('❌ User NOT found in auth.users');
console.log(' → Need to create user via Supabase Dashboard or auth.signUp()');
return;
}
// Check user_profiles
console.log('\n2️⃣ Checking user_profiles table...');
const { data: profile, error: profileError } = await supabase
.from('user_profiles')
.select('*')
.eq('id', authUser.id)
.maybeSingle();
if (profileError) {
console.error('❌ Error fetching profile:', profileError);
return;
}
if (profile) {
console.log('✅ Profile found:');
console.log(' ID:', profile.id);
console.log(' Display name:', profile.display_name || '(none)');
console.log(' Created:', profile.created_at);
} else {
console.log('❌ Profile NOT found in user_profiles');
console.log(' → NextAuth will try to create it on first sign-in');
console.log(' → Let\'s create it now...\n');
// Create profile
const { data: newProfile, error: createError } = await supabase
.from('user_profiles')
.insert({
id: authUser.id,
display_name: email.split('@')[0],
email: email,
})
.select()
.single();
if (createError) {
console.error('❌ Error creating profile:', createError);
} else {
console.log('✅ Profile created successfully!');
console.log(' ID:', newProfile.id);
console.log(' Display name:', newProfile.display_name);
}
}
// Summary
console.log('\n📊 Summary:');
console.log(' Auth user:', authUser ? '✅' : '❌');
console.log(' Profile:', profile ? '✅' : '❌');
console.log('\n✅ You should now be able to sign in with this account!');
}
checkUser(email)
.then(() => process.exit(0))
.catch(err => {
console.error('\n❌ Error:', err);
process.exit(1);
});