# Authentication with custom backend

The **Custom Authentication** feature allows creating an authentication flow for your apps using any backend tool of your choice, even a custom backend.

In this page, we'll describe how to set up the design file and the necessary API requests to perform the user authentication.

## What you will need

* An app design with the Bravo authentication tags correctly set up.
* A prepared backend with several endpoints for authentication: login, user information and (optionally) signup.
* The API Collection configured in Bravo Studio.
* Finally, bind the API requests to your UI elements to add functionality to your app.

Let's learn how to do all this!

{% hint style="warning" %}
**If you don't have your user authentication backend ready**, we suggest you to first follow our tutorial on [how to set up Xano as your user authentication backend](https://docs.bravostudio.app/integrations/user-authentication/authentication-with-custom-backend/set-up-xano-as-your-user-authentication-backend).
{% endhint %}

## 1. Setting up the UI <a href="#id-1.-setting-up-the-ui" id="id-1.-setting-up-the-ui"></a>

We'll need the following pages to set up the authentication flow in the apps:

* Login page.
* Signup page (optional).

🎨 Here is a sample Figma file with the setup:

{% embed url="<https://www.figma.com/community/file/1046060070731163579/Bravo-Sample%3A-Custom-Authentication>" %}

### **Login page** <a href="#login-page" id="login-page"></a>

The login page is displayed when the user is not logged in upon app startup or after logging out. It is important to note that **only one login page** can exist within the app.\
\
Components:

* <mark style="color:red;">`[page:login]`</mark>: Add to the screen that we want to use as the login page.
* <mark style="color:red;">`[component:input-email]`</mark>: Creates an input field for the account email.
* <mark style="color:red;">`[component:input-password]`</mark>**:** Creates an input field for the account password.
* <mark style="color:red;">`[action:login]`</mark>: Defines the action triggered by the button, facilitating the login process using the credentials entered in the previous fields.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FJOmP0z1bTWHeRLy3CunG%2FCaptura%20de%20pantalla%202021-11-25%20a%20las%2018.50.54.png?alt=media\&token=748cb2b1-67f4-476d-a1e4-e6f3571f4f35)

### **Signup page (optional)** <a href="#signup-page-optional" id="signup-page-optional"></a>

You can either allow news users to register in your app or only allow access to a specific list of users defined in your backend. While this page is optional, it provides the simplest method for users to create their own accounts.\
\
Components:

* <mark style="color:red;">`[component:input-email]`</mark>: Creates an input field for the account email.
* <mark style="color:red;">`[component:input-password]`</mark>`:` Creates an input field for the account password.
* <mark style="color:red;">`[action:register]`</mark>: This triggers the process of sending the filled information to the backend to create a new user account.&#x20;

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FFrS8dRemQpYi1cPgc4MQ%2Fimage.png?alt=media&#x26;token=5793332e-736c-4f1b-ac51-df486b3fc911" alt=""><figcaption></figcaption></figure>

### Log out action (optional) <a href="#log-out-action-optional" id="log-out-action-optional"></a>

If you want to provide an option for your users to log out after they have logged in, you can incorporate this action tag into a UI element.\
\
Component:

* <mark style="color:red;">`[action:logout]`</mark>: This action enables the log out functionality and can be applied to any UI element within the app.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FMJeKmUKckI0stopdbOjb%2FCaptura%20de%20pantalla%202021-11-25%20a%20las%2018.37.49.png?alt=media\&token=75120d53-a102-42de-9338-b9016dfe9dfb)

## 2. Creating the API collection and requests

### Necessary API endpoints

After configuring the design file, the next step is to construct an API collection which contains all the essential requests for the authentication flow. This involves setting up the following endpoints in your backend tool:

* A <mark style="color:blue;">`POST`</mark> request for the login page
* A <mark style="color:blue;">`POST`</mark> request for the signup page (if applicable)
* A <mark style="color:green;">`GET`</mark> request, called User Info, which retrieves user-specific information like user ID and username from the backend.

&#x20;These endpoints must, at least, return the following data:

