# Form button, trigger action instead of flow?

**URL:** <https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [November 5, 2025, 4:13am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082 "2025-11-05T04:13:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurt.dunn](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kurt.dunn/32/72021_2.png) [@kurt.dunn](https://community.auth0.com/u/kurt.dunn)\
**Post date:** [November 5, 2025, 4:13am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/1 "2025-11-05T04:13:47Z")

</div>

Hi all,

I have a form that collects text from 2 text inputs. From there i need to collect the inputs and then run 2 http calls, one for a token the other for the response needed.

When i get a response i either need to continue or display the error message back into the form so the user can try again.

Is it possible to listen to the click events in a form? The flow http fucntion is very basic and doesnt allow for the repsonse to be used with actions and logic.

Below is an example of what I need.

```auto
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {

      api.prompt.render(event.secrets.FORM_ID);

      const input1 = event.prompt.fields.input1
      const input2 = event.prompt.fields.input1

      const button = event.prompt.id = 'button1'

      button.onclick () => {
        // GET A TOKEN
        const getToken = await fetch(url)
        const token = getToken.response()

        // GET THE DATA
        const getData = await fetch(url, token)
        const data = getData.response()

        if(data.err){
          api.prompt.render(':form_id', {
              vars: {
                errmsg: '123',
              }
            });
        } else {
          return;
        }
      }

};

```

---

<div class="post-metadata">

**Author:** ![teodor.andrei](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/teodor.andrei/32/75320_2.png) [@teodor.andrei](https://community.auth0.com/u/teodor.andrei)\
**Post date:** [November 5, 2025, 8:05am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/3 "2025-11-05T08:05:04Z")

</div>

Hi @kurt.dunn and thank you for your question!

Please allow me some time to look into this, and I will get back to you as soon as possible.

Thanks!  
Teodor.

---

<div class="post-metadata">

**Author:** ![teodor.andrei](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/teodor.andrei/32/75320_2.png) [@teodor.andrei](https://community.auth0.com/u/teodor.andrei)\
**Post date:** [November 5, 2025, 12:51pm UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/4 "2025-11-05T12:51:59Z")

</div>

Hi again @kurt.dunn !

You can’t attach `onclick` listeners inside an Action because the Action code runs on the **server** , not in the user’s **browser**.

Instead, you handle this by:

1. **Rendering** the form and stopping execution.
2. **Restarting** the Action from the top when the user _submits_ the form.
3. **Detecting** the submitted data and then running your HTTP calls.
4. **Conditionally** re-rendering the same form with an error if your calls fail.

I hope this answers your question!  
Teodor.

---

<div class="post-metadata">

**Author:** ![kurt.dunn](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kurt.dunn/32/72021_2.png) [@kurt.dunn](https://community.auth0.com/u/kurt.dunn)\
**Post date:** [November 6, 2025, 7:29am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/5 "2025-11-06T07:29:09Z")

</div>

Hi Teodor,

Thank you for your reply. Its very helpful!

Do you have a litle more info or code oh how to do the following? From my understanding the actions are linear and cant be re-run? Whats the event or api to do that?

Is it something like this?

```auto
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {

      api.prompt.render(event.secrets.FORM_ID);

      const input1 = event.prompt.fields.input1
      const input2 = event.prompt.fields.input1

      const getToken = await fetch(url)
      const token = getToken.response()

      // GET THE DATA
      const getData = await fetch(url)
      const data = getToken.response()

      if(data.err === '123'){
        api.prompt.render(event.secrets.FORM_ID, {
            vars: {
              errmsg: '123',
            }
          });
      } else if(data.err === '456') {
        api.prompt.render(event.secrets.FORM_ID, {
            vars: {
              errmsg: '456',
            }
          });

      } else{
        return;
    }

};

```

Or here where I run the logic on the onContinue? How does state for the var work if I rerun the action from the top?

```auto
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {

      api.prompt.render(event.secrets.FORM_ID);

};

/**
* Handler that will be invoked when this action is resuming after an external redirect. If your
* onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onContinuePostLogin = async (event, api) => {

      const input1 = event.prompt.fields.input1
      const input2 = event.prompt.fields.input2

      const getToken = await fetch(url)
      const token = getToken.response()

      // GET THE DATA
      const getData = await fetch(url)
      const data = getToken.response()

      if(data.err === '123'){
        api.prompt.render(event.secrets.FORM_ID, {
            vars: {
              errmsg: '123',
            }
          });
          // HOW TO RERUN THE ACTION AND KEEP STATE FOR ERR MSG??

      } else if(data.err === '456') {
        api.prompt.render(event.secrets.FORM_ID, {
            vars: {
              errmsg: '456',
            }
          });

           // HOW TO RERUN THE ACTION AND KEEP STATE FOR ERR MSG??

      } else{
        return;
    }

};

```

---

<div class="post-metadata">

**Author:** ![teodor.andrei](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/teodor.andrei/32/75320_2.png) [@teodor.andrei](https://community.auth0.com/u/teodor.andrei)\
**Post date:** [November 6, 2025, 11:09am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/6 "2025-11-06T11:09:00Z")

</div>

Hi again @kurt.dunn !

You **do not** use `onContinuePostLogin` for this. That function is _only_ for handling returns from an _external redirect_ (like `api.redirect`). The **`api.prompt.render` call STOPS your Action and waits.** When the user submits the form, Auth0 **re-runs your ENTIRE `onExecutePostLogin` function from the very beginning.**

The _only_ way you know it’s the “second run” is that `event.prompt.fields` will exist.

The flow should look like this:

- **First Run (User logs in):**

1. `onExecutePostLogin` starts.
2. Your code checks: “Does `event.prompt.fields` have any data?”
3. **No** , it’s the first run.
4. Your code goes to the `else` block.
5. It calls `api.prompt.render(FORM_ID)`.
6. The Action **STOPS**. The user sees your form.

- **Second Run (User submits form):**

1. `onExecutePostLogin` starts **AGAIN FROM THE TOP**.
2. Your code checks: “Does `event.prompt.fields` have any data?”
3. **Yes!** The user just submitted `input1` and `input2`.
4. Your code enters the `if` block.
5. You run your `fetch` calls.
6. **Scenario A (Success):** Your `fetch` is good. You `return` normally. The user is logged in.
7. **Scenario B (Error):** Your `fetch` returns an error.
8. You call `api.prompt.render(FORM_ID, { vars: { errmsg: '...' } })`.
9. The Action **STOPS**. The user sees your form _again_, but this time with the error message.

- **Third Run (User submits form again):**

1. `onExecutePostLogin` starts **AGAIN FROM THE TOP**.
2. The whole process repeats from Step 2.

As for an example, you can try this:

```auto
exports.onExecutePostLogin = async (event, api) => {

  // Check if form data exists. If it does, this is NOT the first run.
  if (event.prompt && event.prompt.fields) {
    // STAGE 2: Process the submitted data
    
    // NOTE: Your example code was missing "event.prompt.fields" for input2
    const { input1, input2 } = event.prompt.fields;

    try {
      // GET A TOKEN
      // Make sure to use 'await', secrets, and check the response!
      const tokenResponse = await fetch(event.secrets.TOKEN_URL, { /* ...options... */ });
      if (!tokenResponse.ok) {
        throw new Error('Token request failed');
      }
      const tokenData = await tokenResponse.json();
      const token = tokenData.access_token; // Or whatever your response is

      // GET THE DATA
      const dataResponse = await fetch(event.secrets.DATA_URL, {
        headers: { 'Authorization': `Bearer ${token}` }
      });
      if (!dataResponse.ok) {
        throw new Error('Data request failed');
      }
      const data = await dataResponse.json();

      // Check for your application-specific error
      if (data.err) {
        // Validation Failed: Re-render the form WITH the error
        api.prompt.render(event.secrets.FORM_ID, {
            vars: {
              errmsg: data.err.message || 'An error occurred. Please try again.'
            }
          });
        // IMPORTANT: Stop execution so the user doesn't log in
        return; 
      }
      
      // SUCCESS: All checks passed. Do nothing and let the function end.
      // The user's login will now complete.

    } catch (error) {
      // Network Error: Handle fetch failures or other exceptions
      // Re-render the form with a generic error
      api.prompt.render(event.secrets.FORM_ID, {
        vars: {
          errmsg: 'A system error occurred. Please try again.'
        }
      });
      return; // Stop execution
    }

  } else {
    // STAGE 1: This is the first run. Just show the form.
    api.prompt.render(event.secrets.FORM_ID);
  }
};

```

---

<div class="post-metadata">

**Author:** ![kurt.dunn](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kurt.dunn/32/72021_2.png) [@kurt.dunn](https://community.auth0.com/u/kurt.dunn)\
**Post date:** [November 11, 2025, 1:01am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/7 "2025-11-11T01:01:55Z")

</div>

Hi again @teodor.andrei

That was super helpful! Though i made some changes to get it to work.

I do have 1 final question below.

```auto

/**
 * Initial trigger - render the form
 */
exports.onExecutePostLogin = async (event, api) => {
  const formId = event.secrets.FORM_ID;
  
  // Skip if already validated
  if (event.user.app_metadata?.verified) {
    return;
  }
  
  // Show the form
  api.prompt.render(formId);
};

/**
 * Form submission handler - validate and continue or show error
 */
exports.onContinuePostLogin = async (event, api) => {
  const config = {
    apiBaseUrl: event.secrets.API_BASE_URL,
    username: event.secrets.API_USERNAME,
    password: event.secrets.API_PASSWORD,
    formId: event.secrets.FORM_ID
  };

  // Get form data
  const userInput = event.prompt?.fields?.user_input;

  if (!userInput) {
    api.prompt.render(config.formId, {
      vars: { errmsg: 'Input is required.' }
    });
    return;
  }

  try {
    // Step 1: Get API token
    const authResponse = await fetch(`${config.apiBaseUrl}/auth`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        username: config.username,
        password: config.password
      })
    });

    if (!authResponse.ok) {
      throw new Error('Authentication failed');
    }

    const { token } = await authResponse.json();

    // Step 2: Validate user input with API
    const validationResponse = await fetch(`${config.apiBaseUrl}/validate`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`
      },
      body: JSON.stringify({
        email: event.user.email,
        input: userInput
      })
    });

    const result = await validationResponse.json();

    // Step 3: Success - mark as verified and continue
    if (validationResponse.ok && result.valid) {
      api.user.setAppMetadata('verified', true);
      return; // Continue with login
    }

    // Step 4: Failed - re-render form with error
    api.prompt.render(config.formId, {
      vars: { errmsg: result.error || 'Validation failed. Please try again.' }
    });

  } catch (error) {
    // Error handling - re-render form
    api.prompt.render(config.formId, {
      vars: { errmsg: 'Unable to validate. Please try again.' }
    });
  }
};

```

Obviously when the user clicks continue, the action repeats until the logic is satisfied.  
But, Id like to be able to offer a second button to skip all the logic and then contine through the rest of the actions.

 ![Screenshot 2025-11-11 at 11.57.46 am](https://global.discourse-cdn.com/auth0/original/3X/c/5/c51bae0cc717e3bafbd52e6d979e5353c9fd97f6.png)

In the action, can we do something like this and just return?

```auto
  // Check if user clicked "Skip" button
  if (event.prompt.action === 'skip') {
    console.log(`PIVOTAL_VALIDATION_SKIP: User ${event.user.email} clicked Skip button`);
    // Allow login without validation
    return;
  }

```

Thanks again, for your help!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 25, 2025, 1:02am UTC](https://community.auth0.com/t/form-button-trigger-action-instead-of-flow/194082/8 "2025-11-25T01:02:12Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
