Firebase Admin Tools

Admin → Gold
đź’° $2000

Interface to manage authentication, storage, and real-time database operations.

Technology iconTechnology iconTechnology icon

Firebase Admin Tools Overview

Firebase Admin Tools is a powerful interface designed to streamline the management of Firebase services, including authentication, storage, and real-time database operations. This module provides developers with a centralized platform to handle critical backend tasks efficiently, enabling them to focus on building robust and scalable applications.

Purpose

The primary purpose of Firebase Admin Tools is to simplify and enhance the administration of Firebase features for developers. It abstracts the complexities of managing multiple Firebase services into an intuitive interface, allowing developers to perform essential operations such as user authentication, data storage, and real-time database management with ease.

Benefits

Usage Scenarios

Firebase Admin Tools is ideal for developers working on projects that require:

1. Authentication Management

2. Storage Solutions

3. Real-Time Database Operations

By leveraging Firebase Admin Tools, developers can efficiently manage their Firebase infrastructure, ensuring optimal performance, security, and scalability for their applications.

Authentication Management

This module provides tools for managing user authentication across multiple providers, including email/password, OAuth (Google, Facebook, etc.), and custom token systems. It supports user creation, deletion, updates, and bulk operations while maintaining secure authentication practices.

Storage Control

The module offers comprehensive control over Firebase Cloud Storage, enabling developers to manage file uploads, downloads, deletions, and folder organization. It also includes features for setting storage limits, access controls, and lifecycle management to ensure efficient and secure data handling.

Real-Time Database Operations

This feature allows developers to perform CRUD (Create, Read, Update, Delete) operations on Firebase’s real-time database. It supports real-time data synchronization, transaction management, and querying with Firebase Query Language (FQL), ensuring consistent and reliable data access.

Monitoring & Analytics

The module integrates monitoring tools to track the health and performance of Firebase services, such as authentication rates, storage usage, and database query latencies. It also provides analytics for user behavior and app metrics, helping developers optimize their application’s performance.

Security & Compliance

Firebase Admin Tools includes built-in security features like role-based access control (RBAC), audit logs, and encryption to protect sensitive data. It ensures compliance with industry standards and regulations, providing peace of mind for developers managing critical applications.

Firebase Admin Tools Documentation

1. FastAPI Endpoint Example (User Authentication)

This endpoint uses the Firebase Admin SDK to create a new user in your Firebase application.

#firebase_admin_tools.py
from fastapi import APIRouter, Depends, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
import firebase_admin
from firebase_admin.auth import (
    Auth,
    UserRecord,
)
import os

router = APIRouter()
security = HTTPBearer()
firebase Admin instance = firebase_admin.initialize_app()

class CreateUser(BaseModel):
    email: str
    password: str
    role: str
    display_name: Optional[str] = None

async def get_auth_credentials(credentials: HTTPAuthorizationCredentials = Depends(security)):
    if not credentials:
        raise HTTPException(status_code=401, detail="Invalid or missing credentials")
    # Validate credentials and return Firebase Admin instance
    return firebase_admin.get_app()

@router.post("/api/create-user", dependencies=[Depends(get_auth_credentials)])
async def create_user(user: CreateUser):
    try:
        user_record = UserRecord.create(
            firebase Admin instance,
            {
                "email": user.email,
                "password": user.password,
                "role": user.role,
                "display_name": user.display_name
            }
        )
        return {"message": "User created successfully", "uid": user_record.uid}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

2. React UI Example (User Management)

This React component demonstrates a simple UI for creating new users.

"use client";
import { useState } from "react";
import { createUserWithEmailAndPassword } from "@firebase/auth";

