Getting data from Firestore
Last updated
Last updated
In this page, we'll describe how to connect Bravo and Firestore, the non-relational database provided by Firebase.
In order to follow this tutorial, we'll need an existing Firebase project with one or more Firestore collections.
To be able to connect to Firestore from Bravo, we'll need to implement user authentication with Firebase on our app. When a user logs in, an authentication token will be created, and this will allow us to target the Firestore REST API and use it as database for our app (more on this in the next section).
To set this up, we'll need to follow the steps described in the documentation below:
📲User login with FirebaseOnce we have our Firebase authentication in place, we can create some users, either in the Firebase project itself, or by creating a register screen in the app and creating users there.
After having some users in Firebase, we can start creating our API collection to target Firestore. If you haven't created a Firestore collection yet, you can do that in the Firestore Database
section of the Firebase console.
Once we have the Firebase project in place, and we have set up Firebase login in our app project, we can start creating our API collection.
Firestore requires authenticated requests to be able to communicate with its REST API. This means the API requests will contain an auth token from an existing user in our Firebase project. This token will be validated in Firestore when performing an API request.
Therefore, we'll specify an authentication method in our API collection. Once you have created a new collection, go to Collection Settings, and specify the Bearer authentication mode (leaving the token field empty).
Once we do this, we'll be able to create API requests targeting the Firestore collections.
The next step will be creating a sign in request. Although we won't bind this request to any app screen, we'll need it to obtain an authentication token and send authenticated requests against the Firestore API.
For the sign in request, we'll use the following URL, as indicated in the Firebase API reference.
We'll replace [API_KEY]
with the value of the Web API Key of our Firebase project. We can obtain it in the Project Settings section of our project, in the Firebase dashboard.
This will be a POST
request. We'll need to add the following body to it:
Now, we'll be able to test the request by indicating the credentials (email and password) of an existing user in our project in Test Values
. If the request is successful, we'll get back a set of values, including our authentication token (.data.idToken
path).
Now that we can get valid authentication tokens, we'll be able to interact with our Firestore collections. In this example, we want to retrieve data from our data collection Users:
According to Firebase API documentation, to create the Rest API endpoint, you should use the following structure:
https://firestore.googleapis.com/v1/projects/
YOUR_PROJECT_ID
/databases/(default)/documents/
COLLECTION_NAME
Replace YOUR_PROJECT_ID
with your current project ID name.
Replace COLLECTION_NAME
with your data collection name, in this case, users
.
Add the URL request to Bravo:
Don't forget to add _authorization
in your Test values and add the token you've received from the Sign in request to authenticate the request.
Click send to receive the data coming from your Firestore data collection! 🎉