getSubscriptionUsers
The
getSubscriptionUsers function retrieves a list of users associated with a subscription.The getSubscriptionUsers Firebase Cloud Function retrieves a paginated list of active users and pending invited users for a given subscription. This function is accessible only to users with admin permissions within that subscription.
Function Signature
export const getSubscriptionUsers = https.onCall(async (data, context) => { ... });
Parameters
The function expects the following data in the data object:
| Parameter | Type | Description | Required |
|---|---|---|---|
subscriptionId | string | The ID of the subscription for which to retrieve users. | Yes |
page | number | (Optional) The page number for pagination (default: 1). | No |
pageSize | number | (Optional) The number of users per page (default: 10). | No |
Context
The function requires an authenticated user context (context.auth). The authenticated user must also possess an admin permission level within the target subscription.
Behavior
- Authentication Check: Verifies that the user calling the function is authenticated. If not, it throws an
unauthenticatederror. - Subscription Retrieval: Fetches the subscription document from Firestore using the provided
subscriptionId.- If the subscription does not exist, it throws a
not-founderror.
- If the subscription does not exist, it throws a
- Admin Permission Check: Checks if the authenticated user has any admin permission level within the subscription.
- If not, it throws a
permission-deniederror.
- If not, it throws a
- User ID Extraction: Gathers all unique user UIDs from the subscription’s
permissionsfield. - Active User Details Retrieval: For each active user ID:
- Retrieves user data from Firebase Auth.
- Retrieves additional user data from the Firestore
userscollection. - Combines this information into a
UserDetailsobject, including their assigned permissions.
- Pending Invite Retrieval: Queries Firestore for all pending invites associated with the
subscriptionId.- Transforms invite data into
UserDetailsobjects with astatusof ‘pending’ andpending_permissions.
- Transforms invite data into
- Combine and Sort: Merges the lists of active users and pending invites, then sorts them by
create_timestampin descending order (newest first). - Pagination: Applies pagination based on
pageandpageSizeparameters. - Success: Returns an object containing
users(the paginated list ofUserDetails) andtotal(the total count of all users and pending invites).
Error Handling
The function throws HttpsError with specific codes for different failure scenarios:
unauthenticated: If the user is not authenticated.not-found: If the subscription is not found.permission-denied: If the user does not have admin permission level.internal: For any other unexpected errors during execution.
Example Usage (Client-side)
import { getFunctions, httpsCallable } from 'firebase/functions';
import { getApp } from 'firebase/app';
const functions = getFunctions(getApp());
const getSubscriptionUsersCallable = httpsCallable(functions, 'getSubscriptionUsers');
async function callGetSubscriptionUsers(subscriptionId: string, page: number = 1, pageSize: number = 10) {
try {
const result = await getSubscriptionUsersCallable({ subscriptionId, page, pageSize });
console.log('Subscription users:', result.data.users);
console.log('Total users:', result.data.total);
// Expected result.data: { users: UserDetails[], total: number }
} catch (error) {
console.error('Error getting subscription users:', error.code, error.message);
}
}
// Example call
// callGetSubscriptionUsers('sub_your_stripe_subscription_id', 1, 5);
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.