export default function CreateUserForm() {
    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");
    const [role, setRole] = useState("user");
    const [error, setError] = useState(null);
    const [loading, setLoading] = useState(false);

    const handleSubmit = async (e) => {
        e.preventDefault();
        try {
            setLoading(true);
            setError(null);
            await createUserWithEmailAndPassword(firebaseAuth(), email, password).then((userCredential) => {
                // Additional logic for setting role and other attributes
                console.log("User created with uid:", userCredential.user.uid);
            });
        } catch (err) {
            setError(err.message);
        } finally {
            setLoading(false);
        }
    };

    return (
        <form onSubmit={handleSubmit} className="max-w-md mx-auto p-6">
            <div className="mb-4">
                <label className="block text-sm font-medium text-gray-700">Email</label>
                <input
                    type="email"
                    value={email}
                    onChange={(e) => setEmail(e.target.value)}
                    className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500"
                />
            </div>
            <div className="mb-4">
                <label className="block text-sm font-medium text-gray-700">Password</label>
                <input
                    type="password"
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                    className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500"
                />
            </div>
            <div className="mb-4">
                <label className="block text-sm font-medium text-gray-700">Role</label>
                <select
                    value={role}
                    onChange={(e) => setRole(e.target.value)}
                    className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500"
                >
                    <option value="user">User</option>
                    <option value="admin">Admin</option>
                </select>
            </div>
            {error && (
                <div className="mb-4 p-3 bg-red-100 text-red-700 rounded-md">
                    {error}
                </div>
            )}
            <button
                type="submit"
                disabled={loading}
                className={`w-full py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white ${loading ? "bg-gray-300" : "bg-blue-600 hover:bg-blue-700"}`}
            >
                {loading ? "Creating User..." : "Create User"}
            </button>
        </form>
    );
}

3. Pydantic Data Schema (User Model)

This schema defines the structure for a user in your Firebase application.

# schemas.py
from pydantic import BaseModel
from typing import Optional

class CreateUser(BaseModel):
    email: str
    password: str
    role: str
    display_name: Optional[str] = None
    photo_url: Optional[str] = None
    phone_number: Optional[str] = None
    disabled: Optional[bool] = False

    class Config:
        orm_mode = True

Usage Examples

FastAPI Endpoint Usage:

curl -X POST \
  http://localhost:8000/api/create-user \
  -H "Authorization: Bearer YOUR_FIREBASE_ADMIN_SDK_KEY" \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"securepassword123","role":"user"}'

React UI Usage:

import CreateUserForm from "./CreateUserForm";

function App() {
    return (
        <div className="min-h-screen bg-gray-50">
            <main className="container mx-auto px-4 py-8">
                <h1 className="text-2xl font-bold mb-6">Create New User</h1>
                <CreateUserForm />
            </main>
        </div>
    );
}

This documentation provides a comprehensive guide for integrating Firebase Admin Tools into your application using FastAPI, React, and Pydantic.

Firebase Admin Tools Documentation

Module Overview

The Firebase Admin Tools module provides an interface to manage authentication, storage, and real-time database operations in your application. This module is designed for developers who need to interact with Firebase services programmatically.


  1. Firebase Authentication: Manages user authentication across various identity providers.
  2. Firebase Real-time Database: Enables syncing data between client and server in real-time.
  3. Firebase Cloud Functions: Server-side code that responds to events from Firebase services or other Google Cloud resources.

Use Cases

1. User Management

2. Data Synchronization

3. Storage Bucket Operations


Integration Tips

  1. Environment Variables: Ensure all necessary environment variables (e.g., FIREBASE_API_KEY) are set before initializing the module.
  2. Error Handling: Implement proper error handling to catch exceptions like authentication failures or network issues.
  3. Security Practices: Use service accounts for authentication and avoid hardcoding sensitive information.

Configuration Options

Option NameTypeDescription
apiKeystringThe API key for Firebase Authentication.
projectIdstringYour Firebase project ID.
databaseURLstringThe URL of your Firebase real-time database instance.
storageBucketstringThe name of your Firebase storage bucket.
authDomainstringThe domain for authentication, typically your-project-id.firebaseapp.com.
messagingSenderIdstringThe sender ID used for Firebase Cloud Messaging.

Conclusion

The Firebase Admin Tools module offers a powerful interface to manage various Firebase services. By leveraging its features and following the provided tips, developers can efficiently integrate and maintain their applications using Firebase services.