0
0
Firebasecloud~30 mins

Authentication trigger functions in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Firebase Authentication Trigger Functions
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Both functions should be exported using exports.functionName = ... syntax.