Skip to content

Commit

Permalink
Fix exception when enabling two-factor authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Nov 3, 2018
1 parent b829548 commit 5098bc1
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/Services/Authy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;
use Srmklive\Authy\Contracts\Auth\TwoFactor\PhoneToken as SendPhoneTokenContract;
use Srmklive\Authy\Contracts\Auth\TwoFactor\Provider as BaseProvider;
Expand Down Expand Up @@ -50,24 +51,36 @@ public function isEnabled(TwoFactorAuthenticatable $user)
* @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
* @param bool $sms
*
* @throws \Exception
*
* @return void
*/
public function register(TwoFactorAuthenticatable $user, $sms = false)
{
$response = json_decode((new HttpClient())->post($this->config['api_url'].'/protected/json/users/new?api_key='.$this->config['api_key'], [
'form_params' => [
'user' => [
'email' => $user->getEmailForTwoFactorAuth(),
'cellphone' => preg_replace('/[^0-9]/', '', $user->getAuthPhoneNumber()),
'country_code' => $user->getAuthCountryCode(),
try {
$request = (new HttpClient())->post($this->config['api_url'].'/protected/json/users/new?api_key='.$this->config['api_key'], [
'form_params' => [
'user' => [
'email' => $user->getEmailForTwoFactorAuth(),
'cellphone' => preg_replace('/[^0-9]/', '', $user->getAuthPhoneNumber()),
'country_code' => $user->getAuthCountryCode(),
],
],
],
])->getBody(), true);
]);

$response = $request->getBody()->getContents();
$response = \GuzzleHttp\json_decode($response, true);

$user->setTwoFactorAuthProviderOptions([
'id' => $response['user']['id'],
'sms' => $sms,
]);
$user->setTwoFactorAuthProviderOptions([
'id' => $response['user']['id'],
'sms' => $sms,
]);
} catch (ClientException $e) {
$errors = $e->getResponse()->getBody()->getContents();
$errors = \GuzzleHttp\json_decode($errors, true);

throw new \Exception($errors['message']);
}
}

/**
Expand Down

0 comments on commit 5098bc1

Please sign in to comment.