//JUST MAPPING UNIQUE USER ID(token.sub) WITH ALLDEAL DATABASE ( NOT USING OR AUTHORIZING TOKEN )
function apple(){
$result = 'success';
try{
//data
$code = $_POST['code'];
$tokens = decode($_POST['id_token']);
$user = !empty($_POST['user']) ? json_decode(str_replace('\\','',$_POST['user'])) : null;
$appleId = $tokens[1]->sub;
$destination = 'https://alldeal.kr/Login?applelogin=error';
$firstName = !empty($user->name->firstName) ? $user->name->firstName : null;
$lastName = !empty($user->name->lastName) ? $user->name->lastName : null;
$name = $lastName.$firstName;
$email = !empty($user->email) ? $user->email : null;
//check & login
$row = $this->member->select("mb_id,mb_stt,pt_id,mb_passwd,mb_login_cnt,count(mb_id) as cnt"," and mb_sns_id_2 = '{$appleId}' and pt_id = '{$this->shopId}'");
if( $row['cnt'] < 1 ){ // if not member -> JOIN
//generate secret key
$privKey = openssl_pkey_get_private(file_get_contents('key/<파일명.pem>', true));
$secret = generateJWT($this->appleKey, $this->appleTeam, $this->appleService, $privKey);
//access_token request
$data = [
'client_id' => $this->appleService,
'client_secret' => $secret,
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://appleid.apple.com/auth/token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$serverOutput = curl_exec($ch);
$output = json_decode($serverOutput,true);
curl_close($ch);
$refresh = $output['refresh_token'];
$res = "CONTINUE";
$sub = <토큰 발급 로직>
$destination = 'https://alldeal.kr/Login/Phone?res='.$res.'&sub='.$sub.'&name='.$name.'&email='.$email;
}else{
//LOGIN
if( $row['mb_stt'] != 2 || empty($row['mb_id'])){
$result['res'] = "FAILED";
echo json_encode($result);
exit;
}
$res = "LOGIN";
$memId = $row['mb_id'];
$destination = 'https://alldeal.kr/Login/Phone?res='.$res.'&memId='.$memId;
}
}catch(Exception $e){
$result['res'] = "FAILED";
$result['error'] = $e;
$destination = 'https://alldeal.kr/Login?applelogin=error';
}
header('Location:'.$destination);
exit;
}