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?
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();
Think about what happens when you request a user that does not exist in Firebase Authentication.
The Firebase Admin SDK throws an error with code auth/user-not-found when trying to get a user that does not exist. The catch block logs this error code.
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?
Use ES modules import syntax and ensure the JSON file is imported correctly.
In ES modules, JSON files must be imported with assert { type: 'json' }. Then pass the imported JSON object to admin.credential.cert(). Option C does this correctly.
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?
Look for a method that supports batch sending to multiple tokens.
sendMulticast() allows sending a message to up to 500 device tokens in one call, which is efficient for large scale notifications. Sending individually (A) is inefficient. Option D is unrelated to sending messages directly. Option D sends to one token repeatedly, which is inefficient.
You deploy a Node.js app using Firebase Admin SDK. Which practice ensures your service account credentials remain secure?
Think about how to keep sensitive data out of source code and public repos.
Storing credentials in environment variables or a secure secrets manager keeps them out of source code and version control, reducing risk of leaks. Committing to public repos or hardcoding exposes credentials. Sharing via email is insecure.
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?
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();
Check Firebase password requirements for creating users.
Firebase requires passwords to be at least 6 characters. The password 'short' has 5 characters, so auth/invalid-password error is thrown.