# Phone Login with Firebase

This guide covers how to set up a login page in Bravo using Firebase as the user management service.  where users introduce their phone number, and get a SMS with a code they need to use to log in.

{% hint style="info" %}
**What is Firebase?** [Firebase](https://firebase.google.com/) is a backend-as-a-service (Baas) from Google that provides features for building mobile apps.
{% endhint %}

## 1. Setting up the UI

#### 💾 Sample design file

Here's a sample design file that already contains all the necessary tags for this phone login feature. It contains the tags phone login and also email login. They can also be combined with [Firebase's Social Login](https://docs.bravostudio.app/integrations/user-authentication/app-login-firebase/social-login-with-firebase).

{% embed url="<https://www.figma.com/community/file/1119178635735720047>" %}
Sample design for email and phone login
{% endembed %}

{% hint style="success" %}
Make sure to place the [**flow start point**](https://docs.bravostudio.app/set-up-design/setting-up-your-design-file/app-navigation/setting-up-app-navigation-on-figma) **(Figma) or Home icon (XD)** on the "Home" screen of your app (i.e. the first screen users will see after they have logged in).
{% endhint %}

### 1.1. Phone number page

In order to set up the phone login, use the tags below:

1. <mark style="color:red;">`[component:input-tel]`</mark>: Indicates which field will be used as the user phone input. It needs to be a valid phone number, or a test phone number provided in Firebase.
2. <mark style="color:red;">`[action:login:firebase-phone]`</mark>: performs the login action, needs to be added in the login button.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F9yunNbI65tldoII1USwn%2Fimage.png?alt=media\&token=b7e89106-e48a-411d-b648-0355f5a42ac6)

In this Bravo Sample, we use other Firebase login options, if you're using only the phone option as login, add the <mark style="color:red;">`[page:login]`</mark> tag to the `login-phone` screen.

After the login screen, the user will navigate to a screen where they need to input a confirmation code sent by email.&#x20;

### 1.2. Confirmation code page (phone login - mandatory)

You'll need to create a page where the user will introduce **a confirmation code received via SMS**. The user will automatically navigate to this page after the login screen.

To set it up, add the following tags:

* <mark style="color:red;">`[page:login:verify-phone]`</mark>: add this tag in the **page-level component.**
* <mark style="color:red;">`[component:input-number]`</mark>: This will create a numeric input field for the confirmation code.
* <mark style="color:red;">`[action:verify-phone:firebase]`</mark>: this action sends the confirmation code. If the code is correct, the user will navigate to the home page. Otherwise, an error message will be displayed, and the user will need to log in again.

![Figma with a Bravorized phone login screen ](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FrC3AVVlnUuKAVWoDeEkS%2Fimage.png?alt=media\&token=39dcf10a-38b6-4b47-b984-03c36bbf0ba7)

{% hint style="warning" %}
For phone and social logins, **there's no signup page** - the login action will also create new accounts for first-time users.
{% endhint %}

{% hint style="info" %}
Please note that phone logins are functional exclusively on the **final versions of apps**, either in the testing phase or when officially released. In Bravo Vision, you can engage in a simulation.
{% endhint %}

## 2. Set up in Firebase

Once you have the UI ready, it's time to manage user authentication in your existing Firebase project. If you haven't created your Firebase project yet, follow the steps provided [here](https://docs.bravostudio.app/integrations/user-authentication/app-login-firebase/..#create-your-firebase-project).

### 2.1. Enable Type of Authentication

1. After the project is created, click **on Project Overview** from the left menu and then select **Authentication**. Here is where your user accounts will be created and stored.&#x20;
2. Go to **Sign-in method** tab and **select Phone** from the Native provider options.
3. **Enable the phone toggle**. As you'll see, you can add here some test phone numbers to log in - you'll need to provide a test confirmation code as well, that you'll be able to introduce in the login flow.<br>

   <figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2Fezy1nIFUpJEEjocz0oV8%2FScreenshot%202023-12-12%20at%2011.48.44.png?alt=media&#x26;token=14b46a71-7fe9-4a7b-8cb5-86295876e31a" alt=""><figcaption></figcaption></figure>
4. Enable **Anonymous** login and do not forget to save!

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FOpoHrbJPJljdIZ6yrxKp%2FScreenshot%202024-05-27%20at%2011.55.40.png?alt=media&#x26;token=e3508a48-fa18-405c-854e-ed35e73a7e2a" alt=""><figcaption></figcaption></figure>

## **3. Firebase Configuration in Bravo**

Now, you need to obtain the Firebase configuration files (**GoogleService-Info.plist for iOS** and **google-services.json for Android)** and upload them to Bravo.

The purpose of this configuration file is to **create a link between our app and this Firebase project**.  So, if in the future, you want to connect to another Firebase project, you can do it by uploading a new Firebase configuration file in Bravo for each mobile platform (iOS and Android).&#x20;

Depending on your needs, you can either:

* Set up your Firebase Login for Bravo Vision (recommended for prototyping and testing)
* Set up your Firebase Login for your live or published app (recommended for publishers)

In case you want to configure both Bravo Vision and live app for iOS and Android, you will need 4 files in total:

1. **GoogleService-Info.plist for iOS Bravo Vision**
2. **google-services.json for Android Bravo Vision**
3. **GoogleService-Info.plist for iOS published app**
4. **google-services.json for Android published app**

### **3.1. Firebase Services for Bravo Vision**

Here are the steps to follow if you want to get the configuration files for Bravo Vision:

#### 3.1.1.  Create an app and download the Config files for Bravo Vision

If you don't have any app created in Firebase yet, follow the next steps as **you need to create new Firebase configuration** **files** to upload them to Bravo Studio.&#x20;

1. Scroll down to **Your apps** section and click **Add app**.
2. **Choose a platform** (iOS or Android) and do the following depending on the platform:

{% tabs %}
{% tab title="IOS" %}

1. Go to **Project Overview** (from the left menu), click the **iOS symbol.**

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZe-lalbD1_cPadK%2Ffirebase8.gif?generation=1629274918818351\&alt=media)

2. In step 1, fill in the **iOS Bundle ID** for Bravo Vision: <mark style="color:red;">`com.codelesslabs.app`</mark> Leave the rest blank, and click **Register app**.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZe4PMCTqr0fNRny%2FScreenshot_2020-10-08_at_16.24.49.png?generation=1629274918592048\&alt=media)

