Newer Version Available

This content describes an older version of this product. View Latest

Headless Forgot Password JavaScript Examples

Use these high-level examples to understand how to implement the Headless Forgot Password Flow for a single-page app.

The Headless Forgot Password Flow contains two requests to the same endpoint. You initialize the flow with a request to Headless Forgot Password API. Here’s a function that you can call to initialize the password reset.

1//This is the function call to initialize the forgot password request
2forgotPasswordRequest(username, null, null, recapchaToken, forgotPasswordProcess.init, callbackFunction);

This request results in Salesforce sending the user a one-time password (OTP).

For the second request, you pass the username, new password, and OTP to Headless Forgot Password API. Here’s a function that you can call to complete the password change.

1//This is the function call to complete the forgot password request
2forgotPasswordRequest(username, password, otp, null, forgotPasswordProcess.changePassword, callbackFunction)

Because both requests call the same endpoint, you can use one function for both calls.

1function forgotPasswordRequest(username, password, otp, recapchaToken, forgotPasswordProcessStep, callbackFunction) {
2    client = new XMLHttpRequest();
3    client.open("POST", expDomain + forgotPasswordURI, true);
4    client.setRequestHeader("Content-Type", "application/json");
5
6    requestBody = {
7    username: username,
8    newpassword: password,
9    otp: otp,
10    recaptcha: recapchaToken
11    }
12
13    client.send(JSON.stringify(requestBody)); 
14
15    client.onreadystatechange = function() {
16      if(this.readyState == 4) {
17        if (this.status == 200) {
18          callbackFunction(JSON.parse(client.response), username, forgotPasswordProcessStep)
19        } else {
20          this.onError("An Error Occured during Forgot Password Step: " + forgotPasswordProcessStep, client.response); 
21        }
22      } 
23    }
24  }