You are here

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

Same name and namespace in other branches
  1. 8 src/Controller/AuthController.php \Drupal\auth0\Controller\AuthController::verify_email()
1 string reference to 'AuthController::verify_email'
auth0.routing.yml in ./auth0.routing.yml
auth0.routing.yml

File

src/Controller/AuthController.php, line 1036
Contains \Drupal\auth0\Controller\AuthController.

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 = [];
  $auth0_settings['authorized_iss'] = [
    $auth0_domain,
  ];
  $auth0_settings['supported_algs'] = [
    $this->auth0JwtSignatureAlg,
  ];
  $auth0_settings['valid_audiences'] = [
    $this->clientId,
  ];
  $auth0_settings['client_secret'] = $this->clientSecret;
  $auth0_settings['secret_base64_encoded'] = $this->secretBase64Encoded;
  $jwt_verifier = new JWTVerifier($auth0_settings);
  try {
    $user = $jwt_verifier
      ->verifyAndDecode($idToken);
  } catch (\Exception $e) {
    return $this
      ->failLogin($this
      ->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 = $this->httpClient;
    $client
      ->request('POST', $url, [
      "headers" => [
        "Authorization" => "Bearer {$idToken}",
      ],
    ]);
    \Drupal::messenger()
      ->addStatus($this
      ->t('An Authorization email was sent to your account.'));
  } catch (\UnexpectedValueException $e) {
    \Drupal::messenger()
      ->addError($this
      ->t('Your session has expired.'));
  } catch (\Exception $e) {
    \Drupal::messenger()
      ->addError($this
      ->t('Sorry, we could not send the email.'));
  }
  return new RedirectResponse('/');
}