# My Notes: using user ID and email from Firebase

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-CbwTNYJnEbyL3p%2FNotes_Visual.gif?generation=1629447259816318\&alt=media)

In this tutorial, you'll learn how to set the [Bravo variables](https://docs.bravostudio.app/connect-api/request-url-variables#bravo-variables) `user.id` and `user.email`. The values of these variables are taken from an authentication provider, so the app must authenticate the users first in order to get these values.

To showcase this, we'll create a sample notes app. In this app, we'll set up an authentication flow and connect it to Firebase. Then, we'll use Airtable to store the user-related data. In order to show each user only their notes, we'll use the `user.id` value when storing a new note in the table. Finally, we'll set up a personalised message, where the user will see their own email as part of a greeting message.

{% hint style="info" %}
For this personalised message, another [Bravo variable](https://docs.bravostudio.app/connect-api/request-url-variables#bravo-variables) (`user.name`) could be used. Currently, this is not possible with Firebase, as it doesn't fill the value for this variable. When new authentication providers are integrated with Bravo, this variable will also have a value.
{% endhint %}

## ✨ Features

* User authentication with Firebase.
* Display a welcome message for the user with their email address.
* View, add, and remove notes, leveraging the user.id variable to show each user only their notes.

## 💎 Resources

* **Design file: Figma**

{% embed url="<https://www.figma.com/embed?embed_host=notion&url=https%3A%2F%2Fwww.figma.com%2Ffile%2FLlISfOw3NWPdExqAcnWlg5%2FUser-Variables%3Fnode-id%3D0%253A1>" %}

* **Data source: Airtable**

  Open the Airtable and click `Copy Base` to duplicate to your account.

  **Includes**

  * Table setup for the notes, user id and user email.

{% embed url="<https://airtable.com/shrpihLWGUp69cQ2y>" %}

## 1️⃣ Set up authentication with Firebase

Once the design file has been imported into Bravo, follow [this tutorial](https://docs.bravostudio.app/integrations/user-authentication/app-login-firebase) in order to set up the authentication with **Firebase**.

## 2️⃣ Create the data collection and requests in the Data Library

Now, we'll create a Data Collection in the Data Library, to connect our API with Airtable, and to display the welcoming message with the `user.email` variable. We'll use the Airtable wizard when adding the API endpoint ~~tutorial~~ to speed things up - we'll need to provide the **Airtable base URL** and the **API Key** of our account.

Once the Data Collection has been created, we'll configure three API requests. These are detailed below.

{% hint style="info" %}
You can duplicate the requests generated by the Airtable tutorial in order to create new ones. This way, the Authentication value with the API key will be already copied in the request header.
{% endhint %}

### `GET` List Notes

In this request, we'll get the list of notes from Airtable. However, we don't get all the notes, but only the ones that are associated with a certain `user.id`. The value for this variable will be filled once the user is logged in, so each user will only be able to see their own notes.

In order to set up this request, follow these steps:

1. Set the following request URL

   ```jsx
    https://api.airtable.com/v0/**BASE_ID**/**TABLE_NAME**?filterByFormula={userid}='${user.id}'
   ```

   Note that the **BASE\_ID** and **TABLE\_NAME** values will be custom for your own Airtable base and table name. These are found in the request URL generated by the Airtable tutorial.
2. In the **Parameters** tab, set the parameter key `user.id` with the value `test_id` (this is the value we have in our Airtable base for the three sample notes). Then, hit `Send`. This will return the three sample notes.
3. In the **Received Data** tab, make sure all these data records below are selected. This will allow us to bind the data later in the screens.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-CitTWslHs2UkJS%2FUntitled.png?generation=1629447259896209\&alt=media)

### `GET` Get User Email

This will be a special request to be able to display the `user.email` variable in the welcoming message. As Bravo binds the data that comes from an API, we first need to send the `user.email` variable to an API service once the user is logged in, then fetch it from that API to display it on the screen. There's a service called [httpbin](https://httpbin.org/) that allows you to create a request URL with a certain parameter, that will trigger a response containing that same parameter. This way, we can send the user.email variable to the httpbin service, and fetch it again using the same GET request.

In order to set up this request, follow these steps:

1\. Set the following request URL

```jsx
https://httpbin.org/get?name=${user.email}
```

This will send the `user.email` variable to the **httpbin** service as a query parameter, and get that value back in the response body (received data).

2\. In the Parameters tab, set the parameter `user.email` with the value `user_test` (just a sample value, you can use any value you wish). Then, hit `Send`. We need to do this in order to configure the request for later use in our app.

3\. Finally, in the **Received Data** tab, select the `.data.args.name` *\*\**&#x70;arameter. You'll see here the value you set as a parameter before sending the request. This will be the parameter that we'll bind to the home screen later, to display the welcome message with the user email.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-Cexg9gIIf7f3bM%2FUntitled%201.png?generation=1629447259804305\&alt=media)

### `POST` Add Note

Finally, we'll set up a **POST** request to be able to add a note. In the request body, we'll add the note content and the `user.id` variables, respecting the structure that we created in Airtable.

In order to set up this request, follow these steps:

1\. Set the following request URL:

```jsx
 https://api.airtable.com/v0/**BASE_ID**/**TABLE_NAME**
```

2\. In the **Body** tab, select `JSON`, and copy the following JSON body. In this body, we defined the `noteContent` and `user.id` variables with the dollar sign and the brackets. The `noteContent` variable value will be fetched later from one of the screens, and the `user.id` will be automatically filled in by Bravo with the `user.id` value.

```jsx
 {
   "records": [
     {
       "fields": {
         "note": "${noteContent}",
         "userid": "${user.id}"
       }
     }
   ]
 }
```

{% hint style="warning" %}
It's very important to respect the naming that is set in Airtable. The "note" and "userid" key names are also the names of the colums in the Airtable base provided for this tutorial. If you change those names, you must change the key names in the JSON body as well. Otherwise, there will be an HTTP error when sending the request.
{% endhint %}

3\. Now, as we did for the previous requests, we need to set test values for the variables we defined for this request (`noteContent` and `user.id`). In the **Parameters** tab, set those test parameters to any value you wish.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-Cc2gyOt1SOH1AZ%2FScreenshot_2021-05-05_at_12.26.08.png?generation=1629447259754634\&alt=media)

