📜  firebase-admin npm (1)

📅  最后修改于: 2023-12-03 15:15:04.963000             🧑  作者: Mango

Firebase-Admin npm

Firebase-Admin npm is a Node.js module that enables server-side access to Firebase services. It allows developers to manage Firebase projects and execute Firebase commands from the Node.js environment. The module provides a variety of functionalities, including authentication, database management, cloud storage, and messaging.

Installation

To install Firebase-Admin npm, use the following command:

npm install firebase-admin --save
Authentication

Firebase-Admin npm allows developers to authenticate with Firebase using a service account JSON file. The service account JSON file contains credentials for accessing Firebase services from the Node.js environment.

const admin = require('firebase-admin');

const serviceAccount = require('/path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
Database Management

Firebase-Admin npm provides database management functionality that allows developers to create, read, update, and delete data in the Firebase Realtime Database. The module offers functionalities such as setting data, updating data, and deleting data.

const admin = require('firebase-admin');

const serviceAccount = require('/path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://your-project-id.firebaseio.com'
});

const db = admin.database();
const ref = db.ref('users');

ref.set({
  alanisawesome: {
    date_of_birth: 'June 23, 1912',
    full_name: 'Alan Turing'
  },
  gracehop: {
    date_of_birth: 'December 9, 1906',
    full_name: 'Grace Hopper'
  }
});
Cloud Storage

Firebase-Admin npm also provides cloud storage management functionality that allows developers to manage files in Firebase Cloud Storage. The module offers functionalities such as uploading files, downloading files, and deleting files.

const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');

const serviceAccount = require('/path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  storageBucket: 'your-project-id.appspot.com'
});

const bucket = admin.storage().bucket();

async function uploadFile() {
  await bucket.upload('/path/to/file');
  console.log('File uploaded successfully');
}

async function downloadFile() {
  await bucket.file('/path/to/file').download({destination: '/path/to/local/file'});
  console.log('File downloaded successfully');
}
Messaging

Firebase-Admin npm also provides messaging functionality that allows developers to send notifications, topic messages, and data messages to devices using Firebase Cloud Messaging.

const admin = require('firebase-admin');

const serviceAccount = require('/path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

const registrationToken = 'YOUR_DEVICE_REGISTRATION_TOKEN';

const message = {
  notification: {
    title: 'Hello World',
    body: 'This is my first notification'
  },
  token: registrationToken
};

admin.messaging().send(message);
Conclusion

Firebase-Admin npm is an essential tool for Node.js developers who are building applications on Firebase. It provides a variety of functionalities, including authentication, database management, cloud storage, and messaging, that help developers manage their Firebase projects and execute Firebase commands from the Node.js environment.