Build a signup form with HTML

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

#Example: custom signup form

You can clone the example signup form on CodePen(opens new window) 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
  • account-name - example of a custom field
  • password - required for signup with password
  • password-verify - optional, for checking the password before registering the user

#Pass form data to Userfront.signup()

The signup() method allows you to pass in data to sign up a user.

Our JavaScript needs to pass our form data into this method.

Userfront then does the following:

  1. Creates a user record
  2. Adds the user's access token as a cookie named access.demo1234
  3. Redirects the page to the After-signup path

#Example JavaScript

In the example code here, we do the following:

  1. Reference all the elements on the page
  2. Define a custom formSignup() method that calls Userfront.signup() with the form values
  3. Add an event listener to call formSignup() when the form is submitted

#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.

#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.

#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 the setAlert() method to display the error message inside of our alert element.

#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

Add an event listener to call

Userfront.signup({ method: "google" })

when the Google button is clicked. You can style the button however you like.

Other provider options like GitHub, LinkedIn, and Facebook are in the docs for signup().

#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.