getUserSubscriptions
getUserSubscriptions function retrieves all subscriptions the authenticated user belongs to.The getUserSubscriptions Firebase Cloud Function returns every subscription in which the authenticated user holds the default permission role (typically access). It runs with Admin SDK privileges, bypassing Firestore security rules, so the client never needs direct collection-query access to the subscriptions collection.
This function replaces the previous pattern of querying the subscriptions collection directly from the client, which required allow list to be open to all authenticated users and exposed every subscription document to enumeration.
Function Signature
export const getUserSubscriptions = https.onCall(async (_, context) => { ... });
Parameters
This function takes no input data.
Context
The function requires an authenticated user context (context.auth). If the caller is not authenticated, it throws an unauthenticated error.
Behavior
- Authentication Check: Verifies the caller is authenticated.
- Permission Query: Queries the
subscriptionscollection for documents wherepermissions.<defaultPermission>array contains the caller’s UID. The default permission key is resolved fromglobal.saasConfig.permissionsat startup. - Success: Returns an object containing
subscriptions— an array of subscription documents, each including the documentidand all fields fromSubscriptionData.
Return Value
{
subscriptions: Array<{ id: string } & SubscriptionData>
}
Error Handling
| Code | Condition |
|---|---|
unauthenticated | Caller is not authenticated |
internal | Unexpected server-side error |
Example Usage (Client-side)
import { getFunctions, httpsCallable } from 'firebase/functions';
import { getApp } from 'firebase/app';
const functions = getFunctions(getApp());
const getUserSubscriptions = httpsCallable(functions, 'getUserSubscriptions');
async function loadUserSubscriptions() {
try {
const result = await getUserSubscriptions();
console.log('Subscriptions:', result.data.subscriptions);
// result.data: { subscriptions: Array<{ id: string } & SubscriptionData> }
} catch (error) {
console.error('Error loading subscriptions:', error.code, error.message);
}
}
Firestore Rules
Because subscription loading is handled server-side by this function, the subscriptions collection should block all client-side list queries:
match /subscriptions/{docId} {
// List queries disabled — use getUserSubscriptions Cloud Function instead
allow list: if false;
allow get: if request.auth != null
&& get(/databases/$(database)/documents/subscriptions/$(docId)).data.permissions.access.hasAny([request.auth.uid]);
...
}
Deployment
Export the function from your project’s functions/src/index.ts:
export {
// ... other functions
getUserSubscriptions
} from '@fireact.dev/functions';
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.