* **Login and signup actions (**<mark style="color:blue;">**`POST`**</mark>**&#x20;requests)**: The endpoint should return a **user token value** in the response message. This value is generated by the backend tool based on the authentication method used.
* **User Info request (**<mark style="color:green;">**`GET`**</mark>**&#x20;request):** The endpoint must return, at least, a **user ID** value. It can also return **user name**, **user email** and **user picture** values.&#x20;

{% hint style="warning" %}
For comprehensive guidance on Setting up Xano as your user authentication backend, please refer to our dedicated tutorial: [how to set up Xano as your user authentication backend](https://docs.bravostudio.app/integrations/user-authentication/authentication-with-custom-backend/set-up-xano-as-your-user-authentication-backend).
{% endhint %}

### Create the API collection in Bravo

Once we have the backend endpoints ready, we’ll create a new API collection in Bravo that will contain all the requests.&#x20;

1. In **Account Collections,** click on Create a New Collection, and select **Start from scratch**.

2. Provide a **Collection Name** and if desired, **Description** and then, **Save.**

3. Click on the **Collection Settings** (look for the icon beside the collection name).<br>

   <div align="left" data-full-width="true"><figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FqRwKi97pUqW54pmCIDs7%2FScreenshot%202023-10-24%20at%2010.57.38.png?alt=media&#x26;token=e6a8a4af-d28e-4b1b-baa0-8fdcfa72d3c6" alt=""><figcaption></figcaption></figure></div>

4. Navigate to the **Authentication** tab, indicate the authentication method that your backend uses.

{% hint style="info" %}
Keep in mind that there are different authentication methods, it is important to identify which one your backend employs. Your backend may already have a specific method in place. For example, Xano utilises the bearer token method, issuing a [JWT token](https://jwt.io/) upon successful user authentication. If you're unsure about the authentication method your backend employs, consult the tool or system's documentation for guidance.
{% endhint %}

{% hint style="warning" %}
For the **Bearer** token method, you must **leave the Token value empty**, as this will be later processed by Bravo to insert the correspondent for each specific user in the requests.
{% endhint %}

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FQiAkhjcZjZRbbZRFbLD1%2FCaptura%20de%20pantalla%202021-11-25%20a%20las%2019.16.12.png?alt=media\&token=69aa07c8-cec4-49cb-ab22-993a0c172e06)

### Add the requests to the API Collection

Let's define the individual API requests:

#### Login request (POST)

This request will be bound to the UI elements in the Login screen: the email and password input fields.

1. Create a new request in the collection, and select <mark style="color:blue;">`POST`</mark> as the request type. Copy the endpoint URL from your backend tool, and paste it in the request URL.
2. Since it's a POST request, it's crucial to specify the body of the request, which will hold the email and password credentials. For this example, click <mark style="color:blue;">`Body`</mark> and select <mark style="color:blue;">`JSON`</mark>. If your backend uses a different format like Form-data, select that option instead.
3. Fill the body content with the following JSON:

```
{
  "email": "${email}",
  "password": "${password}"
}
```

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FLm8zybqoBzpgUkeyHehb%2FCaptura%20de%20pantalla%202021-11-25%20a%20las%2019.16.52.png?alt=media\&token=da057bfa-31fd-42c2-8288-50294e8b2b09)

Please note that <mark style="color:red;">`${email}`</mark> and <mark style="color:red;">`${password}`</mark> are [Bravo variables](https://docs.bravostudio.app/connect-api/request-url-variables#how-can-a-variable-be-defined) designed to store values from the app's fields. On the other hand, the names "email" and "password" are the identifiers that the backend use to receive this data, so they must align with the API endpoint's definition. It's possible that they might differ, such as "\_email" or "userEmail," for instance. It's important to verify the specific names required.

4. Time to test the login request! Make sure you have created a user in your user table. Go to **Test Values** and fill in the email and password variables with the values you have created:

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F8khuDXL8kramj1q35pfI%2FCaptura%20de%20pantalla%202021-11-25%20a%20las%2019.25.26.png?alt=media\&token=8f1c80b0-3972-4d22-a49e-7281dec70b96)

5. Click **Send**, and you’ll get in the response from your backend. In the **Received Data** tab, select the parameter correspondent to the auth token:&#x20;

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FbTREPITkcGp4BMMu9Uuf%2FScreenshot%202022-06-27%20at%2014.23.26.png?alt=media\&token=cd7b4081-566c-481b-adb1-803b85583b0b)

6. Once selected, go to the **Selected Data** tab and set the **Name** as <mark style="color:red;">`accessToken`</mark>:

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FatJUg79iQ6JoYHSHCOpX%2FScreenshot%202022-06-27%20at%2014.26.03.png?alt=media\&token=48865114-916c-4e63-a739-941d5a58d0c7)

