Build an Advanced Registration Form

On this page

In this tutorial, you build a registration form for a fictional real estate application. The first step of the form collects the account details. The second step collects the user's house hunting preferences. You then register a user, view the data in the Admin UI, and read the data with the API.

For how advanced registration forms work, see Advanced Registration Forms.

The form collects the following profile data:

  • Email
  • Password
  • First name
  • Mobile phone
  • The geographic area where the user wants to buy
  • The minimum house price
  • The maximum house price

Prerequisites#

  • A FusionAuth instance with an activated license on a paid plan. To compare plans, see the pricing page. Activation needs outbound network access from FusionAuth. For the activation steps, see Licensing.
  • Access to the Admin UI.

Create Form Fields#

FusionAuth ships with Form Fields for standard user data, including the following:

  • Email
  • Phone number
  • Username
  • Password
  • First name
  • Middle name
  • Last name
  • Full name
  • Mobile phone
  • Birthdate

The first step uses only standard fields. The second step needs custom fields for the house hunting preferences. Only the real estate application uses the preferences, so the custom fields store the values in registration.data. For how to choose between user.data and registration.data, see Where FusionAuth Stores the Data.

Caution

After you save a form field, you can't change the Field, Consent, Data type, or Form control settings. To change one of these settings, create a new form field and replace the old field in your forms. Then delete the old field. FusionAuth doesn't delete a form field that a form still uses.

  1. In the Admin UI, go to Customizations -> Form Fields . The list shows the standard fields and any custom fields.

  2. Click to add a minimum price field. Set Field to Custom registration data (registration.data.*) and enter minprice as the key. Set Data type to Number and Form control to Number. Turn on Required, so a new user can't finish registration without a value. Save the field.

    The Add Form Field page with the minimum price field filled in.
    The Add Form Field page with the minimum price field filled in.
  3. Duplicate the minimum price field to add a maximum price field. Change the name, and set the key to maxprice. A key must be unique within the registration.data object. Keep the other settings, and save the field.

  4. Add a geographic area field with the key geographicarea. Set Data type to String. Leave Required off, because some users don't know yet where they want to buy. Save the field.

    The Add Form Field page with the geographic area field filled in.
    The Add Form Field page with the geographic area field filled in.

The list of form fields now shows the custom fields. Any form can use the fields.

Create a Form#

A form arranges Form Fields into steps. You can put standard and custom fields in any order and add as many steps as you need. Group similar fields in the same step.

Each form needs at least one step and a required email, phone number, or username field. For every rule, see Form Rules.

  1. Go to Customizations -> Forms and click . The new form shows a Name field and a button to add steps.

    A new registration form with no steps or fields.
    A new registration form with no steps or fields.
  2. Enter a name, and set Type to Registration.

  3. Add a step, and add the following fields to the step:

    • First name
    • Email
    • Password
    • Mobile phone
    The first step of the registration form with its fields added.
    The first step of the registration form with its fields added.
  4. Add a second step, and add the custom fields:

    • Geographic area
    • Minimum price
    • Maximum price
    The second step of the registration form with its fields added.
    The second step of the registration form with its fields added.
  5. To change the order of fields within a step, click the arrows next to a field. To move a field to another step, delete the field from the first step and add the field to the other step.

  6. Save the form.

The form controls the steps and the order of fields. To change the validation of a field, edit the field in Customizations -> Form Fields .

Associate a Form With an Application#

An Application uses a form for self-service registration. Any Application in any Tenant can use the same form.

  1. Go to Applications . Add an Application, or edit an existing Application with .
  2. On the OAuth tab, add a URL to Authorized redirect URLs. FusionAuth sends the user to the redirect URLThe URL where the authorization server sends the browser after login, with an authorization code or an error. after registration. In most configurations, the user is logged in at the end of registration. Settings such as required email verification change that behavior.
  3. On the Registration tab, set Registration mode to Create registration. Without self-service registration, users can't create accounts and never see the form.
  4. Set Type to Advanced, and set Form to the form you created.
  5. Save the Application.

Register a User#

  1. Go to Applications and view the Application with . Copy the Registration URL.

    The Application details dialog with the Registration URL.
    The Application details dialog with the Registration URL.
  2. Open the registration URL in a private browser window or in another browser. The first step asks for the first name, email address, password, and mobile phone number. Each step shows the number of steps in the form.

    The first step of the custom registration form on the hosted pages.
    The first step of the custom registration form on the hosted pages.
  3. Fill out the first step and continue. The second step shows the geographic area and the price fields.

  4. Fill out the second step and click Register . FusionAuth sends you to the redirect URL and logs you in.