Then, hit `Send`. You should see the test parameters you have set both in the Airtable base and in the **Received Data** tab. We'll not need any of those values that we get in the response (recall that we have set a **List Notes** request to display all the notes), you can just leave it as it is.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-CdTRr0S41hJ5Fa%2FScreenshot_2021-05-05_at_14.33.29.png?generation=1629447259746962\&alt=media)

## 3️⃣ Bind the screens to the data

Finally, we can bind the screens that we imported from Figma to the data by using the API requests we just set. We'll want to bind the **Home**, **Add note** and **Notes** pages with the **Get User Email**, **Add Note** and **List Notes** requests, respectively.

### Home screen

In this screen, we want to fetch the `user.email` variable to display a welcome message. We'll use the API request we specifically created for this, with the **httpbin** service.

1. In your Bravo project, click on the **Home** app page to open the **Data Binding** mode. Then, select the **Data Collection** we created (the group of API requests), and select the **Get User Email** request.
2. Select the `User` text element in the `greeting` frame, and bind it to the **variable containing the user email**. The name of this variable is set in the **Selected Data** tab of the Data Library, go there and check it out if you're not sure of which variable you should select.

### Notes screen

This screen will be the one listing all the user notes. Therefore, we must bind it to the **List Notes** request we set up before.

1. In your Bravo project, click on the **Notes** app page to open Data Binding mode. Then, select the Data Collection we created, and select the **List Notes** request.
2. Bind the **Notes container** to the `Records` value on the right pane, and the **Note text element** inside to the **variable representing the note content** (remember that this is set in the **Selected Data** tab of the Data Library).

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-Cf_-xFabXCvVq6%2FUntitled%202.png?generation=1629447259790275\&alt=media)

{% hint style="info" %}
If you preview on Bravo Vision now, this page will appear empty because you have not yet added any notes under your user ID. Add an empty page state to visually inform your users.
{% endhint %}

### Add note screen

Finally, we'll bind the **Add note screen** to the **Add Note** **request**, so that the user can add new notes.

1. In your Bravo project, click on the **Add note** app page. Then, select the Data Collection we created, and select the **Add Note** request.
2. Select the `Add note...` text form field inside of the main container, and bind it to the `noteContent` variable we defined in the JSON body of the Add Note request.
3. Finally, in the **Response Actions** pane, under On Success, select `Go To Page`, and choose the **Notes** page. This way, the app will navigate to the Notes screen after the user submits a new note. You can also set up a message to display in case an error occurs when sending the note.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MhXbw7lRw9Y23I7qHjh%2F-MhXc-ChL1GrmuGl8HNY%2FUntitled%204.png?generation=1629447259812966\&alt=media)

That's it! 🥳
