acceptInvite
The
acceptInvite function allows a user to accept an invitation to a subscription.The acceptInvite Firebase Cloud Function enables a user to accept a pending invitation to join a subscription. Upon successful acceptance, the user’s UID is added to the relevant permission groups within the subscription, and the invite’s status is updated.
Function Signature
export const acceptInvite = https.onCall(async (data: AcceptInviteData, context) => { ... });
Parameters
The function expects the following data in the data object:
| Parameter | Type | Description | Required |
|---|---|---|---|
inviteId | string | The ID of the invitation to be accepted. | Yes |
AcceptInviteData Interface:
interface AcceptInviteData {
inviteId: string;
}
Context
The function requires an authenticated user context (context.auth).
Behavior
- Authentication Check: Verifies that the user calling the function is authenticated. If not, it throws an
unauthenticatederror. - Invite ID Validation: Ensures that
inviteIdis provided in the request data. If missing, it throws aninvalid-argumenterror. - Invite Retrieval: Fetches the invite document from Firestore using the provided
inviteId.- If the invite does not exist, it throws a
not-founderror.
- If the invite does not exist, it throws a
- Invite Status and User Verification:
- Checks if the invite’s status is ‘pending’. If not, it throws a
failed-preconditionerror. - Verifies that the email associated with the invite matches the authenticated user’s email. If not, it throws a
permission-deniederror.
- Checks if the invite’s status is ‘pending’. If not, it throws a
- Subscription Retrieval: Fetches the associated subscription document.
- If the subscription does not exist, it throws a
not-founderror.
- If the subscription does not exist, it throws a
- Transaction: Executes a Firestore transaction to ensure atomicity of updates:
- Updates Subscription Permissions: Adds the authenticated user’s UID to the permission groups specified in the invite data within the subscription document.
- Updates Invite Status: Changes the invite’s status to ‘accepted’, records the
accept_time, andaccepted_byUID.
- Success: Returns
{ success: true }upon successful completion.
Error Handling
The function throws HttpsError with specific codes for different failure scenarios:
unauthenticated: If the user is not authenticated.invalid-argument: IfinviteIdis missing.not-found: If the invite or associated subscription is not found.failed-precondition: If the invite is not in a ‘pending’ state.permission-denied: If the invite is for a different user.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 acceptInviteCallable = httpsCallable(functions, 'acceptInvite');
async function callAcceptInvite(inviteId: string) {
try {
const result = await acceptInviteCallable({ inviteId });
console.log('Invite accepted successfully:', result.data);
// Expected result.data: { success: true }
} catch (error) {
console.error('Error accepting invite:', error.code, error.message);
}
}
// Example call
// callAcceptInvite('your-invite-id-here');
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.