2. In step 2, **download** the file **GoogleService-Info.plist** and click, **Next** to continu&#x65;**.**

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZdvKvKR7p7p2aJq%2Ffirebase11.png?generation=1629274918518820\&alt=media)

3. The rest of the steps are not needed, so click **Next** for Steps 3 and 4.&#x20;
4. To finish, in **Step 5**, click **Continue to the console.**
5. To verify that the app has been created, go to your **Project Overview > General > Your apps.**
   {% endtab %}

{% tab title="Android" %}

1. Go to **Project Overview** (from the left menu), click the **Android symbol** or **Add an app.**
2. In step 1, fill in the **Android package name** for Bravo Vision: <mark style="color:red;">`com.appfoundry.previewer`</mark> Leave the rest blank, and click **`Register app`**.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZe57jRIQKWrNGdW%2FScreenshot_2020-10-08_at_16.45.06.png?generation=1629274918605131\&alt=media)

3. In step 2, **download** the file **google-services.json.**

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZdxXXqMA4FHjsso%2Ffirebase14.png?generation=1629274918526621\&alt=media)

3. Click **Next**. The rest of the steps are not needed, so click `Next` for steps 3. In the final step, click **Skip this step** (or **Continue to console** if it's enabled).

**Important:** In case you want to set up **phone login for Android**, there are some security steps you need to complete in Firebase:

1. You'll need to **request an app bundle** (the easiest will be to request a debug APK), in **Bravo's** **Publish** **section**. Even if you don't want to publish your app yet, you'll need this to obtain a **valid SHA-256 code**.
2. To obtain this code, go to the **History section** after your bundle is generated, and copy the SHA-256 code.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FylpFSsmHPJHWpMNjyMJy%2Fimage.png?alt=media&#x26;token=efd35d68-d7f8-43c1-bc21-ff5c523972df" alt=""><figcaption></figcaption></figure>

3. Once this is done, go back to your **Firebase project**. In the project settings, **paste this code** in the section shown below, inside the Android app you previously created and download the **google-services.json**.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2Fr6u3EmLGtxpoYZ7JfUrp%2Fimage.png?alt=media\&token=d789b560-f449-4881-9e8b-0cf7660cc613)

