Build a signup form with React

#Before you begin

  • Sign up for a Userfront account
  • In your Userfront Dashboard, note your workspace ID: this appears under the workspace name on your Overview page, and looks something like 123abc4d

In this section, we create a custom signup form with email and password that includes:

#Example: custom signup form with React

You can clone the example signup form on CodePen and make edits, or follow along below.

The example form has the Userfront Core JS library added to the document, as described in the next section.

#Add the Userfront Core JS library

You can add the Userfront Core JS library by CDN or using npm.

You only need to do one of these.

#CDN

#NPM

Then import the library into your file(s)

#Set up the form

Create your signup form with the elements you want to use.

In this case, we've added:

  • email - required for signup
  • accountName - example of a custom field
  • password - required for signup with password
  • passwordVerify - optional, for checking the password before registering the user

#Signup form React code

#constructor()

Here we set up our state variables with email, accountName, password, and passwordVerify.

We also bind our functions so that this.setState will update the state variables.

#handleInputChange()

Whenever an input changes value, this function will set the corresponding state variable.

#handleSubmit()

When the form is submitted, this function will call Userfront.signup() with the current email and password.

For custom fields like accountName, we use the data object. We can add any custom fields we like to this object, and they will be saved upon signup.

#render()

Adds the signup form with 4 inputs and a button, and connects them to the handleInputChange() and handleSubmit() functions.

#Custom fields

The form has a field for Account Name, which is a custom field.

When we pass this to the Userfront.signup() method under the data object, it is saved to the user's record as user.data.accountName.

#Error handling

Whenever the Userfront.signup() method fails, we can catch its error in the promise chain.

This error contains a message property with what went wrong.

In this example, we use an <Alert /> component to display the error message.

Our signup form can use this component by including an alertMessage variable in the state, and then setting it whenever we want to update the message.

Now the handleSubmit() method clears the alert message whenever the button is clicked. Then if there is an error with Userfront.signup(), it catches the error and displays the error message.

The alert component is rendered above the form as:

<Alert message={this.state.alertMessage} />

#Password verification

Userfront will verify that the password is the correct length and format, and we can additionally verify that the user has typed what they intended by having them type it twice.

This "passwords match" verification is performed before sending the information to Userfront.

#Single sign-on

To configure Single sign-on (SSO), first add the provider you want to use in the Userfront dashboard in the SSO tab.

The SSO flow is as follows:

  1. The user clicks the SSO button ("Sign up with Google"), which triggers Userfront.signup()
  2. The browser redirects to the provider (Google), where the user authorizes your application
  3. Upon success, the browser redirects back to your login page (your After-logout path) with uuid and token login credentials in the URL
  4. Your application calls Userfront.login() to log in the user with the uuid and token

#Sign up with Google button

In this example, we add an <SSOButton /> component to allow signup with Google.

We need to call Userfront.signup({ method: "google" }) whenever the button is clicked. You can style the button however you like.

You can find more provider options like GitHub, LinkedIn, and Facebook in the docs for signup().

To render the <SSOButton /> component into the signup form, we can add it below the <form> element.

#Login after redirect

Once the browser is redirected back to your login page after SSO approval, your application should call

Userfront.login({ method: "link" })

You can set up your JS to call this method automatically by checking whether the URL contains the token and uuid parameters.

If your original SSO signup call contained a redirect parameter, it will be included in the URL and followed automatically.