Firebase Admin Go SDK

repository·master·Indexed 23 days ago

https://github.com/firebase/firebase-admin-go

The Firebase Admin Go SDK allows Go applications running in privileged environments, such as servers and cloud functions, to interact with Firebase services. It provides clients for Auth, Database, Firestore, Storage, Messaging, App Check, Remote Config, Instance ID, and Phone Number Verification.

Tokens
906
Snippets
1
Records
6
Agent score
30%

What's inside firebase-admin-go

  1. Overview of the Firebase Admin Go SDK

    master
    The Firebase Admin Go SDK allows you to access Firebase services from privileged environments (such as servers or cloud functions) using the Go programming language. Currently, the SDK provides support for Firebase custom authentication.
  2. Install the Firebase Admin Go SDK

    master

    You can install the Firebase Admin Go SDK using the go get utility. You can either install the latest version or target a specific version using the v4 module path.

    # Install the latest version:
    go get firebase.google.com/go/v4@latest
    
    # Or install a specific version:
    go get firebase.google.com/go/v4@4.x.x
  3. Configure the Firebase App using Config

    master

    The Config struct defines the settings used to initialize an App. Use these fields to specify your project details and service endpoints.

    FieldTypeJSON KeyDescription
    ProjectIDstringprojectIdThe unique identifier for your Firebase project.
    DatabaseURLstringdatabaseURLThe URL of your Firebase Realtime Database.
    StorageBucketstringstorageBucketThe name of your Cloud Storage bucket.
    ServiceAccountIDstringserviceAccountIdThe ID of the service account to use.
    AuthOverride*map[string]interface{}databaseAuthVariableOverrideA map used to override database authentication variables.
  4. Initialize a Firebase App with NewApp

    master

    The NewApp function is the primary way to create an App instance, which serves as the central entity for accessing all Firebase services.

    Authentication:

    • If opts contains valid credentials (service account file, refresh token, or OAuth2 token source), the App uses them.
    • Otherwise, it attempts to use Google Application Default Credentials (ADC).

    Configuration:

    • If config is provided, it uses the values in the Config struct.
    • If config is nil, the SDK attempts to load configuration from the FIREBASE_CONFIG environment variable. This variable can be a JSON string or a path to a JSON file.

    Project ID Resolution: If config.ProjectID is not set, the SDK resolves the Project ID by checking:

    1. The credentials provided in opts.
    2. The GOOGLE_CLOUD_PROJECT environment variable.
    3. The GCLOUD_PROJECT environment variable.
  5. Access Firebase services from an App instance

    master

    Once an App is initialized, you can access various Firebase service clients using methods on the App struct. Most methods require a context.Context and return a client and an error.

    Available Service Clients:

    • Auth(ctx): Returns an *auth.Client for managing users and authentication.
    • Database(ctx): Returns a *db.Client for the default Firebase Realtime Database.
    • DatabaseWithURL(ctx, url): Returns a *db.Client for a specific database URL.
    • Firestore(ctx): Returns a *firestore.Client for the default Firestore database.
    • FirestoreWithDatabaseID(ctx, databaseID): Returns a *firestore.Client for a specific named Firestore database.
    • Storage(ctx): Returns a *storage.Client for Cloud Storage.
    • Messaging(ctx): Returns a *messaging.Client for Firebase Cloud Messaging.
    • AppCheck(ctx): Returns an *appcheck.Client for App Check.
    • RemoteConfig(ctx): Returns an *remoteconfig.Client for Remote Config.
    • InstanceID(ctx): Returns an *iid.Client for Instance ID.
    • PhoneNumberVerification(ctx): Returns a *phonenumberverification.Client for phone number verification.