"require":{
"google/apiclient" : "^2.0"
}
Route::get('googleLoginPage',['as'=>'googleLoginPage', 'uses'=> 'GoogleController@googleLoginPage'])
Route::get('callbackGoogle',['as'=>'callbackGoogle', 'uses'=> 'GoogleController@callbackGoogle'])
use Google/Client as Google_Client;
# 구글 로그인 페이지
public function googleLoginPage(){
$this->getGoogleOauthClient("login");
}
# 구글 유저 정보 가져오기
public function callbackGoogle(){
$client = $this->getGoogleOauthClient("auth");
$plus = new \Google_Service_Oauth2($client);
$userData = $plus->userinfo->get();
# 유저 정보
$social_id = $userData->id;
$social_email = $userData->email;
$social_name = $userData->name;
}
private function getGoogleOauthClient($type){
$client_id = $this->client_id;
$client_secret = $this->client_secret;
$authCode = $_GET['code'];
$redirectURI = '도메인/callbacokGoogle';
$client = new Google_Client();
$client->setScopse(
array(
\Google_service_Oauth2::USERINFO_PRIFILE,
\Google_service_Oauth2::USERINFO_EMAIL
)
);
$client->setClientId($client_id);
$client->setRedirectUri($redirectURI);
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
if(!empty($authCode) && $type == 'auth'){
# 로그인 페이지가 아닌, 인증 받은 후 유저 정보를 가져올때 $client_secret 필요
$client->setClientSecret($client_secret);
}
if($client->isAccessTokenExpired()){
if($client->getRefreshToken()){
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}else{
if(!empty($authCode) && $type == 'auth'){
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
if(array_ket_exists('error', $accessToken)){
throw new Exception(join(',',$accessToken));
}
}elseif($type =='login'){
# 구글 로그인 페이지로 리다이렉트
header("location".$client->createAuthUrl());
}
}
}
if($type != "login"){
return $client;
}
}