0
0
Firebasecloud~20 mins

Firebase Admin SDK (Node.js) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firebase Admin SDK Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What is the output of this Firebase Admin SDK code snippet?

Consider this Node.js code using Firebase Admin SDK to fetch a user by UID:

import admin from 'firebase-admin';

admin.initializeApp();

async function getUser() {
  try {
    const userRecord = await admin.auth().getUser('nonexistentUID');
    console.log(userRecord.uid);
  } catch (error) {
    console.log(error.code);
  }
}

getUser();

What will be printed to the console?

Firebase
import admin from 'firebase-admin';

admin.initializeApp();

async function getUser() {
  try {
    const userRecord = await admin.auth().getUser('nonexistentUID');
    console.log(userRecord.uid);
  } catch (error) {
    console.log(error.code);
  }
}

getUser();
ASyntaxError
Bauth/user-not-found
Cundefined
Dnull
Attempts:
2 left
💡 Hint

Think about what happens when you request a user that does not exist in Firebase Authentication.

Configuration
intermediate
2:00remaining
Which option correctly initializes Firebase Admin SDK with a service account JSON file?

You want to initialize Firebase Admin SDK in Node.js using a service account JSON file located at ./serviceAccountKey.json. Which code snippet correctly does this?

A
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
B
import admin from 'firebase-admin';

admin.initializeApp({
  credential: admin.credential.applicationDefault()
});
C
import admin from 'firebase-admin';
import serviceAccount from './serviceAccountKey.json' assert { type: 'json' };

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
D
import admin from 'firebase-admin';

admin.initializeApp({
  credential: admin.credential.cert('./serviceAccountKey.json')
});
Attempts:
2 left
💡 Hint

Use ES modules import syntax and ensure the JSON file is imported correctly.

Architecture
advanced
2:00remaining
Which Firebase Admin SDK feature best supports sending notifications to multiple devices efficiently?

You want to send a push notification to thousands of devices using Firebase Admin SDK in Node.js. Which approach is best to optimize performance and cost?

ACall <code>admin.messaging().send()</code> individually for each device token in a loop.
BSend notifications via Firebase Realtime Database triggers only.
CUse <code>admin.messaging().sendToDevice()</code> with a single token repeatedly.
DUse <code>admin.messaging().sendMulticast()</code> to send to up to 500 tokens at once.
Attempts:
2 left
💡 Hint

Look for a method that supports batch sending to multiple tokens.

security
advanced
2:00remaining
What is the safest way to manage Firebase Admin SDK credentials in a production Node.js app?

You deploy a Node.js app using Firebase Admin SDK. Which practice ensures your service account credentials remain secure?

AStore the service account JSON file in environment variables or a secure secrets manager and load it at runtime.
BCommit the service account JSON file to the public GitHub repository for easy access.
CHardcode the service account credentials directly in the source code.
DShare the service account JSON file via email to all developers.
Attempts:
2 left
💡 Hint

Think about how to keep sensitive data out of source code and public repos.

🧠 Conceptual
expert
2:00remaining
What error will this Firebase Admin SDK code produce and why?

Analyze this Node.js code snippet:

import admin from 'firebase-admin';

admin.initializeApp();

async function createUser() {
  const user = await admin.auth().createUser({
    email: 'user@example.com',
    emailVerified: true,
    password: 'short',
  });
  console.log(user.uid);
}

createUser();

What error will occur when running this code and why?

Firebase
import admin from 'firebase-admin';

admin.initializeApp();

async function createUser() {
  const user = await admin.auth().createUser({
    email: 'user@example.com',
    emailVerified: true,
    password: 'short',
  });
  console.log(user.uid);
}

createUser();
Aauth/invalid-password - Password must be at least 6 characters long.
Bauth/email-already-exists - Email is already registered.
Cauth/invalid-email - Email format is incorrect.
DNo error, user is created successfully.
Attempts:
2 left
💡 Hint

Check Firebase password requirements for creating users.