Skip to content

Commit

Permalink
modified: src/providers/GoogleProvider.php
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 committed Nov 22, 2023
1 parent 262ab43 commit 2134b0c
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions src/providers/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace Axm\Socialite\Providers;

use Axm\Socialite\MakesHttpRequests;

/**
* GoogleProvider - A provider for Google OAuth2 authentication.
*/
class GoogleProvider
{
use MakesHttpRequests;

/**
* Base URL for Google API.
*/
Expand All @@ -24,7 +21,7 @@ class GoogleProvider
/**
* URL for obtaining access token.
*/
const ACCESS_TOKEN_URL = 'https://oauth2.googleapis.com/token';
const ACCESS_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token';

/**
* URL for fetching user information.
Expand All @@ -38,7 +35,6 @@ class GoogleProvider

/**
* Configuration array for the provider.
*
* @var array
*/
private $config;
Expand Down Expand Up @@ -66,7 +62,6 @@ public function redirect()
];

$authorizeUrl = self::AUTHORIZE_URL . '?' . http_build_query($params);
// Redirects user to the authorization URL
$this->makeRedirect($authorizeUrl);
}

Expand All @@ -76,15 +71,35 @@ public function redirect()
* @return string Access token.
* @throws \RuntimeException If unable to obtain access token.
*/
public function getAccessToken()
public function user()
{
$code = $this->getCode();
if (empty($code)) {
return null;
} else {
app()->response->redirect('/home');
if (!empty($code)) {
try {
$params = $this->getParams($code);

$curl = new \Axm\Http\Curl();
$response = $curl->post(self::ACCESS_TOKEN_URL, $params);
$data = json_decode($response['response'], true);

$userInfoUrl = self::USER_INFO_URL . '?access_token=' . $data['access_token'];
$userInfoResponse = $curl->get($userInfoUrl);
$userInfo = json_decode($userInfoResponse['response'], true);

$curl->close();
} catch (\Throwable $e) {
throw new \RuntimeException('Failed to obtain access token: ' . $e->getMessage());
}

return (object)$userInfo;
}
}

/**
* @return array
*/
public function getParams($code): array
{
$params = [
'code' => $code,
'client_id' => $this->config['client_id'],
Expand All @@ -93,46 +108,29 @@ public function getAccessToken()
'grant_type' => 'authorization_code',
];

try {
$response = $this->sendRequest(self::ACCESS_TOKEN_URL, 'POST', $params);
app()->response->redirect('/home');
} catch (\Throwable $e) {
throw new \RuntimeException('Failed to obtain access token: ' . $e->getMessage());
}

return $response['access_token'];
return $params;
}

/**
* Get user information using the provided access token.
*
* @param string $access_token Access token.
* @return object User information.
* @throws \RuntimeException If unable to fetch user information.
* @return mixed
*/
public function getUserInfo(string $access_token): object
public function getCode()
{
$headers = ['Authorization: Bearer ' . $access_token];

try {
$userInfo = $this->sendRequest(self::USER_INFO_URL, 'GET', [], $headers);
} catch (\Throwable $e) {
throw new \RuntimeException('Failed to fetch user information. ' . $e->getMessage());
}

return (object)$userInfo;
return app()
->request
->get('code') ?? null;
}

/**
* @param string $url
*
* @return [type]
*/
public function makeRedirect(string $url)
{
if (!headers_sent()) {
app()->response->redirect($url);
exit;
app()
->response
->redirect($url);
}
}
}

0 comments on commit 2134b0c

Please sign in to comment.