Run Custom JavaScript After Signup with the Universal Login Page

Overview

This article explains how to perform a JavaScript operation before redirecting while using Auth0’s Universal Login with webAuth.redirect.signupAndLogin.

Applies To

  • Universal Login
  • JavaScript Operation

Solution

It is possible to use the webAuth.signup API followed by webAuth.login instead of webAuth.redirect.signupAndLogin. This allows the execution of custom JavaScript code after a successful signup but before redirecting the user.

This works by calling webAuth.signup and using a callback that performs the custom operations and subsequently calls webAuth.login.

Here’s an example:

function signupAndLogin() {
  var button = this;
  var email = document.getElementById('email').value;
  var password = document.getElementById('password').value;

  button.disabled = true;
  webAuth.signup({
    connection: databaseConnection,
    email: email,
    password: password,
    captcha: captcha.getValue()
  }, function(err) {
    if (err) {
      displayError(err);
      button.disabled = false;
      return;
    }
    
    // Perform any custom operations here

    webAuth.login({
      realm: databaseConnection,
      username: email,
      password: password,
      captcha: captcha.getValue()
    }, function(err) {
      if (err) {
        displayError(err);
        button.disabled = false;
        return;
      }
    });
  });
}

document.getElementById('btn-signup').addEventListener('click', signupAndLogin);

Thanks for sharing it with the rest of community @Saltuk!