Firebase Admin Node.js SDK

repository·main·Indexed 23 days ago

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

Privileged server-side SDK for Node.js (version 14.2.0) that provides secure back-end access to Firebase services, including Authentication, Databases, Cloud Messaging, and App Check. It supports Node.js 22 and higher and includes utilities for managing multiple app instances, service account authentication via cert() or applicationDefault(), and re-exported types for Firestore and Realtime Database.

Tokens
33.2K
Snippets
63
Records
235
Agent score
82%

What's inside firebase-admin

  1. Supported environments for Firebase Admin Node.js SDK

    main

    The SDK currently supports Node.js 22 and higher.

    Security Warning: The Admin SDK should only be used in server-side or back-end environments controlled by the app developer (e.g., servers, serverless platforms, or cloud environments). Do not use the Admin SDK in client-side environments.

  2. Evaluate Remote Config on a server using ServerTemplate

    main

    When you need to evaluate Remote Config parameters on a server based on specific user context, use ServerTemplate.

    1. Use initServerTemplate(options) to create a template instance.
    2. Use load() to fetch the template data.
    3. Use evaluate(context) to get a ServerConfig object tailored to the provided EvaluationContext (which includes UserProvidedSignals and PredefinedSignals).
    4. Access values via ServerConfig methods like getString(key), getNumber(key), or getBoolean(key).
    const serverTemplate = remoteConfig.initServerTemplate();
    await serverTemplate.load();
    
    const context = { myCustomSignal: 'value' }; // UserProvidedSignals
    const config = serverTemplate.evaluate(context);
    const myValue = config.getString('my_parameter_key');
  3. Configure task scheduling with DeliverySchedule

    main

    When enqueuing a task, you can control when it runs using the DeliverySchedule type. You must choose between one of two mutually exclusive modes:

    1. DelayDelivery: Uses scheduleDelaySeconds (a number) to specify how many seconds to wait before execution.
    2. AbsoluteDelivery: Uses scheduleTime (a Date) to specify a specific point in time for execution.

    Note: You cannot provide both scheduleDelaySeconds and scheduleTime in the same options object.

  4. Impersonate users in Data Connect operations

    main

    When executing operations, you can use the impersonate option within GraphqlOptions or OperationOptions to simulate different authentication states. This is useful for testing security rules or performing actions on behalf of a user.

    • ImpersonateAuthenticated: Provide authClaims (a partial DecodedIdToken) to simulate a logged-in user.
    • ImpersonateUnauthenticated: Set unauthenticated: true to simulate an unauthenticated request.
  5. Manage Firebase Security Rules with the SecurityRules service

    main

    The SecurityRules service allows you to programmatically manage security rules for Cloud Firestore and Cloud Storage. You can create new rulesets from source code, apply (release) them to your services, retrieve existing rulesets, and list metadata for all rulesets in your project.

    To use this service, access it via admin.securityRules() after initializing your Firebase Admin app.

  6. Handle RequestResponse and RequestResponseError

    main

    When using HttpClient.send(), the result is a Promise<RequestResponse>.

    • Successful responses (2xx): Resolve with a RequestResponse object. You can access status, headers, text, and data (if the response is JSON). Use isJson() to check if the data field is available.
    • Server errors (3xx, 4xx, 5xx): Reject with a RequestResponseError. This error contains the response object, allowing you to inspect the status and headers returned by the server.
    • Network/Internal errors: Reject with a FirebaseAppError (e.g., NETWORK_TIMEOUT or NETWORK_ERROR).
  7. Configure Multi-Factor Authentication (MFA) for users

    main

    When creating or updating users, you can manage their enrolled second factors using the multiFactor property in CreateRequest or UpdateRequest objects.

    Creating a user with MFA

    Use MultiFactorCreateSettings to specify a list of CreateMultiFactorInfoRequest objects. Currently, only phone is supported.

    Updating a user's MFA

    Use MultiFactorUpdateSettings to manage enrolled factors.

    • Providing a list of UpdateMultiFactorInfoRequest objects will overwrite the user's existing list.
    • Passing null to enrolledFactors will remove all existing second factors for that user.
  8. Use Percent conditions for user targeting

    main

    Percent conditions allow you to target a specific portion of your user base. You must specify a percentOperator and the corresponding limit or range.

    Operators:

    • LESS_OR_EQUAL: Targets percentiles less than or equal to microPercent.
    • GREATER_THAN: Targets percentiles greater than microPercent.
    • BETWEEN: Targets percentiles within the microPercentRange (lower bound is exclusive, upper bound is inclusive).

    Important Notes:

    • microPercent values must be in the range [0 and 100000000].
    • seed: A case-sensitive string (0-32 ASCII characters) used to ensure consistent hashing for the user group.