Cloud FunctionsBeginner
1Question 1 of 1
typescriptHow do Firebase Cloud Functions work and how do you write HTTPS callable functions?
Explanation & Concept
Cloud Functions for Firebase allows running serverless backend code in response to HTTPS requests or Firebase events (such as Firestore document updates, Auth user creation, or Storage uploads).
**Callable Functions** automatically deserialize parameters and inject authentication context (`context.auth`), ensuring secure client-to-server communication.
Code Example
typescript
import { onCall, HttpsError } from "firebase-functions/v2/https";
import { getFirestore } from "firebase-admin/firestore";
export const addAdminRole = onCall(async (request) => {
// Enforce caller authentication and authorization
if (!request.auth || !request.auth.token.admin) {
throw new HttpsError("permission-denied", "Only administrators can assign roles.");
}
const { targetUid } = request.data;
const db = getFirestore();
await db.collection("users").doc(targetUid).update({ role: "admin" });
return { success: true, message: `User ${targetUid} promoted to admin.` };
});How It Works & Key Takeaways
Callable functions handle CORS, authentication verification, and error serialization automatically.
Mock Test · 1 Questions · Optional
Ready for Realtime Database vs Firestore?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner1 questions