Fix: Make test user creation idempotent

This commit is contained in:
gpt-engineer-app[bot]
2025-10-30 17:53:52 +00:00
parent 1f727d6a76
commit 3318fdaa3f

View File

@@ -34,19 +34,32 @@ export async function setupTestUser(
throw new Error('Service role key not configured');
}
// Create user in auth
const { data: authData, error: authError } = await supabaseAdmin.auth.admin.createUser({
email,
password,
email_confirm: true,
});
// Check if user already exists
const { data: existingUsers } = await supabaseAdmin.auth.admin.listUsers();
const existingUser = existingUsers?.users.find(u => u.email === email);
if (authError) throw authError;
if (!authData.user) throw new Error('User creation failed');
let userId: string;
const userId = authData.user.id;
if (existingUser) {
// User exists - use their ID
userId = existingUser.id;
console.log(` Using existing test user: ${email}`);
} else {
// User doesn't exist - create new one
const { data: authData, error: authError } = await supabaseAdmin.auth.admin.createUser({
email,
password,
email_confirm: true,
});
// Create profile
if (authError) throw authError;
if (!authData.user) throw new Error('User creation failed');
userId = authData.user.id;
console.log(`✓ Created new test user: ${email}`);
}
// Create or update profile (ensures correct role and is_test_data flag)
const { error: profileError } = await supabaseAdmin
.from('profiles')
.upsert({