7. To make sure the request worked as expected, go to the **Debug** tab related to the request. If everything went well, you'll find the full **user token** value in the response message. Copy this value, as we’ll need it later to configure the User Info request.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F3IaKHfzSXnoUdrtjJMxD%2FScreenshot%202022-06-27%20at%2014.27.47.png?alt=media\&token=ad7a5b26-773f-46b5-8860-6671067a6f16)

#### Signup request (POST) - optional

If you've designed a signup page, you'll want a POST request to create new user accounts. Just like the login request, you'll first need to specify the API endpoint that handles adding new users in your backend tool.

1. Create a new request in the collection, and select <mark style="color:blue;">`POST`</mark> as the request type.&#x20;
2. Copy the endpoint URL from your backend tool, and paste it in the request URL.
3. Define a request body. In this case, the body will be the same we used for the Login request, but keep in mind that you’ll need to check the names for the keys (the ones to the left in the body) in your backend tool - "email" and "password" in this case.&#x20;

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FG2AIuJmUkQRjgrKzpQeb%2FCaptura%20de%20pantalla%202021-11-25%20a%20las%2019.15.37.png?alt=media\&token=ea873917-63fc-464d-b305-f49a3b41cd54)

5. &#x20;Test the request providing some **Test Values**. If the request is successful, a new user with the specified email and password values will be created in the backend.
6. After clicking **Send,** based on you backend, you might receive a user token data which will allow the user to log in your app after sign in. If you wish to proceed with this, simply select the token data.
7. Just as the login request, once selected, go to the **Selected Data** tab and set the **Name** as <mark style="color:red;">`accessToken`</mark>:<br>

   ![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FatJUg79iQ6JoYHSHCOpX%2FScreenshot%202022-06-27%20at%2014.26.03.png?alt=media\&token=48865114-916c-4e63-a739-941d5a58d0c7)

#### User Info request (GET)

Let's set up a request to retrieve specific user information. This request **must return** a user ID as a unique identifier. Make sure this is properly configured in your backend tool. Additionally, it can also fetch data like the user's name, email, and profile picture.

1. Create a new API request and in this case, select <mark style="color:green;">`GET`</mark> as the request type.
2. Insert the corresponding endpoint URL.

{% hint style="info" %}
It's important to note that this request will fetch data from a real user stored in your database. Therefore, it requires a valid user token for access. Otherwise, the access to the backend data will be unauthorised.
{% endhint %}

3. Go to the **Test Values** section, set <mark style="color:red;">`_authorization`</mark> as key name, and paste the user token you copied before from the Login/Signup request step before.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FyLwVwpqsWGveXFL9Zjd3%2FCaptura%20de%20pantalla%202021-11-26%20a%20las%209.27.01.png?alt=media\&token=955e5616-19a9-4e2c-99d3-a39e0efc24ee)

4. Test the request. If it’s successful, it will return the necessary user data specified before. You need to give these data items some specific naming in the Selected Data section:

* User ID (mandatory): <mark style="color:red;">`id`</mark>
* User name: <mark style="color:red;">`name`</mark>
* User email: <mark style="color:red;">`email`</mark>
* User picture: <mark style="color:red;">`picture`</mark>

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FoCsbOsmStj6Mu295aSIc%2FScreenshot%202023-10-24%20at%2013.06.16.png?alt=media&#x26;token=05b4b647-9369-4e26-a92f-9d266fe7f387" alt=""><figcaption></figcaption></figure>

In our example, we're only getting the "id" field, which is mandatory. If you get more data from your endpoint, use the names we've suggested.

**All authenticated requests**

An authenticated request means that the backend requires a valid authentication token to be included in the request headers when interacting with a specific API endpoint or group of endpoints. This allows the backend to process information tailored to the user who just logged into the app, like fetching a list of items or receiving user inputs through a form. The backend tool itself determines whether an endpoint requires authentication.