4. Finally, you'll need to set up the **SafetyNet** option. To do that, go to **Build** -> **App Check** in the side menu. Then, select SafetyNet, enable the checkbox (the SHA code should already be there), and click **Save**.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FWqvvIexhchaLY4c6JFky%2Fimage.png?alt=media\&token=03f7ea50-8607-4e0b-ac57-5d8ab0a12c27)
{% endtab %}
{% endtabs %}

#### 3.1.2 Upload the Firebase Config files in Bravo

1. Once having the Firebase configuration files, **go back to Bravo Studio, open the App project** that has the login (or import your design file if you haven't yet) and click on the **Integrations** ta&#x62;**.**
2. Navigate to the **Login** option in the left-hand sidebar and **Enable the Firebase toggle**.
3. Once enabled, the Bravo Vision tab will be selected by default. Click on the **Configure** button to upload the Firebase files in the corresponding fields. It is also possible to do so from **Firebase Config.** <br>

   <figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FkfnKp2dvdLc2CznRO28x%2FScreenshot%202023-12-07%20at%2010.52.23.png?alt=media&#x26;token=d42e53fa-9f8f-4f84-8a27-69352b92aae5" alt=""><figcaption><p>Enable Firebase Login from the App's Integrations tab</p></figcaption></figure>

   <figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FuLA4nXKT4iYwrY3J2Qxh%2FScreenshot%202023-12-07%20at%2011.14.43.png?alt=media&#x26;token=873e7ca9-c95c-4538-bacd-5030307e7438" alt=""><figcaption><p>Upload the Firebase files from Firebase Config</p></figcaption></figure>
4. After uploading the files, do not forget to **Save** the changes. You must have a green check in order for that integration to work correctly on that platform you're setting up. If you're not setting up Firebase in a certain platform, you can ignore the warning icon.<br>

   <figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FtVEKwNI2EcEGpdUO5kFo%2FScreenshot%202023-12-07%20at%2011.26.18.png?alt=media&#x26;token=6f410982-f6d4-452c-8499-d8915d54085d" alt=""><figcaption><p>Completed Firebase Config files for Android and IOS</p></figcaption></figure>
5. Your app is ready to test! Open **Bravo Vision** to test out the login pages.

{% hint style="info" %}
On Bravo Vision, every time the app is opened, login will be required. This won't happen on the published app, where the session will be maintained until the user chooses to log out.
{% endhint %}

{% hint style="info" %}
After the user has logged in, a special [variable](https://docs.bravostudio.app/connect-api/request-url-variables#built-in-variables) will be created: <mark style="color:red;">`${user.id}`</mark>. This built-in variable can be sent as part of an API request, to handle user-specific data in the database.
{% endhint %}

### 3.2 Firebase Services for published app

Once you are ready to publish your app to the stores, **you need to create new Firebase configuration** files with the final bundle ID (for iOS) and package name (for Android) and upload them to Bravo Studio.

![Activate Firebase services in your final app](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZdpKH4f4WfIGHe2%2Ffirebase-bravo4.png?generation=1629274918528211\&alt=media)

#### 3.2.1. Create an app and download the Config files for the live app

If you don't have any app created in Firebase yet, follow the next steps as **you need to create new Firebase configuration** files with the final bundle ID (for iOS) and package name (for Android) and upload them to Bravo Studio. Otherwise, add the missing params we mention:

1. Scroll down to **Your apps** section and click **Add app**.
2. Select a platform: iOS or Android.
3. Register the app and do the following depending on the platform:

{% tabs %}
{% tab title="iOS" %}

1. Enter your <mark style="color:red;">**`Bundle ID`**</mark> and <mark style="color:red;">**`App Store ID`**</mark>. Remember you need to complete [step 2 of the publishing process](https://docs.bravostudio.app/app-publication/publishing-your-app/ios-publication-complete-process/generate-certificates-appids-and-provisions) to have these parameters ready.

**Where do I find the final Bundle ID (for iOS)?**

When you have the necessary files for publication, use the same **Bundle ID** from the Bravo publication form for the final Firebase file. (Check how to [set up the Bundle ID](https://docs.bravostudio.app/app-publication/publishing-your-app/ios-publication-complete-process) on the app publication process)

![Register iOS app with iOS bundle ID](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhNKqDur4tQGJPUgzbJ%2F-MhNLZdoVqpOMWLn5ido%2Ffirebase-bravo3.png?generation=1629274918607348\&alt=media)

For iOS, you'll need to set up **Cloud Messaging** in Firebase. This is found under **Project Settings** > **Cloud Messaging** > **Apple App configuration**.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FYUFX121pvAwgfutEq1KU%2Fimage.png?alt=media\&token=4b76bb99-6eed-417e-b975-ef97fd3f6dc7)

Cloud messaging allows to be configured using an APNs Authentication Key or using APNs Certificates. Using authentication key is recommended, and also easier to set up.

To do this, inside the Developer Central, go to Keys and create a new one, of type Apple Push Notifications service (APNs):

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F9vE2Qn3IjthKZqOX5e0q%2Fimage.png?alt=media&#x26;token=4ff2988a-9641-494e-b858-4e3fb6cf6d34" alt=""><figcaption></figcaption></figure>

Fill the KeyID, TeamID and upload the p8 file in Firebase settings, and that's it. The TeamID is displayed in the Membership page.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FPMEJ8OTcTtJQbGGbl82h%2Fimage.png?alt=media&#x26;token=7969e7e3-9aa4-4a1c-9d31-24035031a375" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Android" %}
1\. **Where do I find the final package name (for Android)?**

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F3oFUYgn10vYI3QuUB3pf%2Fimage.png?alt=media&#x26;token=a7912cef-5909-451e-9894-0475bd5fff12" alt=""><figcaption></figcaption></figure>

4\. In case you're using phone login, remember that you'll need to provide a SHA code for an app bundle in the Firebase settings, even if you'll only test in Bravo Vision for now. You can see [here](#android) how to do that. This SHA can be added in Firebase after downloading the google-services.json.

In case you're using phone login, there are some additional points to consider.

**Android**

Once you generate your debug APK or AAB bundle, you'll need **to add its SHA-256 code to the Firebase settings** as indicated [here](#android). Even if you did this before to test the app in Bravo Vision, you'll need to do it again for each app bundle you create and want to test (you can add many SHA codes to the same Firebase project).&#x20;

So, for instance, in case you decide to have both a debug APK and a publishable AAB, you'll need to add the two SHA codes for those bundles to the Firebase project settings. Remember that you can add the SHA codes **after** downloading the corresponding google-services.json file - you **don't need to generate a new one** for every new SHA code.

After doing this, your app bundle will have a functional phone login in place! 🥳
{% endtab %}
{% endtabs %}

#### **3.2.2.** Upload the Firebase Config files in Bravo

1. Once having the Firebase configuration files, **go back to Bravo Studio,** select the **App > Integrations tab > Firebase Config.**

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FluWn05K0kuqefk4bIZsI%2FScreenshot%202023-12-12%20at%2011.40.35.png?alt=media&#x26;token=8680c29d-8337-48e9-90e0-3b1ec398ed65" alt=""><figcaption></figcaption></figure>

2. Under **"Activate Firebase services for final app",** upload the Firebase files in the corresponding fields and do not forget to **Save** the changes.&#x20;
3. Firebase integration has been updated in this app. Please request a new build and upload a new version to ensure your published app continues to work properly.

{% hint style="warning" %}
In case you already have a published app bundle, and you change these Firebase assets, you'll need to generate a new bundle and publish an update to the stores.
{% endhint %}

Once completed, your app's login is ready to test! :tada: Open **Bravo Vision** or your **Published app** to test out the login pages.
