📖 Scenario: You are building a simple app that needs to track when users sign up and sign in. You will use Firebase Authentication trigger functions to react to these events and store user data in the database.
🎯 Goal: Create Firebase Authentication trigger functions that run when a user signs up and when a user signs in. These functions will write user information to the Firestore database.
📋 What You'll Learn
Create a Firestore collection called users to store user data.
Write a Firebase function called onUserCreate that triggers when a user signs up.
Write a Firebase function called onUserSignIn that triggers when a user signs in.
In onUserCreate, add a document to users with the user's uid, email, and createdAt timestamp.
In onUserSignIn, update the user's document with the lastSignIn timestamp.
💡 Why This Matters
🌍 Real World
Authentication triggers are used in real apps to keep user data updated and perform actions automatically when users sign up or sign in.
💼 Career
Understanding Firebase Authentication triggers is important for backend and full-stack developers working with serverless architectures and real-time databases.
Progress0 / 4 steps
1
Set up Firestore and import Firebase functions
Write the code to import functions from firebase-functions and admin from firebase-admin. Initialize the Firebase admin app and get a Firestore database reference called db.
Firebase
Hint
Use require to import modules. Initialize admin with admin.initializeApp(). Get Firestore with admin.firestore().
2
Create the user creation trigger function
Write a Firebase Authentication trigger function called onUserCreate that triggers on user creation using functions.auth.user().onCreate. Inside the function, create a document in the users collection with the user's uid as the document ID. Store the user's email and the current timestamp as createdAt.
Firebase
Hint
Use functions.auth.user().onCreate to trigger on user signup. Use db.collection('users').doc(user.uid).set() to save data.
3
Create the user sign-in trigger function
Write a Firebase Authentication trigger function called onUserSignIn that triggers on user sign-in using functions.auth.user().onLogin. Inside the function, update the user's document in the users collection with the current timestamp as lastSignIn. (Note: Firebase does not have a built-in onLogin trigger, so simulate this with a callable function named onUserSignIn that accepts a uid parameter.)
Firebase
Hint
Firebase does not have an onLogin trigger. Use a callable function with functions.https.onCall that accepts uid and updates the user's lastSignIn.
4
Export both functions for deployment
Ensure both onUserCreate and onUserSignIn functions are exported properly in the module exports so they can be deployed to Firebase.
Firebase
Hint
Both functions should be exported using exports.functionName = ... syntax.
Practice
(1/5)
1. What does the Firebase Authentication trigger function onCreate do?
easy
A. It sends a password reset email.
B. It deletes a user from the database.
C. It runs automatically when a new user signs up.
D. It updates user profile information.
Solution
Step 1: Understand the purpose of onCreate
The onCreate trigger 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 C
Quick Check:
onCreate = New user signup trigger [OK]
Hint: Remember: onCreate = new user signup trigger [OK]
Common Mistakes:
Confusing onCreate with onDelete trigger
Thinking onCreate sends emails automatically
Assuming onCreate updates user data
2. Which of the following is the correct syntax to define a Firebase Authentication trigger function for user deletion in Node.js?
easy
A. functions.auth.user().onCreate((user) => { /* code */ });
B. exports.userDeleted = functions.auth.user().onDelete((user) => { /* code */ });
C. exports.userDeleted = functions.auth.user().onRemove((user) => { /* code */ });
D. functions.auth.user().delete((user) => { /* code */ });
Solution
Step 1: Identify the correct trigger for user deletion
The correct trigger for user deletion is onDelete attached to functions.auth.user().
Step 2: Check the syntax for exporting the function
The function should be exported using exports.functionName = ... syntax.
B. Incorrect trigger function; should use onCreate instead of onDelete.
C. Missing return statement for the database operation.
D. The user parameter should be destructured as { uid }.
Solution
Step 1: Check for admin SDK initialization
The code uses admin.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 A
Quick Check:
admin SDK must be initialized before use [OK]
Hint: Always import and initialize admin SDK before database calls [OK]
Common Mistakes:
Confusing onDelete with onCreate trigger
Ignoring admin SDK initialization
Thinking return statement is mandatory
5. You want to send a welcome email only to users who sign up with an email ending in @example.com. Which code snippet correctly implements this using Firebase Authentication trigger functions?
hard
A. exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
if (user.email.endsWith('@example.com')) {
sendEmail(user.email);
}
});
B. exports.sendWelcomeEmail = functions.auth.user().onDelete((user) => {
if (user.email.includes('@example.com')) {
sendEmail(user.email);
}
});
C. exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
if (user.email === '@example.com') {
sendEmail(user.email);
}
});
D. exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
if (user.email.indexOf('@example.com') === 0) {
sendEmail(user.email);
}
});
Solution
Step 1: Choose the correct trigger for new user sign-up
Use onCreate to run code when a user signs up, not onDelete.
Step 2: Check the email condition for domain matching
Use endsWith('@example.com') to check if the email ends with the domain. Using includes or equality check is incorrect here.
Final Answer:
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
if (user.email.endsWith('@example.com')) {
sendEmail(user.email);
}
}); -> Option A
Quick Check:
Use onCreate + endsWith() for domain check [OK]
Hint: Use onCreate and endsWith('@domain.com') to filter emails [OK]
Common Mistakes:
Using onDelete instead of onCreate
Checking email equality instead of endsWith
Using indexOf === 0 instead of endsWith
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.