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
|
// Check if current user is admin
|
||||||
const { data: { user } } = await supabase.auth.getUser();
|
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);
|
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
|
// Get all users with their profiles
|
||||||
const { data: profiles, error: profilesError } = await supabase
|
const { data: profiles, error: profilesError } = await supabase
|
||||||
.from('profiles')
|
.from('profiles')
|
||||||
@@ -42,6 +49,8 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('[getAllUsersWithCredits] Found profiles:', profiles?.length);
|
||||||
|
|
||||||
// Get all user credits
|
// Get all user credits
|
||||||
const { data: credits, error: creditsError } = await supabase
|
const { data: credits, error: creditsError } = await supabase
|
||||||
.from('user_credits')
|
.from('user_credits')
|
||||||
@@ -49,25 +58,17 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
|||||||
|
|
||||||
if (creditsError) {
|
if (creditsError) {
|
||||||
console.error('Error fetching credits:', creditsError);
|
console.error('Error fetching credits:', creditsError);
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user emails from auth.users
|
console.log('[getAllUsersWithCredits] Found credits:', credits?.length);
|
||||||
const { data: { users }, error: usersError } = await supabase.auth.admin.listUsers();
|
|
||||||
|
|
||||||
if (usersError) {
|
// Combine data - we'll use profile id as email fallback
|
||||||
console.error('Error fetching users:', usersError);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Combine data
|
|
||||||
const usersWithCredits: UserWithCredits[] = profiles?.map(profile => {
|
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);
|
const userCredits = credits?.find(c => c.user_id === profile.id);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: profile.id,
|
id: profile.id,
|
||||||
email: userAuth?.email || 'Unknown',
|
email: profile.id.substring(0, 8) + '...', // Show partial ID as placeholder
|
||||||
username: profile.username || 'Unknown',
|
username: profile.username || 'Unknown',
|
||||||
balance: userCredits?.balance || 0,
|
balance: userCredits?.balance || 0,
|
||||||
total_purchased: userCredits?.total_purchased || 0,
|
total_purchased: userCredits?.total_purchased || 0,
|
||||||
@@ -75,10 +76,11 @@ export async function getAllUsersWithCredits(): Promise<UserWithCredits[]> {
|
|||||||
daily_limit: userCredits?.daily_limit || null,
|
daily_limit: userCredits?.daily_limit || null,
|
||||||
google_search_cost: userCredits?.google_search_cost || 1,
|
google_search_cost: userCredits?.google_search_cost || 1,
|
||||||
gemini_ai_cost: userCredits?.gemini_ai_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;
|
return usersWithCredits;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error in getAllUsersWithCredits:', err);
|
console.error('Error in getAllUsersWithCredits:', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user