Authentication trigger functions in Firebase - Time & Space Complexity
When using authentication trigger functions in Firebase, it's important to understand how the time to run these functions changes as more users sign up or log in.
We want to know how the function's work grows when many users trigger it.
Analyze the time complexity of the following Firebase authentication trigger function.
exports.onUserCreate = functions.auth.user().onCreate((user) => {
const userId = user.uid;
return admin.firestore().collection('profiles').doc(userId).set({
email: user.email,
createdAt: admin.firestore.FieldValue.serverTimestamp()
});
});
This function runs when a new user signs up. It creates a profile document in Firestore for that user.
Look for repeated work inside the function.
- Primary operation: Writing one document to Firestore per user creation.
- How many times: Once per user sign-up event.
Each new user triggers one write operation to Firestore.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes |
| 100 | 100 writes |
| 1000 | 1000 writes |
Pattern observation: The work grows directly with the number of users signing up.
Time Complexity: O(n)
This means the time to handle all sign-ups grows linearly with the number of new users.
[X] Wrong: "The function runs once and handles all users at once."
[OK] Correct: Each user sign-up triggers the function separately, so the work adds up with each new user.
Understanding how trigger functions scale helps you design systems that handle many users smoothly and predict how your app behaves as it grows.
What if the function also read multiple documents from Firestore for each new user? How would the time complexity change?
Practice
onCreate do?Solution
Step 1: Understand the purpose of
TheonCreateonCreatetrigger runs when a new user account is created in Firebase Authentication.Step 2: Match the trigger to the correct action
Since it runs on new user sign-up, it does not delete users or send emails directly.Final Answer:
It runs automatically when a new user signs up. -> Option CQuick Check:
onCreate= New user signup trigger [OK]
- Confusing onCreate with onDelete trigger
- Thinking onCreate sends emails automatically
- Assuming onCreate updates user data
Solution
Step 1: Identify the correct trigger for user deletion
The correct trigger for user deletion isonDeleteattached tofunctions.auth.user().Step 2: Check the syntax for exporting the function
The function should be exported usingexports.functionName = ...syntax.Final Answer:
exports.userDeleted = functions.auth.user().onDelete((user) => { /* code */ }); -> Option BQuick Check:
onDelete trigger syntax = exports.userDeleted = functions.auth.user().onDelete((user) => { /* code */ }); [OK]
- Using onCreate instead of onDelete for deletion
- Using non-existent onRemove trigger
- Not exporting the function properly
exports.welcomeUser = functions.auth.user().onCreate((user) => {
console.log(`Welcome, ${user.email}!`);
});Solution
Step 1: Understand the onCreate trigger behavior
The function runs automatically when a new user signs up, receiving the user object with an email property.Step 2: Analyze the console.log statement
The template string inserts the user's email, so it logs "Welcome, user@example.com!" if the user's email is user@example.com.Final Answer:
Welcome, user@example.com! -> Option DQuick Check:
onCreate logs user email correctly [OK]
- Assuming user.email is undefined
- Thinking function does not run on sign-up
- Misreading template string syntax
exports.deleteUserData = functions.auth.user().onDelete(user => {
const uid = user.uid;
admin.database().ref(`/users/${uid}`).remove();
});Solution
Step 1: Check for admin SDK initialization
The code usesadmin.database()but does not show admin being imported or initialized, which causes runtime errors.Step 2: Verify other parts of the code
The trigger and user parameter usage are correct; missing return is not mandatory but recommended.Final Answer:
admin is not initialized or imported. -> Option AQuick Check:
admin SDK must be initialized before use [OK]
- Confusing onDelete with onCreate trigger
- Ignoring admin SDK initialization
- Thinking return statement is mandatory
@example.com. Which code snippet correctly implements this using Firebase Authentication trigger functions?
Solution
Step 1: Choose the correct trigger for new user sign-up
UseonCreateto run code when a user signs up, notonDelete.Step 2: Check the email condition for domain matching
UseendsWith('@example.com')to check if the email ends with the domain. Usingincludesor equality check is incorrect here.Final Answer:
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => { if (user.email.endsWith('@example.com')) { sendEmail(user.email); } }); -> Option AQuick Check:
Use onCreate + endsWith() for domain check [OK]
- Using onDelete instead of onCreate
- Checking email equality instead of endsWith
- Using indexOf === 0 instead of endsWith
Ready to prove it?
Pass the test and this pattern moves unknown
