You are here

function configure_otp_over_phone::miniorange_saml_resend_otp in Google Authenticator / 2 Factor Authentication - 2FA 8

File

src/Form/configure_otp_over_phone.php, line 136

Class

configure_otp_over_phone

Namespace

Drupal\miniorange_2fa\form

Code

function miniorange_saml_resend_otp(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form_state
    ->setRebuild();
  $customer = new MiniorangeCustomerProfile();
  $custID = $customer
    ->getCustomerID();
  $api_key = $customer
    ->getAPIKey();
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $utilities = new MoAuthUtilities();
  $custom_attribute = $utilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  $phone_number = str_replace(' ', '', $form['miniorange_phone']['#value']);
  if (empty($phone_number)) {
    \Drupal::messenger()
      ->addMessage(t('Please enter phone number first.'), 'error');
    return;
  }
  $currentMethod = "PHONE_VERIFICATION";
  $customer_config = new MiniorangeCustomerSetup($user_email, $phone_number, NULL, NULL);
  $response = $customer_config
    ->send_otp_token($phone_number, $currentMethod, $custID, $api_key);
  $send_otp_response = json_decode($response);
  if ($send_otp_response->status == 'SUCCESS') {

    // Store txID.
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_tx_id', $send_otp_response->txId)
      ->save();
    \Drupal::messenger()
      ->addStatus(t('You will get a call on <strong>' . $phone_number . '</strong> shortly, which prompts OTP. Please enter the OTP to verify your phone number.'));
  }
  elseif ($send_otp_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addMessage(t('cURL is not enabled. Please enable cURL'), 'error');
  }
  else {
    \Drupal::messenger()
      ->addMessage(t($send_otp_response->message), 'error');
  }
}