For example, the User Info GET request we set up earlier targets an authenticated endpoint in the backend, retrieving user-specific sensitive information. You'll likely want to add more authenticated requests to your API collection, which you can then link to different screens in your app.

By default, all requests in a Bravo API Collection are set to be authenticated, as long as this is specified in the Collection settings (which we did earlier [here](#creating-the-api-collection)). o test them correctly, make sure to include the <mark style="color:red;">**`_authorization`**</mark> test value along with a valid token, as we did for the User Info GET request previously.

{% hint style="warning" %}
In case you get a <mark style="color:red;">`403 error`</mark> after sending the test request, double-check that you've set the <mark style="color:red;">`_authorization`</mark> test value with a valid token. Also, ensure that the API collection has authentication enabled in the collection settings. **You can obtain a new valid token by running a login request from the corresponding API request.**
{% endhint %}

## 3. Binding the UI elements to the requests

For this part, you should have already set up the UI, imported the design to Bravo and created the API collection.

1. Go to your app's `Integrations` section and select the `Custom Login` option.&#x20;
2. Select the `GET User Info` request created before. If everything is correct, you’ll see some green ticks next to the different user data items.

![](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FyVwH0LZHrUKRX2P6bP7Q%2Fimagen.png?alt=media\&token=a89c4df8-677e-4f0f-8bfb-003e6c7103c4)

&#x20;3\. Make sure to click **Save**! Its time to bind the screens to the data items from the API requests.

### Login screen

1. Select the Login screen.&#x20;
2. Bind the email and password inputs with the corresponding variables of the Login request body, as indicated below.&#x20;

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FMmsVpmfRVgZzcQ17D8vo%2Fezgif.com-video-to-gif%20(3).gif?alt=media&#x26;token=7f407c64-5475-4b70-820f-0ebb6d40226c" alt=""><figcaption><p>Binding email input</p></figcaption></figure>

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FscAdoALiPinFYMNp9YDk%2Fezgif.com-video-to-gif%20(4).gif?alt=media&#x26;token=0305aa0d-350b-4406-8f02-780f2d0ab6f4" alt=""><figcaption><p>Binding password input</p></figcaption></figure>

3. Then, in the Form Response Action section, select the `Set Access Token` option, and set the token variable to <mark style="color:red;">`${accessToken}`</mark>. This is the name we gave to the data item containing the user token, in the Login request. This ensures that Bravo will securely store the user token upon successful login.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FhoYIE4IgMPulqNzLW1gL%2Fezgif.com-video-to-gif%20(5).gif?alt=media&#x26;token=2a771894-db46-44e0-b477-7213181b5dec" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
To define which is the app home page, you need to use the [prototyping tool in your design](https://docs.bravostudio.app/set-up-design). **Don't use the "Go to page" action in the Form Response Action**, otherwise the authentication setup won't work.
{% endhint %}

### Signup screen

Finally, bind the signup screen (if applicable). Here you have two options:\
\
1\. **Sign up and redirect the user to the Login screen**. \
\
In this case, the user has to enter the log in credentials to finally access the app. To do this, set `Go to page` as Form Response Action and select the Login screen.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FAcTgRtNe2AfmuBZPVm6c%2Fezgif.com-video-to-gif%20(6).gif?alt=media&#x26;token=04653b12-a38a-40d5-80c4-e9ee0a8727b7" alt=""><figcaption><p>Go to page - Login</p></figcaption></figure>

2. **Sign up and automatically log in the user.**&#x20;

To do this, set `Set Access Token` as Form Response Action and set the token variable to <mark style="color:red;">`${accessToken}`</mark> just as how you've set the Login screen.

![Set Access Token](https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FcQjJO45AeMMQ85jam6Vf%2FCaptura%20de%20pantalla%202021-11-26%20a%20las%200.00.56.png?alt=media\&token=92ce4a87-4e4b-4d2c-9679-dbbef6533fa7)

Now, the authentication flow should be finished! Go ahead and test it on [Bravo Vision](https://docs.bravostudio.app/get-started/bravo-vision-previewer) 📲
