Add Additonal Sign Up Steps with Auth0 Forms

Published on October 28, 2024
Learning GoalLearn how to use Auth0 Forms to add additional steps to the signup flow to collect more information from users

Auth0 Forms has a visual editor that you can use to add custom steps and business logic to your authentication flow.

In this lab, you will learn:

  • Configure additional signup steps using Auth0 Forms.

  • Create a vault connection with Forms to store secrets or API keys.

  • Add a custom Form to Auth0 Login flow.

Prerequisites

Before you can create a new Form these two steps need to be in place:

  1. A Machine-to-Machine Application (M2M) with these permissions (also known as scopes) enabled:
  • read:users
  • update:users
  • create:users
  • read:users_app_metadata
  • update:users_app_metadata
  • create:users_app_metadata
Since this Form is going to change user metadata, it needs to communicate with the Auth0 Management API. You're creating a M2M application to facilitate that communication.
  1. A Forms Vault connection set up with your M2M application credentials.
The Vault is where you store your credentials from the M2M application and it lets Forms access these values securely.

Create a New Form

To start creating a new form go to the Auth0 Dashboard, click on Actions then Forms.

Clicking on Forms will open a new tab in your browser

On the Forms page click the Create Form button, which will open the Create Form modal. On the modal, click Start from Scratch

Auth0 Forms Create Form modal

This will open the Form Editor, by default a new form includes a Start node, a Step node, and an Ending screen node. Nodes are the progression of the Forms process, you can add and remove Nodes within this process.

New Auth0 Form

Configure the Step Node

Step nodes are the graphical interface that users see when your form is rendered.

On the Components menu, scroll down to the Blocks sections and drag and drop the Rich text component to the middle step node.

Auth0 Form Rich text components

A Blocks settings menu will pop on screen when you drag the Rich text component onto the step node. In the text box, type in a custom message:

Name
Complete your profile
Add more information to personalize your content
Rich text block settings
You can add bold, italics, and even change formatting in the settings of the Rich text component

Click Save, then proceed to the next step where you'll see a text input box for the user to enter their information.

On the Components menu, scroll up the Fields section, then drag and drop two text blocks to the Step node below the Rich text block.

Click on the first Text field and the Field Settings menu will pop on screen. Fill in the ID field as company_name. The ID can be referenced when you pass data to your Flows (you will learn about Flows in a moment).

When you add a Form field or a Flow action, the ID is automatically generated. Update the ID to a more descriptive value so you can identify the information it contains

Next, click the Required checkbox, then enter Company Name in the Label field and click Save

Text field Component

Click the next Text field and the Field Settings menu will pop on screen. Fill in the ID field as job_title. Next, click the Required checkbox, then enter Job Title in the Label field and click Save

Forms Company Job Title Field

Next, click Publish to save the Form you created so far.

You have created the form for your user to fill out. Next, you'll create a Flow to save the user input to the user metadata

Configure the Flow Node

Flows represents the server-side logic executed within your Forms. Flows have a visual editor you can use to build that logic

Now you'll create a new Flow to save the user inputs from the Text fields to the user metadata. The Flow will use the Auth0 Management API to use the credentials you saved in the Forms Vault.

To create a new Flow, click Flow at the bottom of the Forms editor and it will bring up the "Flow settings" menu. Click Create New Flow on the Flow settings menu. This will open the Flow name box. Enter Update User as the name, then click Create to update the flow name. Now, click Edit Flow to open the Flow Editor.

The Flow Editor will open a new tab in your browser
Auth0 Forms Flow Editor

Now let's use the Flow editor to update the user metadata with the information the user inputs from your Text field. Click on the plus sign (+) right below the Start at the top of the Flow editor. This will bring up the List of Actions menu.

Auth0 Forms List of Action in Flow Editor

Now you can configure the Auth0 Update User action to get the data from the fields and update the user metadata by connecting to the Auth0 Management API. Click the drop-down on the Vault connection and select the Auth0 vault connection you created before you start this lab.

