Introduction to Firebase Admin SDK
The Firebase Admin SDK allows you to integrate advanced Firebase features and tools into your applications, automating many common tasks and extending the functionality of Firebase. This guide covers dozens of useful APIs with code snippets to help you get started.
Setting Up Firebase Admin SDK
// Import the Firebase Admin SDK
var admin = require('firebase-admin');
// Initialize the app
var serviceAccount = require('path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
APIs for Authentication
Create a New User
admin.auth().createUser({
email: "user@example.com",
emailVerified: false,
phoneNumber: "+1234567890",
password: "password",
displayName: "John Doe",
photoURL: "http://www.example.com/12345678/photo.png",
disabled: false
})
.then((userRecord) => {
console.log('Successfully created new user:', userRecord.uid);
})
.catch((error) => {
console.log('Error creating new user:', error);
});
Verify User Token
admin.auth().verifyIdToken(idToken)
.then((decodedToken) => {
const uid = decodedToken.uid;
// Further processing with UID
})
.catch((error) => {
console.log('Error verifying token:', error);
});
APIs for Firestore
Add Data to Firestore
var db = admin.firestore();
var docRef = db.collection('users').doc('alovelace');
var setAda = docRef.set({
first: 'Ada',
last: 'Lovelace',
born: 1815
});
Read Data from Firestore
var docRef = db.collection('users').doc('alovelace');
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
APIs for Realtime Database
Write Data
var db = admin.database();
var ref = db.ref("server/saving-data/fireblog");
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
Read Data
var ref = db.ref("server/saving-data/fireblog/users");
ref.once("value", function(snapshot) {
console.log(snapshot.val());
});
Full Application Example
var admin = require('firebase-admin');
var serviceAccount = require('path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://.firebaseio.com"
});
// Create a new user
admin.auth().createUser({
email: "user@example.com",
password: "password",
displayName: "John Doe"
}).then((userRecord) => {
console.log('Successfully created new user:', userRecord.uid);
// Add user data to Firestore
var db = admin.firestore();
var userRef = db.collection('users').doc(userRecord.uid);
userRef.set({
displayName: "John Doe",
email: "user@example.com"
});
// Write user profile to Realtime Database
var db = admin.database();
var ref = db.ref("users/" + userRecord.uid);
ref.set({
profile: {
displayName: "John Doe",
email: "user@example.com"
}
});
}).catch((error) => {
console.log('Error creating new user:', error);
});
Using the Firebase Admin SDK allows you to create powerful applications that integrate seamlessly with Firebase services, improving the user experience and simplifying back-end management.
Hash: 2277f852f6c18ce5f0c1355ccafab07e7c559f7fe960195022833ffea0d2fc38