To confirm the registration, log in to the Admin UI and go to Users . Manage the new user, and open the User data tab. The tab shows the custom data:

The User data tab of the new user, with the values from the registration form.
The User data tab of the new user, with the values from the registration form.

The theme has no messages for the custom fields yet, so the form shows field keys, such as registration.data.minprice, as labels and placeholders. To add readable labels, placeholders, and error messages, see Theme an Advanced Registration Form.

Read the Data#

FusionAuth returns the profile data from the APIs in the standard user fields, user.data, and registration.data. The Admin UI shows the data on the User data tab. To let admins edit the data in the Admin UI, see Custom Admin Forms.

To let users edit their own profile data, build a profile page in your application. OAuth and OIDC libraries don't return custom data, such as the price range. Read and write the data with the FusionAuth APIs or a client library instead.

The following example is a Python Flask application. The full code, with templates and the login and registration links, is in the example Flask portal on GitHub.

  1. Go to Settings -> API Keys and add an API key. Allow the following endpoints:

    • /api/user/registration: all methods
    • /api/form: GET only
    • /api/form/field: GET only
  2. Add a home page route that retrieves the Registration of the logged in user:

    # ...
    
    @app.route('/', methods=["GET"])
    def homepage():
      user=None
      registration_data=None
      fields = {}
      if session.get('user') != None:
        user = session['user']
        fusionauth_api_client = FusionAuthClient(app.config['API_KEY'], app.config['FA_URL'])
        user_id = user['sub']
        application_id = user['applicationId']
        client_response = fusionauth_api_client.retrieve_registration(user_id, application_id)
        if client_response.was_successful():
          registration_data = client_response.success_response['registration'].get('data')
          fields = get_fields(fusionauth_api_client)
        else:
          print(client_response.error_response)
      return render_template('index.html', user=user, registration_data=registration_data, fields=fields)
    
    # ...
    

    The route reads the user object from the session after a successful login. The sub claim holds the user Id, such as 8ffee38d-48c3-48c9-b386-9c3c114c7bc9. The route also reads the applicationId claim. The route then retrieves the Registration with the FusionAuth client and passes the data field to the template as registration_data. The route also passes the result of the get_fields helper to the template as fields.

  3. Add the get_fields helper:

    # ...
    
    def get_fields(fusionauth_api_client):
      fields = {}
      client_response = fusionauth_api_client.retrieve_form(app.config['FORM_ID'])
      if client_response.was_successful():
        field_ids = client_response.success_response['form']['steps'][1]['fields']
        for id in field_ids:
          client_response = fusionauth_api_client.retrieve_form_field(id)
          if client_response.was_successful(): 
            field = client_response.success_response['field']
            fields[field['key']] = field
      else:
        print(client_response.error_response)
      return fields
    # ...
    

    The helper reads the Ids of the fields on the second step of the form, ['form']['steps'][1]. The helper then retrieves the configuration of each field. The helper stores each configuration in a dictionary under the field key, such as registration.data.minprice. The update form uses the dictionary to build the inputs. When more than one step collects profile data, change the helper to loop over every step.

  4. Add a route that processes the update form:

    # ... 
    @app.route("/update", methods=["POST"])
    def update():
      user=None
      error=None
      fields=[]
      fusionauth_api_client = FusionAuthClient(app.config['API_KEY'], app.config['FA_URL'])
      if session.get('user') != None:
        user = session['user']
        user_id = user['sub']
        application_id = user['applicationId']
    
        client_response = fusionauth_api_client.retrieve_registration(user_id, application_id)
        if client_response.was_successful():
          registration_data = client_response.success_response['registration'].get('data')
          fields = get_fields(fusionauth_api_client)
          for key in fields.keys():
            field = fields[key]
            form_key = field['key'].replace('registration.data.','')
            new_value = request.form.get(form_key,'')
            if field['control'] == 'number':
              registration_data[form_key] = int(new_value)
            else:
              registration_data[form_key] = new_value
          patch_request = { 'registration' : {'applicationId': application_id, 'data' : registration_data }}
          client_response = fusionauth_api_client.patch_registration(user_id, patch_request)
          if client_response.was_successful():
            pass
          else:
            error = "Unable to save data"
            return render_template('index.html', user=user, registration_data=registration_data, fields=fields, error=error)
      return redirect('/')
    # ... 
    

    The route retrieves the Registration and copies the new values from the update form into the data object. The route converts number values from strings. You can extend the conversion to boolean or other data types. The route then sends a PATCH request, which updates only the data field of the Registration.

To confirm the result, log in to the Flask application and change a price. The profile page shows the new value:

The profile page of the example Flask application, with the house hunting preferences.
The profile page of the example Flask application, with the house hunting preferences.