cfmgrv4/app/Libraries/MySocket/GoogleSocket.php
2024-10-08 15:11:33 +09:00

45 lines
1.2 KiB
PHP

<?php
namespace App\Libraries\MySocket;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\ConfigException;
use Google\Client;
use Google\Service\Oauth2;
class GoogleSocket extends Client
{
private $session;
private string $token_code;
public function __construct(string $token_code)
{
parent::__construct();
$this->setAuthConfig(env('socket.google.api.config_file'));
$this->setRedirectUri(base_url(env('socket.google.client.callback_url')));
$this->addScope(Oauth2::USERINFO_EMAIL);
$this->addScope(Oauth2::USERINFO_PROFILE);
// $this->setAccessType('offline');
$this->session = Services::session();
$this->token_code = $token_code;
}
public function setToken(string $access_code): void
{
// 토큰 정보 가져오기
$tokenInfo = $this->fetchAccessTokenWithAuthCode($access_code);
if (isset($tokenInfo['error'])) {
throw new ConfigException($tokenInfo['error']);
}
$token = $tokenInfo[$this->token_code];
// Google Service에 접근하기 위해 Access Token 설정
$this->setAccessToken($token);
// 세션에 Token 값 설정
$this->session->set($this->token_code, $token);
}
public function getToken(): ?string
{
return $this->session->get($this->token_code);
}
}