Files
MOHPortalTest-AllAgents-All…/qwen/hack/src/Services/AuthService.php
2025-10-24 14:54:44 -05:00

88 lines
2.7 KiB
PHP

<?hh // strict
namespace App\Services;
use League\OAuth2\Client\Provider\Google;
use League\OAuth2\Client\Provider\Github;
use Psr\Http\Message\ServerRequestInterface;
class AuthService
{
private Google $googleProvider;
private Github $githubProvider;
public function __construct()
{
$this->googleProvider = new Google([
'clientId' => $_ENV['GOOGLE_CLIENT_ID'] ?? '',
'clientSecret' => $_ENV['GOOGLE_CLIENT_SECRET'] ?? '',
'redirectUri' => $_ENV['APP_URL'] . '/auth/google/callback' ?? 'http://localhost:18000/auth/google/callback',
]);
$this->githubProvider = new Github([
'clientId' => $_ENV['GITHUB_CLIENT_ID'] ?? '',
'clientSecret' => $_ENV['GITHUB_CLIENT_SECRET'] ?? '',
'redirectUri' => $_ENV['APP_URL'] . '/auth/github/callback' ?? 'http://localhost:18000/auth/github/callback',
]);
}
public function getGoogleAuthorizationUrl(): string
{
return $this->googleProvider->getAuthorizationUrl([
'scope' => ['email', 'profile']
]);
}
public function getGithubAuthorizationUrl(): string
{
return $this->githubProvider->getAuthorizationUrl([
'scope' => ['user:email']
]);
}
public function handleGoogleCallback(ServerRequestInterface $request): array
{
$code = $request->getQueryParams()['code'] ?? null;
if (!$code) {
throw new \Exception('No authorization code received from Google');
}
$token = $this->googleProvider->getAccessToken('authorization_code', [
'code' => $code
]);
$user = $this->googleProvider->getResourceOwner($token);
return [
'provider' => 'google',
'id' => $user->getId(),
'email' => $user->getEmail(),
'name' => $user->getName(),
'avatar' => $user->getAvatar(),
];
}
public function handleGithubCallback(ServerRequestInterface $request): array
{
$code = $request->getQueryParams()['code'] ?? null;
if (!$code) {
throw new \Exception('No authorization code received from GitHub');
}
$token = $this->githubProvider->getAccessToken('authorization_code', [
'code' => $code
]);
$user = $this->githubProvider->getResourceOwner($token);
return [
'provider' => 'github',
'id' => $user->getId(),
'email' => $user->getEmail(),
'name' => $user->getName(),
'avatar' => $user->getAvatarUrl(),
];
}
}