Form button, trigger action instead of flow?

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.


/**
 * 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.

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

  // 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!