> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portalhq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Firebase Auth Backup

> Use your existing Firebase Authentication to back up and recover Portal wallets via a secure enclave.

Allow customers to use their existing Firebase Authentication to authenticate into a secure enclave that holds the encryption key for the user. The Portal SDK leverages Firebase ID tokens to securely store and retrieve encryption keys from the secure enclave.

## How it works

1. Your app signs the user in with Firebase Auth (Email/Password, Google Sign-In, Phone, or any other Firebase Auth provider).
2. You configure the Portal SDK with a callback that returns a fresh Firebase ID token.
3. During **backup**, the SDK encrypts the client backup share and stores the encryption key in the secure enclave, authenticated by the Firebase ID token.
4. During **recovery**, the SDK retrieves the encryption key from the secure enclave using the same authentication, then decrypts the backup share to restore the wallet.

## Prerequisites

1. A Firebase project with Firebase Authentication enabled.
2. At least one sign-in provider configured in Firebase Auth (Email/Password, Google Sign-In, Apple Sign-In, etc.).
3. Your Firebase project registered with Portal (see below).

## Register your Firebase project with Portal

You must register your Firebase project with Portal so that the secure enclave can validate your users' Firebase ID tokens.

1. Go to the [Portal Dashboard](https://app.portalhq.io).
2. Navigate to **Configuration > Webhooks**.
3. In the **Firebase Backup Authentication** section, enter your **Firebase Project ID** and save.

<Note>
  You can also register the JWT provider programmatically via the Custodian API:

  ```bash theme={null}
  curl -X POST https://api.portalhq.io/api/v3/custodians/me/jwt-provider \
    -H "Authorization: Bearer YOUR_CUSTODIAN_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "provider": "firebase",
      "projectId": "your-firebase-project-id"
    }'
  ```
</Note>

## Add Firebase to your app

<Tabs>
  <Tab title="iOS">
    If you have not already set up Firebase in your iOS app, follow the [Firebase iOS setup guide](https://firebase.google.com/docs/ios/setup). At a minimum, you need to:

    1. Add your iOS app in the [Firebase Console](https://console.firebase.google.com).
    2. Download the `GoogleService-Info.plist` file and add it to your Xcode project.
    3. Add the `FirebaseAuth` dependency via Swift Package Manager or CocoaPods.
    4. Call `FirebaseApp.configure()` in your `AppDelegate` or app entry point.
  </Tab>

  <Tab title="Android">
    If you have not already set up Firebase in your Android app, follow the [Firebase Android setup guide](https://firebase.google.com/docs/android/setup). At a minimum, you need to:

    1. Add your Android app in the [Firebase Console](https://console.firebase.google.com).
    2. Download the `google-services.json` file and add it to your `app/` directory.
    3. Add the Firebase dependencies to your `build.gradle`:

    ```groovy theme={null}
    dependencies {
        implementation platform("com.google.firebase:firebase-bom:33.1.0")
        implementation "com.google.firebase:firebase-auth-ktx"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.3"
    }
    ```

    4. Apply the Google services plugin:

    ```groovy theme={null}
    // In your project-level build.gradle
    plugins {
        id "com.google.gms.google-services" version "4.4.2" apply false
    }

    // In your app-level build.gradle
    plugins {
        id "com.google.gms.google-services"
    }
    ```
  </Tab>

  <Tab title="Flutter">
    If you have not already set up Firebase in your Flutter app, follow the [FlutterFire setup guide](https://firebase.google.com/docs/flutter/setup). At a minimum, you need to:

    1. Install the FlutterFire CLI: `dart pub global activate flutterfire_cli`
    2. Run `flutterfire configure` to connect your Flutter app to your Firebase project.
    3. Add the `firebase_core` and `firebase_auth` packages to your `pubspec.yaml`:

    ```yaml theme={null}
    dependencies:
      firebase_core: ^3.0.0
      firebase_auth: ^5.0.0
      portal_flutter: ^X.X.X
    ```

    4. Initialize Firebase in your app entry point:

    ```dart theme={null}
    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(MyApp());
    }
    ```
  </Tab>

  <Tab title="Web">
    If you have not already set up Firebase in your web app, follow the [Firebase Web setup guide](https://firebase.google.com/docs/web/setup). At a minimum, you need to:

    1. Add a Web app in the [Firebase Console](https://console.firebase.google.com) and copy your Firebase config (`apiKey`, `authDomain`, `projectId`, etc.).
    2. Install the Firebase JavaScript SDK:

    ```bash theme={null}
    npm install firebase
    ```

    3. Initialize Firebase in your app (for example in your root layout or entry module):

    ```typescript theme={null}
    import { initializeApp, getApps } from 'firebase/app'
    import { getAuth } from 'firebase/auth'

    const firebaseConfig = {
      apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
      authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
      projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
    }

    const app = getApps().length ? getApps()[0]! : initializeApp(firebaseConfig)
    export const firebaseAuth = getAuth(app)
    ```

    4. Sign users in with any Firebase Auth provider your app supports (Email/Password, Google, etc.) before calling Portal backup or recovery with `BackupMethods.firebase`.
  </Tab>
</Tabs>

## Supported platforms

Firebase Auth Backup is currently available on:

* **iOS** - See the [iOS Back up a wallet guide](/sdks/ios/guide/back-up-a-wallet#firebase-auth-backup)
* **Android** - See the [Android Back up a wallet guide](/sdks/android/guide/back-up-a-wallet#firebase-auth-backup)
* **Flutter** - See the [Flutter Back up a wallet guide](/sdks/flutter/guide/back-up-a-wallet#firebase-auth-backup)
* **React Native** - See the [React Native Back up a wallet guide](/sdks/react-native/guide/back-up-a-wallet#firebase-auth-backup)
* **Web** - See the [Web Back up a wallet guide](/sdks/web/guide/back-up-a-wallet#firebase-auth-backup)
