You are here

public function AuthController::verify_email in Auth0 Single Sign On 8

Same name and namespace in other branches
  1. 8.2 src/Controller/AuthController.php \Drupal\auth0\Controller\AuthController::verify_email()

Send the verification email.

1 string reference to 'AuthController::verify_email'
auth0.routing.yml in ./auth0.routing.yml
auth0.routing.yml

File

src/Controller/AuthController.php, line 508

Class

AuthController
Controller routines for auth0 authentication.

Namespace

Drupal\auth0\Controller

Code

public function verify_email(Request $request) {
  $idToken = $request
    ->get('idToken');

  /**
   * Validate the ID Token
   */
  $auth0_domain = 'https://' . $this->domain . '/';
  $auth0_settings = array();
  $auth0_settings['authorized_iss'] = [
    $auth0_domain,
  ];
  $auth0_settings['supported_algs'] = [
    $this->auth0_jwt_signature_alg,
  ];
  $auth0_settings['valid_audiences'] = [
    $this->client_id,
  ];
  $auth0_settings['client_secret'] = $this->client_secret;
  $auth0_settings['secret_base64_encoded'] = $this->secret_base64_encoded;
  $jwt_verifier = new JWTVerifier($auth0_settings);
  try {
    $user = $jwt_verifier
      ->verifyAndDecode($idToken);
  } catch (\Exception $e) {
    return $this
      ->failLogin(t('There was a problem resending the verification email, sorry for the inconvenience.'), "Failed to verify and decode the JWT ({$idToken}) for the verify email page: " . $e
      ->getMessage());
  }
  try {
    $userId = $user->sub;
    $url = "https://{$this->domain}/api/users/{$userId}/send_verification_email";
    $client = \Drupal::httpClient();
    $client
      ->request('POST', $url, array(
      "headers" => array(
        "Authorization" => "Bearer {$idToken}",
      ),
    ));
    drupal_set_message(t('An Authorization email was sent to your account'));
  } catch (\UnexpectedValueException $e) {
    drupal_set_message(t('Your session has expired.'), 'error');
  } catch (\Exception $e) {
    drupal_set_message(t('Sorry, we couldnt send the email'), 'error');
  }
  return new RedirectResponse('/');
}