I've implemented the Apple Sign In on the website, but I can't retrieve user's full name. It's added to scope but is not being posted (it's being sent only the first time). Is there a way to get it somehow?
Frontend:
<div class="login-btn" id="appleid-signin" data-color="black" data-border="true" data-type="sign in"></div>
<script type="text/javascript"
src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
<script type="text/javascript">
AppleID.auth.init({
clientId: 'net.exmample.oauth',
scope: 'email name',
response_type: 'code',
response_mode: 'form_post',
redirectURI: 'https://example.net/appletest',
usePopup : false
});
const buttonElement = document.getElementById('appleid-signin');
buttonElement.addEventListener('click', () => {
AppleID.auth.signIn();
});
</script>
Backend:
$identityToken = Input::post('id_token');
$appleSignInPayload = ASDecoder::getAppleSignInPayload($identityToken);
$email = $appleSignInPayload->getEmail();
var_dump('email: ' . $email);
echo '<br>';
$user = $appleSignInPayload->getUser();
var_dump('userid: ' . $user);
echo '<br>';
$isValid = $appleSignInPayload->verifyUser($user);
echo 'is valid : ';
var_dump($isValid);
echo '<br>';
$clientId = 'net.example.oauth';
$teamId = 'TT123';
$keyId = 'KK123';
$code = Input::post('code');
echo 'Code: ' . $code . '<br>';
$claims = [
'iss' => $teamId,
'aud' => 'https://appleid.apple.com',
'sub' => $clientId,
'iat' => time(),
'exp' => time() + 3600,
];
$headers = ['kid' => $keyId, 'alg' => 'ES256'];
$privateKey = <<<EOD-----BEGIN PRIVATE KEY-----key goes here-----END PRIVATE KEY-----EOD;
$publicKey = <<<EOD-----BEGIN PUBLIC KEY-----key goes here-----END PUBLIC KEY-----EOD;
$client_secret = JWT::encode($claims, $privateKey, 'ES256', $keyId, $headers);
// var_dump($client_secret);
$decoded = JWT::decode($client_secret, $publicKey, ['ES256']);
// var_dump($decoded);
$ch = curl_init();
$data = [
'client_id' => $clientId, // app id?
'code' => $code, //from request
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => 'https://example.net/appletest'
];
curl_setopt($ch, CURLOPT_URL, "https://appleid.apple.com/auth/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
$response = json_decode($server_output, true);
if ($response['access_token']) {
var_dump($response);
$appleSignInPayload = ASDecoder::getAppleSignInPayload($response['id_token']);
}
Stack overflow requires more details to the code. So I can add that it uses AppleSignIn\ASDecoder and \Firebase\JWT\JWT libraries. Also I've discovered that in the backend you have to supply same redirect_uri as on the frontend, otherwise you get {"invalid_grant"} error. Hope it saves someone a few hours.
If someone in trouble reads this, here is how to extract public key out of .p8 file: openssl ec -in AuthKey_KEY_ID.p8 -pubout -out AuthKey_KEY_ID_Public.p8
UPD: Seems it's true that name can be obtained only the first time. I've tested it with a friend and got a response.
array(3) {
["code"]=> string(64) "c94b9775110randoma918bc357.0.nsqty.covC4GSS1e2O4..."
["id_token"]=> string(766) "eyJraWQiOiJlWGF1bm1MIiwiYWrandomyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaW..."
["user"]=> string(82) "{"email":"[email protected]","name":{"firstName":"Eeee","lastName":"Ggg"}}" }
UPD 2: It's possible to delete application using the mobile phone in Settings->Passwords & Security->Apps Using Your Apple ID. After the deletion user data will be posted again. But it's only good for testing updating user data on production still requires profile update over time.
UPD 3: It's possible to unbind the App from your apple account not only by phone, but also on the https://appleid.apple.com/account/manage website in the Security->APPS & WEBSITES USING APPLE ID->Manage section
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.