fix: remove auth.admin dependency for user listing
Replaced auth.admin.listUsers() with profile-based approach to avoid service role key requirement. Shows partial user ID as placeholder for email. Added debug logging.
This commit is contained in:
@@ -27,11 +27,18 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
||||
|
||||
// Check if current user is admin
|
||||
const { data: { user } } = await supabase.auth.getUser();
|
||||
if (!user) return [];
|
||||
if (!user) {
|
||||
console.log('[getAllUsersWithCredits] No user found');
|
||||
return [];
|
||||
}
|
||||
|
||||
const isAdmin = await checkIsAdmin(user.id);
|
||||
if (!isAdmin) return [];
|
||||
if (!isAdmin) {
|
||||
console.log('[getAllUsersWithCredits] User is not admin');
|
||||
return [];
|
||||
}
|
||||
|
||||
console.log('[getAllUsersWithCredits] Fetching profiles...');
|
||||
// Get all users with their profiles
|
||||
const { data: profiles, error: profilesError } = await supabase
|
||||
.from('profiles')
|
||||
@@ -42,6 +49,8 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
console.log('[getAllUsersWithCredits] Found profiles:', profiles?.length);
|
||||
|
||||
// Get all user credits
|
||||
const { data: credits, error: creditsError } = await supabase
|
||||
.from('user_credits')
|
||||
@@ -49,25 +58,17 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
||||
|
||||
if (creditsError) {
|
||||
console.error('Error fetching credits:', creditsError);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get user emails from auth.users
|
||||
const { data: { users }, error: usersError } = await supabase.auth.admin.listUsers();
|
||||
console.log('[getAllUsersWithCredits] Found credits:', credits?.length);
|
||||
|
||||
if (usersError) {
|
||||
console.error('Error fetching users:', usersError);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Combine data
|
||||
// Combine data - we'll use profile id as email fallback
|
||||
const usersWithCredits: UserWithCredits[] = profiles?.map(profile => {
|
||||
const userAuth = users.find(u => u.id === profile.id);
|
||||
const userCredits = credits?.find(c => c.user_id === profile.id);
|
||||
|
||||
return {
|
||||
id: profile.id,
|
||||
email: userAuth?.email || 'Unknown',
|
||||
email: profile.id.substring(0, 8) + '...', // Show partial ID as placeholder
|
||||
username: profile.username || 'Unknown',
|
||||
balance: userCredits?.balance || 0,
|
||||
total_purchased: userCredits?.total_purchased || 0,
|
||||
@@ -75,10 +76,11 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
||||
daily_limit: userCredits?.daily_limit || null,
|
||||
google_search_cost: userCredits?.google_search_cost || 1,
|
||||
gemini_ai_cost: userCredits?.gemini_ai_cost || 1,
|
||||
last_active: userAuth?.last_sign_in_at || undefined
|
||||
last_active: undefined
|
||||
};
|
||||
}) || [];
|
||||
|
||||
console.log('[getAllUsersWithCredits] Returning users:', usersWithCredits.length);
|
||||
return usersWithCredits;
|
||||
} catch (err) {
|
||||
console.error('Error in getAllUsersWithCredits:', err);
|
||||
|
||||
Reference in New Issue
Block a user