For the User ID field, you're going to use Forms variables

Variables allow you to access data contained in Forms and Flows to create custom business logic and automation.

Forms variables are surrounded by curly brackets {{ variable }}. You can use different types of variables to reference and change data you receive from customers with Forms and Flows. In the User ID field, you'll use the {{context.*}} variable, which references the context data of the current transaction, including the current user.

In the User ID field, enter {{context.user.user_id}}.

{{context.user.user_id}} references the user_id of the user object.

To update the user metadata with company_name and job_title properties, add the code below to the Body section using the Forms field variables.

{
"user_metadata": {
"company_name": "{{fields.company_name}}",
"job_title": "{{fields.job_title}}"
}
}
Forms field variables contain data collected from the user from input fields in the form. The data in the field depends on the field type. {{fields.first_name}} references the input of a field with the ID first_name.

After you configure your Update User action, click Save.

Auth0 Update user flow action completed

Now that the Flow is done, on the Flow editor click Publish to save the Flow, and close out the Flow editor tab in your browser.

To add the logic of of the Update user Flow to the Form, on the Flow editor delete the "line" connecting the Step node and the Ending Screen node. Drag and Drop the Update user Flow in between the Step Node and the Ending Screen node. Then, connect the "lines" on each side of the Flow to the Step node and Ending Screen node as pictured below.

Auth0 Form Flow Connection

Now that the Form is completed, click Publish. This will save the form and the logic. Now you need to render the form so that it prompts your users when they log in. So, next, you'll get the code to render the Form and add it to the Login flow with an Auth0 Action.

Render Form with Auth0 Actions

On the top of the Form editor, you have options for "Edit" and "Render". To render the form, click Render. This will bring up the Render page, which has the code you need to add to an Auth0 Action to render the form. On the bottom of the page, click the Copy button.

Auth0 Forms Render Page

Above the render code box, go and click the Create a Post Login Action link. This will open up the Auth0 Actions Library to create a new Action. Click Create Action, then choose Build from scratch

Create New Auth0 Action

The Create Action modal will pop up. The first step is to name the Action. You can name this Action Render Additional Signup Info

Name
Render Additional Signup Info

Confirm that Login / Post Login is the trigger and keep the recommended runtime. Once all that is complete, click Create to open the Action code editor.

Create Action Modal

For post-login Action to render the Form, start by deleting the existing code from the Action Code editor. Next, paste the Form render code into the Code editor. Then, edit the code to define the conditional logic that will render the form.

exports.onExecutePostLogin = async (event, api) => {
const FORM_ID = 'REPLACE_WITH_YOUR_FORM_ID';
if (
!event.user.user_metadata.full_name &&
!event.user.user_metadata.job_title
) {
api.prompt.render(FORM_ID);
}
}
This code will render the form when the users metadata doesn't have the company_name and job_title properties.

Once the code is entered, click Deploy to save the Action.

Completed Auth0 Actions

In the final step, you need to add the Action to render the Form when the user logs in.

On the left side of the Auth0 Dashboard, click Actions, then Triggers. On the Triggers page, click "Post Login". This will open the Actions trigger drag and drop editor.

Auth0 Actions Trigger Page

On the right side of the Trigger editor under Add Action, click on Custom to bring up the Actions you've built. Drag your Action that renders the Form you just created and drop it in between the Start and Complete points on the editor. Then click *Apply to update the Login with the new Form.

Auth0 Login Trigger

Now, you can run your application and start the login process with a user that doesn't have company_name or job_title in their user_metadata

New Form Render
Forms automatically inherits the branding of your Universal Login settings

Recap

Congratulations! You have successfully collected additional information about a user with Auth0 Forms! You learned how to:

  • Create a Custom Form from scratch
  • Change the authentication flow to update the user's metadata and resume authentication after the information is submitted.
  • Render the Form by using Auth0 Actions.