My Notes: using user ID and email from Firebase

In this tutorial, you'll learn how to set the 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.

For this personalised message, another Bravo variable (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.

✨ 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

  • 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.

1️⃣ Set up authentication with Firebase

Once the design file has been imported into Bravo, follow this tutorial 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.

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.

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

     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.

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 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

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 **parameter. 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.

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:

 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.

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

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.

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.

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.

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).

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.

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.

That's it! 🥳

Last updated