Firebase Admin SDK (Node.js) Basic Setup and User Management
📖 Scenario: You are building a backend service that manages users in a Firebase project. You will set up the Firebase Admin SDK in Node.js, configure it with service account credentials, create a new user, and then update that user's display name.
🎯 Goal: Set up Firebase Admin SDK in Node.js, initialize it with service account credentials, create a user with email and password, and update the user's display name.
📋 What You'll Learn
Create a variable admin by requiring firebase-admin
Initialize Firebase Admin SDK with a service account object called serviceAccount
Create a new user with email user@example.com and password securePass123
Update the created user's display name to John Doe
💡 Why This Matters
🌍 Real World
Backend services often need to manage users securely. Firebase Admin SDK allows server-side user management like creating, updating, and deleting users.
💼 Career
Understanding Firebase Admin SDK is useful for backend developers working with Firebase projects, enabling secure user management and integration with other backend logic.
Progress0 / 4 steps
1
Setup Firebase Admin SDK import and service account
Create a constant called admin by requiring the firebase-admin module. Then create a constant called serviceAccount and assign it the object { projectId: 'my-firebase-project', clientEmail: 'firebase-admin@my-firebase-project.iam.gserviceaccount.com', privateKey: '-----BEGIN PRIVATE KEY-----\nABC123\n-----END PRIVATE KEY-----\n' }.
Firebase
Hint
Use require('firebase-admin') to import the SDK. The serviceAccount is a plain object with keys projectId, clientEmail, and privateKey.
2
Initialize Firebase Admin SDK
Initialize Firebase Admin SDK by calling admin.initializeApp with an object that has a credential property set to admin.credential.cert(serviceAccount).
Firebase
Hint
Use admin.initializeApp with credential: admin.credential.cert(serviceAccount) to initialize the SDK.
3
Create a new user with email and password
Use admin.auth().createUser with an object containing email: 'user@example.com' and password: 'securePass123'. Assign the returned promise to a constant called userRecordPromise.
Firebase
Hint
Call admin.auth().createUser with the email and password inside an object. Assign the result to userRecordPromise.
4
Update the user's display name
Use userRecordPromise.then to get the userRecord. Inside the then callback, call admin.auth().updateUser with userRecord.uid and an object setting displayName to 'John Doe'.
Firebase
Hint
Use userRecordPromise.then to get the user record, then call admin.auth().updateUser with the user's UID and the new display name.
Practice
(1/5)
1. What is the primary purpose of the Firebase Admin SDK in Node.js?
easy
A. To build user interfaces for Firebase apps
B. To securely manage Firebase services from a server environment
C. To run Firebase apps directly in the browser
D. To replace Firebase client SDK on mobile devices
Solution
Step 1: Understand Firebase Admin SDK role
The Admin SDK is designed for server-side use to manage Firebase services securely.
Step 2: Differentiate from client SDKs
Client SDKs run on user devices for app interaction, while Admin SDK runs on servers for management tasks.
Final Answer:
To securely manage Firebase services from a server environment -> Option B
Quick Check:
Admin SDK = server management [OK]
Hint: Admin SDK is for backend server tasks, not frontend apps [OK]
Common Mistakes:
Confusing Admin SDK with client SDK
Thinking Admin SDK runs in browsers
Assuming Admin SDK builds UI
2. Which of the following is the correct way to initialize the Firebase Admin SDK in Node.js using a service account JSON file named serviceAccountKey.json?
easy
A. const admin = require('firebase-admin');
admin.initializeApp({credential: admin.credential.cert(require('./serviceAccountKey.json'))});
B. const admin = require('firebase-admin');
admin.initializeApp({serviceAccount: 'serviceAccountKey.json'});
C. const admin = require('firebase-admin');
admin.initializeApp({credential: admin.credential.applicationDefault()});
D. const admin = require('firebase-admin');
admin.initializeApp({credential: admin.credential.cert(serviceAccountKey.json)});
Solution
Step 1: Load service account JSON correctly
The service account JSON must be imported using require to pass the object, not a string path.
Step 2: Use admin.credential.cert with the JSON object
Pass the imported JSON object to admin.credential.cert for proper initialization.
Final Answer:
const admin = require('firebase-admin');
admin.initializeApp({credential: admin.credential.cert(require('./serviceAccountKey.json'))}); -> Option A
Quick Check:
Use require() to load JSON for cert [OK]
Hint: Use require() to load JSON, not a string path [OK]
Common Mistakes:
Passing file path string instead of JSON object
Using applicationDefault() when service account is needed
Incorrect property name like 'serviceAccount'
3. Given the following Node.js code using Firebase Admin SDK, what will be the output if the user with UID 'user123' exists?